A Joy to Work With
Today, I’ve been playing Joy, a purely functional concatenative programming language.Thanks to Shae for reminding me of Joy, and for suggesting that it might be useful for systems programming.
Everything in Joy is a function that takes a stack and returns a stack.
Hello world looks like thisSourced from here:
"Hello, world!\n" putchars .
Piece by piece, "Hello, world!\n" puts a literal string on the stack.
putchars takes a stack consisting of a single string, and prints it to the console. The . character tells joy to execute the sequence of functions.
Command Line Arguments
My interest in Joy is for short executable programs, so I wanted to know how to learn how to use command line arguments in Joy.
Here’s what I came up withI’m using a joy executable compiled from here:
#!/usr/bin/env joy
argv 1 drop [" " concat] map [putchars] step .
Piece by piece:
argv takes all arguments passed in with the program, including the program’s name, and returns an aggregate (here a list of strings).
1 is a pure function that takes a stack and adds itself to that stack.
drop takes an aggregate and a number, and drops number of elements from the aggregate. In this case, the name of the calling program.
[" " concat] is a quote, which allows me to pass a function unexecuted onto the stack. I believe in this case the concat is partially applied with " " as the second argument.
concat takes two sequences and concatenates them.
map takes an aggregate and a quoted function, and executes the function on each element of the aggregate, returning a stack containing a new aggregate with elements transformed by the function. Here it will take each element from our modified argv aggregate and concatenates them with " ".
step takes an aggregate and a quoted function, and executes the function once for each element of the aggregate. In this case, I call putchars for each string.
Thus:
$ ./hello.joy hello world
hello world
I’ve been using the resources found here which have been most useful at making this short program work.