Analysis error in template: n + 1

Trying to load a function into a file:

Prelude> :load "prova.hs" prova.hs:37:9: Parse error in pattern: n + 1 [1 of 1] Compiling Main ( prova.hs, interpreted ) Failed, modules loaded: none. Prelude> 

This should create a list that contains n times the repeated value of x:

 ripeti :: Int -> a -> [a] ripeti 0 x = [] ripeti (n+1) x = x:(ripeti nx) 

What is wrong with him?

+8
pattern-matching haskell parse-error
source share
1 answer

Your code uses what are called "n + k patterns" which are not supported in Haskell 2010 (they were supported in Haskell 98).

You can read a little about it on this SO question .

To make your code work, you can write

 ripeti :: Int -> a -> [a] ripeti 0 x = [] ripeti nx = x : ripeti (n-1) x 

Note that this will not stop if you specify a negative value for n , so I would rather define

 ripeti :: Int -> a -> [a] ripeti nx | n <= 0 = [] | otherwise = x : ripeti (n-1) x 
+10
source share

All Articles