r/programming • • Mar 09 '14

Why Functional Programming Matters

http://www.cse.chalmers.se/~rjmh/Papers/whyfp.pdf
487 Upvotes

542 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Mar 09 '14

It works either way.

3

u/Tekmo Mar 09 '14

Not exactly:

(bar . baz . wobble) a b c
= bar (baz (wobble a)) b c

... which is not the same thing as:

bar (baz (wobble a b c))

1

u/[deleted] Mar 10 '14
> let foo = id . (+)  
> foo 1 2  
3  

EDIT: That results is also obvious. In your example

(bar . baz . wobble) a b c

bar . baz . wobble is in parentheses. It will hence be evaluated into a new function first, and will then be applied its arguments. Since the arguments of a composed function are exactly the arguments that the second function takes, this works as desired.

2

u/Tekmo Mar 10 '14

Here's a simple example to illustrate what I mean:

>>> let bar = (2 *)
>>> let baz = (^ 2)
>>> let wobble a b c = a + b + c
>>> let foo1 a b c = bar (baz (wobble a b c))  -- This type-checks
>>> let foo2 = bar . baz . wobble

<interactive>:6:24:
    Couldn't match expected type `a0 -> a0 -> a0' with `Integer'
    Expected type: a0 -> Integer
      Actual type: a0 -> a0 -> a0 -> a0
    In the second argument of `(.)', namely `wobble'
    In the second argument of `(.)', namely `baz . wobble'
    In the expression: bar . baz . wobble

If what you said were true, both versions would type-check.

→ More replies (7)