I have this data type:
data Teletype a = End a
| Get (Char -> Teletype a)
| Put Char (Teletype a)
A value of this type can be used to describe programs that read and write characters and return a final result of type a. Such a program can end immediately (End). If it reads a character, the rest of the program is described as a function depending on this character (Get). If the program writes a character (Put), the value to show and the rest of the program are recorded.
I have to write a Teletype-program getLine which reads characters until it finds a newline character, and returns the complete string. So far, I have this:
getline = Get (\c -> if c == "\n" then (Put c (End c)) else getline )
But it doesn't compile because
Couldn't match expected type ‘Char’ with actual type ‘[Char]’
• In the second argument of ‘(==)’, namely ‘"\n"’
In the expression: c == "\n"
In the expression: if c == "\n" then (Put c (End c)) else getline
c is a Char, indeed, the data constructor says Get (Char -> Teletype a). "\n" is not a Char, that is a String (so [Char]).
You can compare with a new line, by comparing with '\n' (notice the single brackets):
getline :: Teletype Char
getline = Get (\c -> if c == '\n' then Put c (End c) else getline)
As @chi says, this is not this does not is a complete solution to the exercise. This fixes a local problem, such that you can continue solving it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With