user6943017
user6943017

Reputation:

Lambda calculus isEven function in Haskell

So I'm learning about lambda calculus in Haskell, and I'm trying to implement an isEven function that returns true if it's even and false otherwise. I know that 0 is even and then 1 is odd and then each alternating number is the alternative of the one before i.e. if one is odd then 2 is even then 3 is odd. Could I have the isEven function check if the input is 0 and if it's not then somehow check if it's successor is even or odd?

Upvotes: 1

Views: 2285

Answers (1)

moonGoose
moonGoose

Reputation: 1510

I assume that by "lambda calculus" you mean we're working with some Church-encoded numerals + booleans, and the "Haskell" part is mostly incidental to your question.

isEven = \n -> n flip True
flip = \x y z -> x z y
True = \x y -> x
False = \x y -> y

This operates slightly differently to how you express it.

Recall the Church numeral n means n iterated function applications. flip repeated an even # of times is id, thus n flip == id for even n, n flip == flip for odd n. Also, flip True == False and flip False == True. Thus the construction correctly encodes parity.

Upvotes: 3

Related Questions