Tom
Tom

Reputation: 51

Haskell Indentation issue

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

Upvotes: 5

Views: 201

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476503

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

Upvotes: 7

Related Questions