0

I've just started learning Functional Programming (Scheme). But I still have problems thinking "functionally".

Something like:

func1(int a){
   if(a==100)
      a=0;
   return func2(a);
}

There's a state change there, so that's imperative programming.

If I take the "if" part and throw it into another function, does that make it funcional?

func1(int a){
   return func2(func3(a));
}

Is this what all's about?

Thanks!

eric17859
  • 418
  • 1
  • 5
  • 17

1 Answers1

4

Not really. First, there's several different definitions of what functional programming means, and it changes by community. Haskellers have a bit different idea of it than Schemers, usually.

Strictly speaking, functional programming uses functions as primitives, so that they can be put into variables and passed around as arguments, without them being evaluated in the process.

Haskellers will usually tack on the purity requirement. Functional purity is the idea that functions mustn't have side effects (including changes to state); i.e. each call to the function with the same arguments must return the same value.

Your second function fails on the first condition, the necessary one. You are not using functions as first-class citizens.

If you write it like this,

func1(int a) {
  return (
    if (a==100)
      then func(0);
      else func2(a);
    )
}

This is now pure, but it is neither particularly functional nor particularly imperative.

I can't really translate your example into something specifically functional, since there's too little context. The usual "hello world" for functional code is this:

square(x) = x * x
twice(f, x) = f(f(x))
twice(square, 4)
  => 256

Here we define a function square that multiplies the number by itself. We define another function twice that takes a function and an argument, and applies the function to the argument twice. Then we give the function twice the arguments square and 4. Note that this isn't twice(square(4)) - the function square is not evaluated until within the definition of the function twice.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • 1
    "Your second function fails on the first condition" first condition = use functions as primitives? I'm sorry, I don't understand why my second code is not functional and yours is. – eric17859 Mar 03 '12 at 02:46
  • 1
    I did say there is nothing particularly functional about my example. But it is pure, as it doesn't mess with state. The example is just one `if`, there isn't enough in it to make it either functional or not. It's kind of like giving a biologist a couple of nucleotides and asking him if it's a tree or a bush. – Amadan Mar 03 '12 at 02:50
  • 3
    Schemers avoid unnecessary side-effects (like the one in the OP's example), but we do use them occasionally. One finds balance through experience; while "retraining", it's best to err towards purity. – Ryan Culpepper Mar 03 '12 at 03:10
  • @RyanCulpepper: I agree; I just meant that most Haskellers include purity into the definition of FP (and Scheme and the rest of the hybrids at best get some "weak functional" label). – Amadan Mar 03 '12 at 03:39