0

I'm just learning Haskell, and I decided to try writing my own version of pred, which returns the number preceding its parameter. I'm using WinGHCi and loading a file called test.hs. Here is my code:

prev :: (Num a) => a -> a
prev x = prev' 0 x
  where prev' y z
    | (succ y) == z = y
    | otherwise = prev' (succ y) z

I get the error:

test.hs:4:5:
    parse error (possibly incorrect indentation or mismatched brackets)

How does one properly write a helper function with guards?

2
  • possible duplicate of Why parse error? Indentation? Commented Dec 31, 2013 at 2:45
  • The standard answer here is to be sure you're using spaces, not tabs.. Commented Dec 31, 2013 at 7:12

1 Answer 1

2

Your pattern guards need to be more deeply indented—they need to be further to the right than the p in the definition of prev'. This version doesn't give the parse error:

prev :: (Num a) => a -> a
prev x = prev' 0 x
  where prev' y z
            | (succ y) == z = y
            | otherwise = prev' (succ y) z

You're still getting a type error there, though—need more type class constraints.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the great answer! What do you mean by type class constraints?
I got it - I need to replace (Num a) by something like (Integral a) so it can test for equality. Thanks!

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.