Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elm: understanding foldp and mouse-clicks

I'm currently learning Elm. relatively new to functional programming. i'm trying to understand this example from http://elm-lang.org/learn/Using-Signals.elm on counting mouse-clicks. they provide the following code:

clickCount =
    foldp (\click count -> count + 1) 0 Mouse.clicks 

They explain that foldp takes three arguments: a counter-incrementer, which we defined as an anonymous function with two inputs, a starting state 0, and the Mouse.clicks signal.

I do not understanding why we need the variable click in our anonymous function. Why can't we just have \count -> count + 1? Is the extra input getting bound to one of our inputs into foldp?

thanks!

like image 278
Ian Taylor Avatar asked May 14 '15 23:05

Ian Taylor


1 Answers

You need it because foldp expects a function with two inputs. In this case, the first input is just ignored by your lambda, but the foldp implementation still puts something in there. Mouse.clicks always puts a sort of do-nothing value called Unit in there.

Some signals have a value associated with them, like Mouse.position, for example. If you wanted to do something like measure how far the mouse has moved, you would need to use that parameter.

like image 151
Karl Bielefeldt Avatar answered Oct 06 '22 01:10

Karl Bielefeldt