19 January 2006

random_trial, abbreviated

I just realized that the function random_trial in my previous post could be written like this:

random_trial n target =
    any (>= target) . map length . group . sort . take n . map random_birthday

The meaning is exactly the same. It's very similar to a sh pipeline, only written backwards. You might write something like this:

cat ./random-numbers.txt | awk '{print random_birthday($1)}' | head -n $n | sort | group.sh | awk '{print $NF}' | any-greater-than-or-equal.sh $target

The Haskell function is analogous, only data flows right-to-left.

No comments: