The Haskell function
foo = zipWith ($) . repeat
does exactly the same as
map
but I cannot see why :-( Who can give an explanation? Thx a lot!
As we already know that Haskell is a functional programming language, Haskell provides us many inbuilt functions which help us to perform the basic operations we need. Also, we can define our own functions as well which will perform the desired operations for us.
How to create a function in Haskell? 1 Function declaration in Haskell: first we will see the function declaration in Haskell, which will define what type... 2 Function definition: Once we defined the function then we have to give its body means the definition of the function,... More ...
We sometimes have to write a function that is going to be used only once, throughout the entire lifespan of an application. To deal with this kind of situations, Haskell developers use another anonymous block known as lambda expression or lambda function. A function without having a definition is called a lambda function.
But this is not so in Haskell. As a simple example, consider const1, the constant 1 function, defined by: const1 x = 1 The value of const1 botin Haskell is 1. Operationally speaking, since const1does not "need" the value of its argument, it never attempts to evaluate it, and thus never gets caught in a nonterminating computation.
OK, so we have
foo = zipWith ($) . repeat
which is the same as
foo f = zipWith ($) (repeat f)
The repeat f
generates an infinite list of copies of f
. Then zipWith
uses the ($)
operator to apply each element of the [infinite copies of f
] list to each element of the incoming list. Which is what map
does.
Yes?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With