[Haskell-cafe] Problems with function recursion in Haskell

Francesco Ariis fa-ml at ariis.it
Mon May 9 13:45:21 UTC 2016


On Mon, May 09, 2016 at 10:30:44AM -0300, Henson wrote:
> Hi,
> 
> I'm new in Haskell and I need help in recursion.
> That function below is returning "*** Exception: Prelude.head: empty list"
> and
> I need resolve that:
> execNTimes 0 [] = return()
> execNTimes n xs = if n<=0  || null xs
>     then return()
>     else do
>         si <- getLine
>         let s = words si
>             l = read (s !! 1) :: Int
>             r = read (s !! 2) :: Int
>         if head s=="Q"
>             then do
>                 let z = slice l r xs
>                     m = foldl lcm 1 z
>                 print (m `mod` (toInteger 1000000007))
>             else do
>                 let s1 = update l r xs
>                 execNTimes (n-1) s1
>         execNTimes (n-1) xs
> 
>  Anybody can me help?

Hello Josenildo,

    the error indeed tells us you are passing an empty list to the function
`head`.

    si <- getLine
        let s = words si
    -- other lines
    if head s == "Q"

so I suppose you are giving an empty string as input to getLine (I cannot
be sure as the code doesn't compile, missing `slice` and `update`).


More information about the Haskell-Cafe mailing list