Asbestos Supply

2011-04-29 Next up: F#

The next language I'll be learning will be F#. I just started and have already written my first stack overflow! But it taught me a lesson about the order of precedents in F#.

I was writing my first real function, one that wasn't pre-written in the tutorials I was using (at least not that I got up to yet!): Fibonacci.  I started with

let rec fib x =
  if x = 0 then
    0
  elif x = 1 then
    1
  else
    (fib x-1)+(fib x-2)

printfn "fib 10 is %A" (fib 10)

and that crashed the interpreter!  can you spot the problem?

line 7 does this:

  1. compute fib x
  2. subtract 1 from that
  3. compute fib x
  4. subtract 2 from that
  5. add the two together

I should have written

fib(x-1) + fib(x-2)

I'm learning here!

The next step, using match patterns instead of if's.  And Matthew Podwysocki's got a great write up of that over here!  Too bad he solved my Fibonacci problem. Now I'll have to come up with another problem to really learn the language. Any suggestions?