1

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
  • 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. Commented Oct 13, 2019 at 13:52

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Q1 The reason is that I did search and found somebody used return True in Haskell..... Thank you so much for a more reliable answer.
@GTTT: well for monadic computations, like IO, Maybe, etc. one can indeed sometimes use return. But not for non-monadic ones, like the one here.
Q2 I didnt fully understand the $ operator, but now because of your explanations I would be more confident of that! thx a lot
@GTTT: ($) is an operator that has very low precedence (haskell.org/onlinereport/decls.html), it is basically used to group the part at the left, and the part at the right, and make a function application. So ... $ ..., is nearly always equivalent to (...) (...) (but without brackets).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.