Removing Pointless Code

These are lecture notes from my Computer Science course. For learning about functional programming, in particular Haskell, I recommend Programming in Haskell.

Lambda functions

Backslash in Haskell is lambda, e.g. (\x c -> x == e || c)

Removing pointless stuff

Here’s an example:

reverse xs = foldl snoc [] xs
	where
	snoc ys y = y : ys

reverse = foldl snoc []
	where
	snoc ys y = y : ys

reverse = foldl snoc []
	where
	snoc = flip (:)

reverse = foldl (flip (:)) []

You may think this is removing type security. But remember at the top of all this you will have:

reverse :: [a] -> [a]

So type security is maintained.

This entry was posted in fun, lecture. Bookmark the permalink.