Hostage Rescue - Part 1 - Training | Haskell template code bug

Hi !

read input_line :: String

throws exception: no parse

Short answer: throw away the read and directly use the input_line (which is already a string).

Long answer:

The same problem existed in the Onboarding puzzle template, but I can’t retrieve my post on this object however. The core of the problem is that the textual representation of a string in Haskell is a sequence of characters between double quotes and the read function expects them to be there. More generally, the read . show function should always behave like the id function. Since show "blabla" outputs "blabla" and not simply blabla, the read function valid input is "blabla", not just blabla. The same way, (show (show (show "blabla"))) outputs """blabla""".

So, in your case, input_line is already a string whose value is not the representation of a string, hence the “no parse” error. The only reason it doesn’t raise an error when you launch the template code unmodified is that the returned value is not used and, Haskell being lazy, the read is not evaluated.

Bonus answer:

A symbol output doesn’t contain double quotes and a easiest way to parse (and then safely use) the direction value is to do something like that:

data Direction = U | UR | R | DR | D | DL | L | UL deriving (Enum, Show, Read)
let d :: Direction
    d = read input_line

Thanks. I ran into this while doing the tutorial.