1
votes

I'm a beginner in haskell and trying to do a cipher validate function.

 validateCipher :: [Char] -> Bool
 validateCipher cipher =
       if length $ nub cipher == length cipher  
        then return True
        else return False

When I run it through ghci, it just told me: error: parse error on input ‘validateCipher’. Not sure what is going wrong, should I add Eq or any stuff(I don't quite sure what does it do either)?

1
remove the return. In Haskell there is no return keyword, there is only a return function. Furthermore the $ results in parsing it as `length (nub cipher == length cipher), so you should use brackets. - Willem Van Onsem

1 Answers

1
votes

There are two errors here:

  1. you use return. In Haskell there is no return keyword, there is a return :: Monad a => a -> m a function, but that works differently; and
  2. the $ results in the fact that length $ nub cipher == length cipher is parsed as length (nub cipher == length cipher), and that does not satisfy the type constraints, since nub cipher returns a [Char], whereas length cipher, will return an Int.

We do not need return True, or True in the first place here, since we can just return the outcome of the condition we check. We can furthermore use parenthesis to write a correct expression:

validateCipher :: [Char] -> Bool
validateCipher cipher = length (nub cipher) == length cipher