I am currently learning Haskell and also participating in a rather theoretical lecture about functional programming at university.
I know that this is purely theoretical/academic question, but nevertheless I am interested how to express different simple functions simply with pure lambda calculus (i.e. without any constants defined).
Some lecture materials of mine define the boolean values such as:
True = \xy.x
False = \xy.y
(\ denoting the lambda symbol)
If they are defined like these selector functions, the if-condition can be easily defined as:
If = \x.x
Now, I'm trying to come up with some short form for the logical "and"-function. My first guess is:
and = \xy.{(If x)[(If y) True False] False}
So basically this lambda function would receive 2 arguments u v where both have to be typed like True/False. If I do various beta-reductions with all 4 combinations of the logic table I receive the right result.
Nevertheless this function looks a little ugly and I'm thinking about making it more elegant. Any proposals here?
Functional programming is rooted in lambda calculus, which constitutes the world's smallest programming language.
The majority of functional programming languages at all do not require you to 'learn' lambda calculus, whatever that would mean, lambda calculus is insanely minimal, you can 'learn' its axioms in an under an hour.
A redex, or reducible expression, is a subexpression of a λ expression in which a λ can be applied to an argument. With more than one redex, there is more than one evaluation order. e.g. (+(* 3 4) (* 7 6)). Normal Order Evaluation. Always reduce leftmost redex.
Evaluation is done by repeatedly finding a reducible expression (called a redex) and reducing it by a function evaluation until there are no more redexes. Example 1: The lambda expression (λx. x)y in its entirety is a redex that reduces to y.
We can just do reductions on your answer to get a pithier one.
First some warm-ups. Obviously IF x ==> x
, and as TRUE TRUE FALSE ==> TRUE
and FALSE TRUE FALSE ==> FALSE
if x
is a boolean then x TRUE FALSE ==> x
.
Now we reduce
\x y . (IF x) ( (IF y) TRUE FALSE ) FALSE
\x y . x ( y TRUE FALSE ) FALSE -- using IF x ==> x
\x y . x ( y ) FALSE -- using y TRUE FALSE ==> y
\x y . x y FALSE -- clean up
and this expression still works with truth tables
AND TRUE TRUE = TRUE TRUE FALSE = TRUE
AND FALSE TRUE = FALSE TRUE FALSE = FALSE
AND TRUE FALSE = TRUE FALSE FALSE = FALSE
AND FALSE FALSE = FALSE FALSE FALSE = FALSE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With