Functional programming in sh

Posted on May 21, 2012 by Mike Ledger

Warning: this code is horribly broken outside of use in Zsh.

As such, all occurences of “sh” are referring to POSIX shells ie bash and /bin/sh, (which is usually symlinked to bash, minus some bash-only features).

I find myself trying to do things I can do in GHCi more each passing day in my innocent /bin/sh. I find myself seeking a good balance between the numerous layers of hacks that compose shell script and the purely functional wonder of a Haskell program. Such efforts already exist, most recently in Shelly.hs, but I believe this to be tackling the problem from the wrong end. Instead of hacking together a library to make Haskell code reminiscent of the beloved /bin/sh, the problem should be tackled with extreme prejudice by hacking together a sh script to mimic the best of Haskell, and retain the beauty of shell scripts.

The beauty of shell scripts?

The horror of shell scripts

Complexity and fun: sh vs Haskell

When we are compare such vastly different languages as these, you have to remember that they both have extremely different purposes and real world uses. Haskell is an extremely feature rich general purpose programming language. sh is very convenient for simple and mundane tools that are best made by composing other programs together via piping. For those of us who have no idea of what they’re doing 90% of the time when they are using bash (that would be me), there is:

Enter adds some tools seen and used mainly by functional programmers including take, drop, scanl, foldl, map and lambdas. Because sh has no real type system, the convention is to use pipes as you would use lists in Haskell, which allows functions to be easily composable via piping.

Anonymous functions in are deceptively dumb. will simply consume and remember every argument given up until a “.”, “->”, “→” or “:” is found, then reading each argument before evaluating the body of the function.

A few demos

# sum of a bash array
boring_sum() {
    for i in $array; do
        s=$(($i + $s))
    echo $s

# sum of a bash array, piping the array in
boring_sum_pipe() {
    while read i; do
        s=$(($s + $i))
    echo $s

# now, with

$ sum(){ foldl λ a b . 'echo $(($a + $b))' } 
$ list {1..100} | sum

# and to show off, 

$ product(){ foldl λ a b . 'echo $(($a * $b))' }
$ list {1..20} | product

$ factorial(){ list {1..$1} | product }
$ factorial 5

$ foobar(){ product | λ l . 'list {1..$l}' | sum | md5sum }
$ list {1,2,3} | foobar
fe9d26c3e620eeb69bd166c8be89fb8f  -

$ id(){ λ x . '$x' }
$ id <<< 'echo :)'

# Oh no, the boss wants me to calculate the sum of every integer between 400 and 500! Whatever shall I do?!
$ list {400..500} | foldl λ x y . 'echo $(($x + $y))'
# Thanks, I don't know where I would be without you.

GitHub project.

blog comments powered by Disqus