Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who can explain this Haskell function?

Tags:

haskell

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!

like image 761
s o Avatar asked Jul 29 '15 13:07

s o


People also ask

What is Haskell programming language?

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?

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 ...

What is a lambda expression in Haskell?

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.

Why doesn't Haskell evaluate the value of its arguments?

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.


1 Answers

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?

like image 65
MathematicalOrchid Avatar answered Oct 19 '22 01:10

MathematicalOrchid