0

I'm having an issue with a conditions where I have nested them. I'm new to Haskell and can't seem to find anything similar in my book or online. Here is an example of what I have:

someFunc s n r c e i
    | (i < s) 
    >>> | (e < s) = someFunc (changes go here conditions are met)
        | otherwise = createList (different set of conditions go here)
    | otherwise = n

The error being given is: "parse error on input `|'" at the point denoted in the code. How what is the best method of solving this?

Thanks and sorry for English.

Minmater
  • 113
  • 1
  • 2
  • 4

1 Answers1

1

You can't nest guards that way, but it might be cleaner to separate it into two functions like so:

someFunc s n r c e i
    | (i < s) = innerCondition s n r c e i
    | otherwise = n

innerCondition s n r c e i
    | (e < s) = someFunc (changes go here conditions are met)
    | otherwise = createList (different set of conditions go here)

Alternatively, you could keep it nested within the same function with an if statement:

someFunc s n r c e i
    | (i < s) =
        if (e < s) then
            someFunc (changes go here conditions are met)
        else
            createList (different set of conditions go here)
    | otherwise = n

But I think the first example with separate guarded functions is cleaner.

Chad Gilbert
  • 36,115
  • 4
  • 89
  • 97
  • Also note that if the inner condition is not exhaustive (e.g. does not end with `otherwise` or equivalent) then the program may crash without returning control to the outer conditions, as one might expect. – chi Feb 19 '16 at 21:25