5
votes

I am using Visual Studio Code as the text editor of chice and the following Haskell code does not compile. Apperently due to indentation or missing parentheses mistake. Since there are no parenthesies I wonder where the problem is

safeSqrt :: Either String Doubble -> Either String | Doubble
safeSqrt sx =
     case sx of
         Left str -> Left str
         Right x -> if x < 0
             then Left "Error"
             else Right $ sqrt x

The GHCi throws the following error message:

Main.hs:51:1: error:
    parse error (possibly incorrect indentation or mismatched brackets)
   |
51 | safeSqrt sx =    
   | ^

Can enybody help

Thanks

Tom

1
The type should be Either String Double, not with a pipe in between.Willem Van Onsem

1 Answers

7
votes

The problem is not with the indentation. It is with the type signature. You used a pipe character (|) in the signature for Either. You should remove that. Furthermore you misspelled Doubble. While a double with double b is nice, it is unfortunately not the name of a Double:

safeSqrt :: Either String Double -> Either String Double
safeSqrt sx =
     case sx of
         Left str -> Left str
         Right x -> if x < 0
             then Left "Error"
             else Right $ sqrt x