Reusing lambda function in Haskell

I have to take this code:

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

And rewrite it without where (or let).

They mean to write it using the lambda function (\ x → ...)

I am trying to reuse the Lambda function on Haskell. Any ideas?

+7
source share
6 answers

As hints for the brave, you can rewrite non-recursive let with lambda as follows:

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

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

+12
source

To reuse something, you can make it something with an argument.

+8
source

I think intention is what the Bravites hint at.
The smartypants workaround following the letter is mandatory g with case ;)

+2
source

To expand the hammar and bravit prompts, your solution will require not only one lambda, but two - one of them will be very similar to g , and the other will be very similar to the second half of f

+2
source

Using the lambda calculus g is (\x -> 2*x^2 + 10*x + 1)

So you need to replace g with fxyz = x^3 - g (x + g (y - gz) + g (z^2))

 $> echo "fxyz = x^3 - g (x + g (y - gz) + g (z^2))" | sed -r -e 's/g/(\\x -> 2*x^2 + 10*x + 1)/g' fxyz = x^3 - (\x -> 2*x^2 + 10*x + 1) (x + (\x -> 2*x^2 + 10*x + 1) (y - (\x -> 2*x^2 + 10*x + 1) z) + (\x -> 2*x^2 + 10*x + 1) (z^2)) 

I'm just joking, sorry.

+1
source

This question seems curious and interesting to me. So, I'm trying to understand what lambda calculus is, find the answer and want to show its OP (all the hints have already been shown in fact, a spoiler alert ).

First, try overriding f :

 Îģ> let f = (\gxyz -> x^3 - g(x + g(y - gz) + g(z^2))) f :: (Integer -> Integer) -> Integer -> Integer -> Integer -> Integer 

So, we have a function that receives a function and 3 numbers and returns a response. Using curring, we can add the definition of g right here, for example f_new = fg :

 Îģ> let f = (\gxyz -> x^3 - g(x + g(y - gz) + g(z^2))) (\x -> 2*x^2 + 10*x + 1) f :: Integer -> Integer -> Integer -> Integer 

We are done. Let it check:

 Îģ> f 0 0 0 -13 

The answer is correct.

UPD

In these examples, let is just a way of declaring a function in the interpreter, so the final answer is:

 f :: Num a => a -> a -> a -> a f = (\gxyz -> x^3 - g(x + g(y - gz) + g(z^2))) (\x -> 2*x^2 + 10*x + 1) 
-one
source

All Articles