4 kyu

Church numbers

343 of 558CarstenKoenig

Description:

The goal of this little kata to implement

  • addition
  • multiplication
  • power

functions for Church numerals that are represented as

newtype Number = Nr (forall a. (a -> a) -> a -> a)

zero :: Number
zero = Nr (\ _ z -> z)

succ :: Number -> Number
succ (Nr a) = Nr (\ s z -> s (a s z))

one :: Number
one = succ zero
newtype Church = Church (forall a. (a -> a) -> a -> a)

zero :: Church
zero = Church (\_ x -> x)

succ :: Church -> Church
succ (Church n) = Church (\f x -> f (n f x))

one :: Church
one = succ zero
zero = f => x => x

succ = n => f => x => f (n (f) (x))

one = succ( zero )

// These definitions are actually in Preloaded
zero = \ f x . x

succ = \ n . f x . f (n f x)

one = succ zero
succ = lambda n: lambda f: lambda x: f(n(f)(x))
zero = lambda f: lambda x: x
one = succ(zero)

# These definitions are actually in Preloaded

hints

  • you can finish it by just replacing the undefined placeholders in Haskell / typed holes in PureScript - no need to change the arguments - they might provide some hints.
  • the solutions can be very short - you don't have to write much if you think about it
  • you don't really need lambda-functions
  • But if you feel like it replace them with your own ideas.
Puzzles

More By Author:

Check out these other kata created by CarstenKoenig

Stats:

CreatedNov 20, 2014
PublishedNov 20, 2014
Warriors Trained2164
Total Skips509
Total Code Submissions1888
Total Times Completed558
Haskell Completions343
PureScript Completions22
JavaScript Completions117
λ Calculus Completions55
Python Completions98
Total Stars73
% of votes with a positive feedback rating93% of 144
Total "Very Satisfied" Votes128
Total "Somewhat Satisfied" Votes13
Total "Not Satisfied" Votes3
Total Rank Assessments23
Average Assessed Rank
4 kyu
Highest Assessed Rank
2 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • CarstenKoenig Avatar
  • donaldsebleung Avatar
  • JohanWiltink Avatar
  • Voile Avatar
  • ice1000 Avatar
  • Bubbler Avatar
  • solitude Avatar
  • Kacarott Avatar
Ad