<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, May 9, 2016 at 6:30 AM, 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:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">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></blockquote><div><br></div><div>Your code is making the assumption that (words si) is non-empty. When that's not true, such as when the input from the console is empty, your program will crash at the use of `head`, since it throws an error when applied to an empty list. That's what you are seeing. Incidentally, by using `l` and `r` later on, you're also assuming that the input contains at least 2 more words after that first one. Violating that will also crash your program. Finally, your use of `read` means that the program will crash if the second two inputs are not numbers. All three of these functions should be used with great caution, or not at all, since they threaten to crash your program!</div><div><br></div><div>Here's a very concise way to write this, using pattern guards:</div><div><br></div><div> case words si of</div><div> [ a, ls, rs ]</div><div> , Right l <- readEither ls :: Either String Int<br></div><div> , Right r <- readEither rs :: Either String Int</div><div> -> if a == "Q"</div><div> then do</div><div> let x = slice l r xs</div><div> let m = foldl lcm 1 z</div><div> print (m `mod` (toInteger 1000000007))</div><div> else do</div><div> let s1 = update l r xs</div><div> execNTimes (n-1) s1</div><div> _ -> putStrLn "Invalid input."</div></div></div></div>