Hacker News new | past | comments | ask | show | jobs | submit login

Wikipedia tends to be technical about math/CS subjects. In other fields, you can use it as a textbook for a 101 course - I once heard of an astronomy professor who did this, and this is what I recommend people do with linguistics. (Except syntax, which, as the most CS-adjacent subfield, gets the most technical treatment.)

The page for Joy, Manfred von Thun's concatenative language, is a little better:

> Joy is unusual ... in its ... lack of formal parameters.

So, where in a standard ALGOL- or C-type language, you'd define a function that squares a number like this:

  def square(x):
    return x * x
And in Haskell... OK, I don't know Haskell, but if I click through tryhaskell.org a bit, I see this:

  let square x = x * x in square 2
But in Joy, the way to write a function that squares a number doesn't involve explicit references to parameters at all:

  dup *
Or, if you want to name your function:

  DEFINE square == dup *.
Every function takes a stack and returns a stack. `dup` takes a stack [A B C...] and returns [A A B C...], for any values of A, B, and C. `` takes a stack [A:int B:int C...] and returns [AB C...].

Now, Joy in particular was written as a research language about recursive combinators. So a mostly-idiomatic* factorial function in Joy looks like this:

  DEFINE fac == 
    [0 =]
    [1 +]
    [dup 1 -]
    [*]
    linrec.
And then you call that with `5 fac` or something.

The neat thing about this is that you don't need to define functions and reference them from inside themselves in order to get recursion, and you can write code that's a little more legible than using the Y combinator for everything. You could also write something like this:

  DEFINE fac-explicit-recursion ==
    [0 =]
    [pop 1]
    [dup 1 - fac-explicit-recursion *]
    ifte. 
But you don't have to.

`linrec` is a function that expects four quoted functions as the top elements on the stack. It executes the first function; if that 'returns' true, it executes the second one and stops, and if not, it executes the third function, recurses, and then executes the fourth.

A while ago, I started working on a Joy interpreter in the browser. I haven't gotten around to finishing it yet, but it's at least working well enough that you can (hopefully) step through the https://defseg.io/joyjs/

For a flashier example (which won't work there right now, because I haven't implemented... `binrec`... yet), here's quicksort:

  [size 1 <=]
  []
  [uncons [>] split]
  [[swap] dip cons concat] 
  binrec
`binrec` is another recursive combinator that expects four quoted functions. It handles the first two functions there the same way as `linrec` - executes the first (here, "does this list have one or fewer elements?"), and if true, executes the second (here, do nothing).

But if the condition is false, it executes the third function to produce two values, recurses on both, and then uses the fourth function to combine them. So you get your pivot, split your list, recurse on both lists, and then stitch the two result lists and the pivot back together in sorted order.

* 'Mostly' because you'd actually write `[null] [succ] [dup pred] [*] linrec`, but the two functions are identical. (`null` is overloaded to also test for zero elements in an aggregate, but if you pass it an aggregate it'll throw a type error anyway.) Similarly for quicksort: `[small] [] [uncons [>] split] [enconcat] binrec`.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: