1

I am implementing a recursive function and I want the stopping condition be (2*scope) which is parameter to function

sortByManhattanDistance agent (2*scope) scope xs sortedNearFoodList = sortedNearFoodList

sortByManhattanDistance agent n scope xs sortedNearFoodList = sortByManhattanDistance agent (n+1) scope xs (sorted ++ sortedNearFoodList) 
where sorted=compareManhattanDistance xs agent n

and hugs complain that: Syntax error in declaration (unexpected symbol "*") Does it mean that I cannot use some function on parameters?

thanks in advance

3
  • You can't use (2*scope) as a input argument value. That is not a valid syntax Commented Mar 31, 2013 at 12:28
  • yeah i forgot it. thanks! Commented Mar 31, 2013 at 12:32
  • related: stackoverflow.com/questions/3748592/… Commented Apr 1, 2013 at 12:18

1 Answer 1

5

No, you cannot use functions or operators on the left hand side of an equation like that.

The proper way to do what you want is to use guards:

sortByManhattanDistance agent n scope xs sortedNearFoodList
    | n == 2 * scope = sortedNearFoodList
    | otherwise      = sortByManhattanDistance agent (n+1) scope xs
                                            (sorted ++ sortedNearFoodList) 
  where sorted = compareManhattanDistance xs agent n
Sign up to request clarification or add additional context in comments.

1 Comment

i forgot about this! your advice was very useful

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.