Last active
August 29, 2015 14:07
-
-
Save jdegoes/eeed3f62cd53848c3e66 to your computer and use it in GitHub Desktop.
Death to data types
This file contains hidden or 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
module Main where | |
import Debug.Trace | |
import Prelude (flip, const, id, ($), (+), (<<<), show) | |
type Tuple a b = forall z. (a -> b -> z) -> z | |
mkTuple :: forall a b. a -> b -> Tuple a b | |
mkTuple a b = \f -> f a b | |
fst :: forall a b. Tuple a b -> a | |
fst = flip ($) (flip $ const id) | |
snd :: forall a b. Tuple a b -> b | |
snd = flip ($) (const id) | |
bimap :: forall a b c d. (a -> c) -> (b -> d) -> Tuple a b -> Tuple c d | |
bimap f g = \t -> mkTuple (f $ fst t) (g $ snd t) | |
type List a = forall z. (z -> a -> z) -> z -> z | |
nil :: forall a. List a | |
nil = const id | |
cons :: forall a. a -> List a -> List a | |
cons a xs = \f -> \z -> xs f (f z a) | |
list :: List Number | |
list = cons 1 (cons 2 (cons 3 (nil))) | |
foldl :: forall z a. (z -> a -> z) -> z -> List a -> z | |
foldl f z l = l f z | |
main = trace <<< show $ foldl (+) 0 list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment