Wednesday, February 23, 2011

Looking for a better way to use guards in lambda expressions

Took a swing at using guards instead of if-then to do flow control in anonymous recursion. After some digging, the cleanest solution I could come up with invokes a single case, expressionless statement against any arbitrary argument.

Surely there's a better way to do this.

-- the Y-combinator
y f = f (y f)

-- factorial in lambda
fac :: Int -> Int
fac = y( \f n -> case () of
_ | n == 0 -> 1
| otherwise -> n*f(n-1) )

No comments:

Post a Comment