Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use functions in Haskell parameters?

I have seen a few examples of Haskell code that use functions in parameters, but I can never get it to work for me.

example:

    -- Compute the nth number of the Fibonacci Sequence
    fib 0 = 1
    fib 1 = 1
    fib (n + 2) = fib (n + 1) + fib n

When I try this, it I get this error:

    Parse error in pattern: n + 2

Is this just a bad example? Or do I have to do something special to make this work?

like image 433
mark Avatar asked Nov 29 '22 17:11

mark


1 Answers

What you have seen is a special type of pattern matching called "n+k pattern", which was removed from Haskell 2010. See What are "n+k patterns" and why are they banned from Haskell 2010? and http://hackage.haskell.org/trac/haskell-prime/wiki/RemoveNPlusK

like image 184
Landei Avatar answered Dec 05 '22 02:12

Landei