views:

349

answers:

7

This question is of course inspired by Monads in Haskell.

+1  A: 

recursion. Difficult to wrap your head around it at times

Toad
Indeed. See this answer here: http://stackoverflow.com/questions/1438558/what-techniqie-in-functional-programming-is-difficult-to-learn-but-useful-afterwa/1438569#1438569
skaffman
laugh(); void laugh() { print("ha"); laugh(); } Although this would result in something akin the name of this site eventually ;^)
Toad
Not if you're language supports tail recursion. ;)
mipadi
@reinier: Looks like tail recursion to me. With many compilers, you've only created an infinite loop!
Mike Daniels
even in c? That was the syntax I was going for
Toad
+6  A: 

I would say First-class functions.

In computer science, a programming language is said to support first-class functions (or function literals) if it treats functions as first-class objects. Specifically, this means that the language supports constructing new functions during the execution of a program, storing them in data structures, passing them as arguments to other functions, and returning them as the values of other functions. This concept doesn't cover any means external to the language and program (metaprogramming), such as invoking a compiler or an eval function to create a new function.

Nick D
Is it usefull ? o_O
Clement Herreman
not just useful but powerful. For example you can simulate OOP even if the language doesn't support it. See for example: http://javascript.crockford.com/prototypal.html
Nick D
Not really difficult, in my opinion...Monads are surely an order of magnitude harder...
emaster70
Doesn't seem like such a foreign concept to me either. Perhaps adding the concept of http://en.wikipedia.org/wiki/Currying to this answer would spice it up a little bit.
Rehno Lindeque
+8  A: 

wrapping my head around continuation passing style has helped my javascript coding a lot

Martin DeMello
+2  A: 

I would say that Structural typing in OCaml is particularly rewarding.

Łukasz Lew
+1  A: 

The concept of higher-order functions, lambda functions and the power of generic algorithms that are easy to combine were very beneficial for me. I'm always excited when I see what I can do with a fold in haskell. Likewise my programming in C# has changed a lot (to the better, I hope) since I got into functional programming (haskell specifically).

Christian
+5  A: 

Do you want to measure the usefulness in connection with functional-programming itself or programming in general?

In general, the positive experience of functional programming doesn't result from particular techniques but from the way it changes your thinking -

  • Holding immutable data
  • Formulating declaratively (recursion, pattern-matching)
  • Treating functions as data

So I'd say that functional programming is the answer to your question itself.

But to give a more specific answer too, I'd vote for functional abstraction mechanisms like

  • monads
  • arrows
  • continuation-passing-style
  • zippers
  • higher-order-functions
  • generics + typeclasses.

As already said, they are very abstract things on the first view, but once you have understood them, they are extremely cool and valueable techniques to write concise, error-safe and last but not least highly reusable code.

Compare the following (Pseudocode):

// Concrete
def sumList(Data : List[Int]) = ...

// Generic
def sumGeneric[C : Collection[T], T : Num](Data : C) = ...

The latter might be somewhat unintuitive compared with the first definition, but it allows you to work with any collection and numeric type in general!

All in all, many modern (mainstream) languages have discovered such benefits and introduced very functional features like lambda functios or Linq. Having understood these techniques will also improve writing code in this languages.

Dario
+3  A: 

One from the "advanced" department: Programming with phantom types (sometimes also called indexed types). It's admittedly not a "standard" technique in functional programming but not entirely esoteric either, and it's something to keep your brain busy for awhile (you asked for something difficult, right? ;)).

In a nutshell, it is about parameterizing types to encode and statically enforce certain properties at compile time. One of the standard examples is the vector addition function that statically ensures that given two vectors of length N and M will return a vector of length N+M or otherwise you get a compile-time error. Yes, there are more interesting applications.

These techniques are not quite as useful in C++ as they are in a proper functional programming language, but so far I've managed to sneak some of this stuff in all of my recent projects at work to a varying degree, most recently in a C++ EDSL context where it worked out really well. You don't necessarily have to encode fancy stuff, learning this helped me catching the situations where a few type tags can reduce the verbosity of an EDSL or allowed a cleaner syntax, for example.

Admittedly, the usefulness is somewhat restricted by language support and what you're trying to achieve.

Some starters:

Generic and Indexed Type (slides with some brief applications overview)

Fun with Phantom Types

The Kennedy and Russo paper mentioned in the slides is Generalized Algebraic Data Types and Object Oriented Programming and puts some of this stuff into the context of C#/Java.

Chapter 3 in Dave Abraham's book C++ Template Metaprogramming is available online as sample chapter and uses these techniques in C++ for dimensional analysis.

A practical FP project using phantom types is HaskellDB.

Rüdiger Hanke