Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What are the challenges that an experienced programmer with the usual C++/UNIX background would face when trying to learn functional programming? The main motive for learning is to gain a fresh perspective, perhaps that'd help write better code. This is debatable though.

Should Haskell be the introductory language or Prolog or Lisp or F#?

share|improve this question
F# is great on Windows. – Jon Harrop Dec 29 '10 at 19:02

6 Answers

up vote 6 down vote accepted

When I started digging deep into Haskell (coming from a mostly Basic/C background), I already had a firm grasp of closures from thinking about them for months and months, and using them in JavaScript. Whether or not this is the biggest hurdle of learning functional programming, I'm not sure, but I'd imagine they're easier to understand when state is taken out of the equation.

I suppose the first major hurdle for me was the syntax. When I first took a look at the xmonad source code, it looked quite humorous:

-- | Set focus explicitly to window 'w' if it is managed by us, or root.
-- This happens if X notices we've moved the mouse (and perhaps moved
-- the mouse to a new screen).
focus :: Window -> X ()
focus w = local (\c -> c { mouseFocused = True }) $ withWindowSet $ \s -> do
    let stag = W.tag . W.workspace
        curr = stag $ W.current s
    mnew <- maybe (return Nothing) (fmap (fmap stag) . uncurry pointScreen)
            =<< asks mousePosition
    root <- asks theRoot
    case () of
        _ | W.member w s && W.peek s /= Just w -> windows (W.focusWindow w)
          | Just new <- mnew, w == root && curr /= new
                                               -> windows (W.view new)
          | otherwise                          -> return ()

The arrows pointing here and there, and things like maybe (return Nothing), gave me the impression that they make it up as they go, and that the syntax has zillions of operators and keywords (custom operators were a foreign concept to me).

The next hurdle was Haskell's type system. The nice thing about this one is, if you don't quite get it, the compiler will be quick to point it out :-)

Then, monads. I suppose my breakthroughs in understanding them were:

  • do {x <- m; k x} is equivalent to m >>= k
  • Monads are merely a design pattern. They are not given special treatment by the language (other than do notation), and one does not have to understand category theory to use them.
  • Monads, functors, etc. are all, in an abstract sense, "actions" yielding a "thing".
    • With a Functor, you can apply a function to that thing, but you don't get to unwrap it (e.g. (*5) <$> Just 2 = Just 10).
    • An applicative functor lets you take two actions and apply one's result to the other's, yielding a new action (e.g. Just (*5) <*> Just 2 = Just 10). This introduces a notion of sequence, but does not allow the result of one action to influence what the next will be.
    • Monad, despite its simple definition (bind and return), can do everything an applicative functor can do and more. Namely, monads allow the result of one action to determine the next (e.g. getLine >>= \x -> if null x then return () else print (read x * 2)).

As for an introductory language for functional programming, I can certainly vouch for Haskell. Thanks to learning Haskell, I feel like I've gotten better at breaking up programs logically, even in languages like C and assembly.

PS: I didn't say anything about the transition from imperative to functional, and I probably should (to really answer the question), but I need to go to sleep.

share|improve this answer
Thanks Joey. Any recommended books? – Fanatic23 Dec 22 '10 at 16:47
1  
@Fanatic23: I learned Haskell mostly from web resources, IRC, and spending way too much time using it. However, I've seen Learn You a Haskell for Great Good! and Real World Haskell mentioned again and again as good books for learning Haskell, and they're both available on the web for free. – Joey Adams Dec 22 '10 at 22:43
I can vouch for "Real World Haskell" as well. It's a pretty good introduction to the language. – davidk01 Dec 22 '10 at 23:56
I much preferred Learn you a Haskell. – Jon Harrop Dec 29 '10 at 19:01
+1 I like the easy to grasp characterization of Monads/Functors/Applicative. (Arrows as a even more powerful generalization would also fit in) – Dario Jan 18 '11 at 12:03

Haskell seems awesome, but I have never really been able to learn it - there are a lot of hurdles to get over before you can really do anything (the type system is ultra-strict).

I learned all of my functional programming using lisp, and other dynamic languages (plus C++), so you certainly don't need to learn haskell to learn new things.

That said, I think there is a parallel with the phenomenon that programmers who learn iteration first usually find iteration hard to understand, and vice versa.

Accordingly, my practical advice to you is to start with haskell, and if you find you can learn what you need to there, explore other functional programming languages.

Good luck!

share|improve this answer

When you learn a different paradigm you learn a different way to approach the problem. The challenge there is retraining yourself to not immediately go down the path of a procedural or OO solution. For the most part I think most of your challenges will be of that nature. The concepts and applications are pretty straightforward, getting used to it was the hard part for me.

I'm a big fan of scheme which is similar to lisp.

share|improve this answer

The best experience I had as a UNIX developer learning lisp was learning how to write emacs extensions using emacs lisp (elisp). The ability to have your text editor do anything you want is a powerful learning incentive, and you don't have to write much lisp code to get emacs to do something useful.

share|improve this answer

I would say take a look at lisp if you want, but Haskell will be more useful for working in. Plus Lazyness rocks.

share|improve this answer
+1 Laziness rocks – Fanatic23 Dec 22 '10 at 16:46

I guess wrapping your head around Monads and all the other categorical nonsense is the biggest challenge.

share|improve this answer
1  
This would have been a good answer without the words "categorical nonsense." – Larry Coleman Dec 22 '10 at 13:44
3  
@Larry Coleman: "categorical nonsense" is actually a technical term used by mathematicians. It's not nonsense in the same way as regular nonsense. – davidk01 Dec 22 '10 at 23:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.