/ HackerRank Functional Challenges

HR F#: Lambda Calculus - Evaluating Expressions #2

The first λ-calculus evaluating expression problem was very easy. The second one is similar:

Compute the value of (λx.x+1)((λy.y+2)3).

Just to make a bit more fun from it, let's solve it by writing the code in F#:

(fun x -> x + 1)((fun y -> y + 2)3)
|> printfn "%A"

And in JavaScript:

console.log((x => x + 1)((y => y + 2)(3)));

And in GO:

fmt.Println((func(x int) int { return x + 1; })((func(y int) int { return y + 2; })(3)))

Ok, In C#:


More extreme, PHP:

print (function($x){ return $x+1; })((function($y){ return $y+2; })(3));

A lot of languages support the anonymous functions, the building block of lambda calculus, althougn not all of them flexible enough for λx.(x x).

Alex Netkachov

Alex Netkachov

Alex likes functional programming and algorithms. Apart from programming, his favourites are walking with his family in the parks and national trails and reading about universe and history.

Read More