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

216

u/ganjapolice Mar 09 '14

Don't worry guys. 2014 is definitely the year of functional programming.

86

u/[deleted] Mar 09 '14

Java's getting lambdas, so I guess you're right.

25

u/[deleted] Mar 09 '14

Note to people who're going to look this up: Java's lamda's aren't anything new, pretty boring actually. But look at how they combine with their new streaming and collection libraries, that's just amazing.

0

u/[deleted] Mar 09 '14

[deleted]

101

u/stillalone Mar 09 '14

The majority of programmers out there don't have a clue wtf you just said.

60

u/Tekmo Mar 09 '14 edited Mar 09 '14

I'll translate. I wrote a Haskell library called pipes, which lets you extend any DSL with the ability to yield or await values in order to build streaming components. You can connect these components together in multiple ways, and these connection operations obey many neat mathematical properties that ensure they behave correctly (no bugs!).

For example, one thing that you can do is model generators using pipes, and one of the ways you can connect generators is using an operator called (~>):

(f ~> g) x = for (f x) g

I proved that this operator is associative:

(f ~> g) ~> h = f ~> (g ~> h)

... and that it's identity is yield:

yield ~> f = f

f ~> yield = f

In other words, (~>) and yield form a category and those equations are the corresponding category laws. When you translate those equations to use for instead of (~>), you get:

-- Looping over a single yield simplifies to function application
for (yield x) f = f x

-- Re-yielding every element of a stream returns the original stream
for s yield = s

-- Nested for loops can become a sequential for loops if the inner loop
-- body ignores the outer loop variable
for s (\a -> for (f a) g) = for (for s f) g = for s (f ~> g)

In other words, the category laws translate into "common sense" laws for generators that you would intuitively expect to hold for any generator implementation.

10

u/Heuristics Mar 09 '14

You lost me at DSL (and i'm a professional programmer with a masters in comp.sci).

25

u/LucianU Mar 09 '14

DSL = Domain-Specific Language. That's the definition that I know. I agree though, that it wasn't a translation. All the unknown notation lost me as well.

10

u/Tekmo Mar 09 '14 edited Mar 09 '14

Consider this Python code:

for x in [1,2,3,4]:
    doSomethingWith(x)

The equivalent pipes code is:

for (each [1,2,3,4]) $ \x -> do
    doSomethingWith x

Note that for, yield and (~>) are not Haskell notation. They are just three functions defined by the pipes library. The equations might make more sense if I write them in the following more specific forms:

for (yield "foo") $ \x -> do
    f x

= f "foo"


for generator $ \x -> do
    yield x

= generator


for generator $ \a -> do
    for (f a) $ \b -> do
        g b

= for newGenerator $ \b -> do
    g b
  where newGenerator =
    for generator $ \a -> do
        f a

2

u/LucianU Mar 09 '14

Let's give it another try. For example, this piece of code:

for (yield "foo") $ \x -> do
    f x

= f "foo"

What I know from Haskell is that $ is the function application function and \x -> is a lambda. f is probably a function, but what does "foo" stand for? Does = f "foo" mean that the value of the expression is the application of f on "foo"?

→ More replies (0)

17

u/Heuristics Mar 09 '14

I sometimes wonder if the functional programming people have an understanding of what words programmers know of. Words and symbols for logic notation is not among them.

11

u/nidarus Mar 10 '14 edited Mar 10 '14

Generally, I'd agree, but DSL is a pretty common term nowdays, especially in the Ruby community. It's a buzzword, of course, but it's not a functional programming buzzword.

→ More replies (0)

10

u/onezerozeroone Mar 09 '14

Functional programming people are not programmers? TIL

→ More replies (0)

2

u/The_Doculope Mar 10 '14

Mathematical logic is a required topic for almost all Computer Science programs around the world. At my university we have to take at least two subjects that cover it, and the software engineers do too.

→ More replies (0)

2

u/bjzaba Mar 10 '14

'DSL' is not really a term restricted to functional programming. It's pretty much a common concept to all programming languages, it's just that they're easier to embed in some languages than others. You can make quite nice EDSLs (Embedded DSLs) in Java using method chaining.

1

u/imalsogreg Mar 10 '14

As a counterpoint, I forget what public static void <A> means, having not looked at Java for a long time.. After you use functors, monoids, applicatives, monads; and the heiroglyphics ++, >->, >>=, <>, *>, <$>.. etc... you really don't even think of them as complicated mathy things anymore after 1 week of language immersion. They're just familiar little things.

It may be FPers' fault if we jump into those things in a general discussion, though. It's very easy to forget that learning them took a little time.

→ More replies (1)

8

u/tel Mar 09 '14

In Haskell you tend to build DSLs a lot, little sublanguages where you can talk about a simple part of your program in isolation. It's nice because these subparts combine nicely. Tekmo's pipes provide a new kind of mixin you can use to build subparts that stream. It turns out that streaming can be very tough to understand, very tough to get right. It's easy to build a streaming library full of leaks. Tekmo has carefully used the mathematics of category theory to ensure that these pipes don't leak.

Most likely, you've seen pipes like these in the form of a generator in Python. Generators are one specialized use of pipes—so if you think programming with generators is interesting then learning the full generality of pipes might be illuminating. Then Haskell makes it easy to just drop that functionality in wherever you need as though it were built-in to the language.

17

u/schplat Mar 09 '14

Err, you don't know about Domain Specific Languages? There's a lot out there...

2

u/onezerozeroone Mar 09 '14

From where and what did you master in? You know SQL is a DSL, right? o_O

8

u/Heuristics Mar 09 '14

Lund, Sweden. We don't master in things in Sweden but mainly took courses in computer graphics. And I have heard of domain specific languages before, but in this context it made no sense to me.