Matching checklists using Haskell

by George Pollard

Our target for this exercise is “Things that other languages should take from Lisp”.

Bignum support

In Scheme and Common Lisp, by default you can’t overflow an integer…

Prelude> fac n = product [2..n]
Prelude> fac 100
933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582
51185210916864000000000000000000000000

In Common Lisp, you can force your code to use fixed-size numbers (fixnums) for efficiency…

Prelude> let fac n = product [2..n] :: Int
Prelude> fac 17
-288522240

Ruby and Python, by default, treat language-integers as logical-integers. Java, C, C++, and Perl don’t.

Prelude> :t 15
15 :: (Num t) => t

Optional type declarations

[Common Lisp] allows, but does not require, type declarations.

Prelude> let doubleApply f x = f (f x)
Prelude> :t doubleApply
doubleApply :: (t -> t) -> t -> t

The compiler can also use type declarations to perform compile-time typechecking. (Sadly, it isn’t required to do this.)

Prelude> doubleApply 3 doubleApply
 
<interactive>:1:12:
    No instance for (Num (((t -> t) -> t -> t) -> (t -> t) -> t -> t))
      arising from the literal `3' at <interactive>:1:12

Tail recursion

Proper (unbounded) tail-recursion (Scheme.)

Prelude> let loop = do { putStr "."; loop }
Prelude> loop
......................................
......................................
[etc]
......................................
..........................Interrupted.

(But see the Haskell wiki. Laziness introduces a small trick here.)

Functions which depend upon the type of multiple arguments

Methods with multiple-dispatch (Common Lisp.)

(This doesn’t exactly match conceptually because Haskell doesn’t have nominal overloading.)

multi Nothing Nothing = 0
multi (Just x) (Just y) = x + y
multi Nothing (Just y) = y
multi (Just x) Nothing = x

Fast implementations

Some Lisp implementations are fast.

Haskell #7 on the Great Language Shootout (in terms of speed), and getting faster by the day as the backend of GHC is rewritten.

Syntactic Simplicity

Syntactic simplicity (Scheme.)

The Haskell CFG.

Or perhaps Python is another incarnation of syntactic simplicity.

The Layout Rule