<div dir="ltr">Hi,<div><br></div><div>Rather than using indexing, or using head, I recommend using pattern matching. These functions are not total, i.e. they fail to provide a valid result for some specific inputs, like head on empty list.</div><div><br></div><div>The following is similar to replicateM from Control.Monad, you can either modify it to suit your need, or use replicateM combined with map to turn a list of Strings to a list of IO () as required by replicateM.</div><div><br></div><div><font face="monospace, monospace"> execNTimes :: Int -> [IO ()] -> IO ()</font></div><div><font face="monospace, monospace"> execNTimes 0 [] = return ()</font></div><div><font face="monospace, monospace"> execNTimes n (x : xs)</font></div><div><font face="monospace, monospace"> | n <= 0 = return ()</font></div><div><font face="monospace, monospace"> | otherwise = x >> execNTimes (n - 1) xs</font><br><br></div><div>Where, the following are equivalent:</div><div><br></div><div><font face="monospace, monospace"> a >> b == do a</font></div><div><font face="monospace, monospace"> b</font></div><div></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div style="font-size:12.8px"><span style="font-size:12.8px">Regards,</span></div><div style="font-size:12.8px"><span style="font-size:12.8px"> Sumit</span></div></div></div></div></div></div></div></div></div></div></div>
<br><div class="gmail_quote">On 9 May 2016 at 19:00, Henson <span dir="ltr"><<a href="mailto:jfsihenson@gmail.com" target="_blank">jfsihenson@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I'm new in Haskell and I need help in recursion.<br>
That function below is returning "*** Exception: Prelude.head: empty list" and<br>
I need resolve that:<br>
execNTimes 0 [] = return()<br>
execNTimes n xs = if n<=0 || null xs<br>
then return()<br>
else do<br>
si <- getLine<br>
let s = words si<br>
l = read (s !! 1) :: Int<br>
r = read (s !! 2) :: Int<br>
if head s=="Q"<br>
then do<br>
let z = slice l r xs<br>
m = foldl lcm 1 z<br>
print (m `mod` (toInteger 1000000007))<br>
else do<br>
let s1 = update l r xs<br>
execNTimes (n-1) s1<br>
execNTimes (n-1) xs<br>
<br>
Anybody can me help?<br>
<br>
Thank you,<br>
Josenildo Silva<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br></div>