Last active
December 31, 2015 20:16
-
-
Save charles-cooper/fc9be14c88111762f348 to your computer and use it in GitHub Desktop.
example of church encoding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- see https://docs.google.com/presentation/d/1f-avqUksUCO8vvwn1KQNu-LIVwqNc1EbQz1l_stTD8o/edit#slide=id.gf6104008a_0_810 | |
{-# LANGUAGE RankNTypes #-} | |
type PersonLambdas = forall a. (String -> Int -> a) -> (String -> Int -> Double -> a) -> a | |
child :: String -> Int -> PersonLambdas | |
child s i = \childL adultL -> childL s i | |
adult :: String -> Int -> Double -> PersonLambdas | |
adult s i d = \_ adultL -> adultL s i d | |
name :: PersonLambdas -> String | |
name lam = let | |
childL = \s _ -> s | |
adultL = \s _ _ -> s | |
in lam childL adultL | |
age :: PersonLambdas -> Int | |
age lam = let | |
childL = \_ a -> a | |
adultL = \_ a _ -> a | |
in lam childL adultL | |
netWorth :: PersonLambdas -> Double | |
netWorth lam = let | |
childL = \_ _ -> 0.0 | |
adultL = \_ _ nw -> nw | |
in lam childL adultL | |
main = do | |
let bobby = child "Bobby" 5 | |
print $ age bobby | |
print $ name bobby | |
let charles = adult "Charles" 24 (1.0/0.0) | |
print $ netWorth charles | |
print $ netWorth bobby |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 | |
"Bobby" | |
Infinity | |
0.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment