Skip to content

Instantly share code, notes, and snippets.

@markhibberd
Created May 22, 2014 11:04
Show Gist options
  • Save markhibberd/7c8eca59e88eb94efd0f to your computer and use it in GitHub Desktop.
Save markhibberd/7c8eca59e88eb94efd0f to your computer and use it in GitHub Desktop.
Fixed points
Prelude> fix (\f c -> if null c then 0 else 1 + (f (tail c))) [1, 2, 3]
3
Prelude> fix (\f c -> if null c then head c else 1 + (f (tail c))) [1, 2, 3]
*** Exception: Prelude.head: empty list
Prelude> fix (\f c -> if null c then 0 else 1 + (f (tail c))) [1, 2, 3]
3
Prelude> fix (\f c -> if null c then 0 else (head c) + (f (tail c))) [1, 2, 3]
6
Prelude> :t fix
fix :: (t -> t) -> t
Prelude> let fix f = let x = f x in x
Prelude> -- t :: ([Int] -> Int)
Prelude> -- fix' :: (([Int] -> Int) -> ([Int] -> Int)) -> [Int] -> Int
Prelude> -- fix' :: (([Int] -> Int) -> [Int] -> Int) -> [Int] -> Int
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment