The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

326925 (10) [Avatar] Offline
#1
ifEven f x =if even x
then f x
else x

genIfXeven x = (\f->IfEven f x)
Can you give me an example of how to use the genIfXeven function.
441303 (1) [Avatar] Offline
#2
ifEven f x =if even x
then f x
else x

genIfXeven x = (\f->IfEven f x)


All this is doing is returning a function with the captured value, x. So if you pass in 4, then 4 will be captured within the closure:

myClosure = genIfXEven 4 == (\f -> ifEven f 4)


Now if we have a function such as:

inc n = n + 1


You can pass it to the closure such as:

myClosure inc => ifEven inc 4


if the number is even then it will increment, if not it will return the passed in value

-- If the value is EVEN:
ifEven inc 4 => 5


-- If the value is NOT EVEN:
ifEven inc 4 => 4


326925 (10) [Avatar] Offline
#3
Thank you very much, I understand it now. I was getting confused with how this could be done in one step.