Skip to content

factorial.hs:3:12: Parse error in pattern: n + 1

Environment of work

Ubuntu 11.10

$ uname -a
Linux fmgo-HP 3.0.0-12-generic #20-Ubuntu SMP Fri Oct 7 14:50:42 UTC 2011 i686 i686 i386 GNU/Linux

$ ghci
GHCi, version 7.0.3

$gcc -v
....
gcc versión 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3)

The problem


I composed the function

factorial :: Integer -> Integer
factorial 0 = 1
factorial (n+1) = (n+1) * factorial n

in the file "factorial.hs". So I make {{ $ ghci factorial }}} and then ghci produces the message

GHCi, version 7.0.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main             ( factorial.hs, interpreted )

factorial.hs:3:12: Parse error in pattern: n + 1
Failed, modules loaded: none.
Prelude> 

therefore I changed the content of factorial.hs to

fact :: Integer -> Integer
fact 0 = 1
fact n = n * fact (n-1)

now I make

Prelude> :l factorial.hs

obtaining the dialog

[1 of 1] Compiling Main             ( factorial.hs, interpreted )
Ok, modules loaded: Main.
*Main> 

but "the problem" is that the simple computation

*Main> factorial (-1)

produces

*** Exception: stack overflow

In GHCi version 6.10.4 there is no bug ... or problem

Edited by Simon Peyton Jones
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information