Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusing a Lambda function in Haskell

I'm supposed to take this code:

f x y z = x^3 - g (x + g (y - g z) + g (z^2))
 where g x = 2*x^2 + 10*x + 1

And rewrite it without where (or let).

They mean to write it with a Lambda function (\x ->...)

I'm trying to reuse a Lambda function on Haskell. Any ideas?

like image 281
Asaf Avatar asked Nov 19 '11 16:11

Asaf


4 Answers

As bravit hints at, you can rewrite a non-recursive let using a lambda in the following way:

let x = A in B     ==>     (\x -> B) A 

where x is a variable and A and B are expressions.

like image 77
hammar Avatar answered Oct 19 '22 09:10

hammar


To reuse something you can make it an argument to something.

like image 45
bravit Avatar answered Oct 19 '22 09:10

bravit


I think the intention is what bravit hints at.
The smartypants follow-the-letters-of-the-law workaround is binding g with a case ;)

like image 2
Daniel Fischer Avatar answered Oct 19 '22 10:10

Daniel Fischer


To expand on hammar's and bravit's hints, your solution is going to require not just one lambda, but two - one of which will look a great deal like g, and the other of which will look a great deal like the second half of f

like image 2
Nate Avatar answered Oct 19 '22 10:10

Nate