I try to use guarded equation to define a function. Why does it not work in GHCi? Thanks.
Prelude> :{
Prelude| maxThree :: Integer -> Integer -> Integer -> Integer
Prelude| maxThree x y z
Prelude| x >= y && x >= z = x
Prelude| y >= z = y
Prelude| otherwise = z
Prelude| :}
<interactive>:77:1: error: Parse error in pattern: x >= y
Your syntax is wrong. Don’t be confused by the fact that the prompt already contains
|! What you’ve written is the following:As you can see, this is clearly wrong. Guards always start with a vertical bar
|, but you’ve left it out. I assume you got confused by the fact that thePrelude|prompt already contains|; that is part of the UI of GHCi, and is not considered to be part of the code you type in. If you want to type a guard into GHCi, do it like this:Note how I have typed the code into GHCi exactly the same as I would type it into a file, including the fact that the guards need to be indented relative to the start of the definition.