2007-01-03
Implementing concatenative words with Pattern Matching
There's a dicussion on the concatenative mailing list about concatenative languages and macros. The discussion veered into pattern matching and Manfred Von Thun wrote about using pattern matching to implement common concatenative operations.
I wrote previously about using pattern matching in Factor to do similar, calling the word 'shuffle'. For example, 2dup, could be implemented as:
{ ?a ?b } { ?a ?b ?a ?b } shuffle
I didn't think of it at the time but Manfred's article prompted me to try doing 'cons' and similar operations that actually have to look inside data structures. Since pattern matching does this already, 'shuffle' does it as well. For example, curry could be:
: my-curry { ?a [ ?b ] } { [ ?a ?b ] } shuffle ;
"hello" [ print ] my-curry .
! => [ "hello" print ]