2

I would like to do the following in Haskell:

myFunc 1 = 0
myFunc 2 = 1

changeMyFunc:: (Integer -> Integer) -> Integer -> Integer -> (Integer -> Integer)
changeMyFunc x y z = undefined

-- change value of function x at position y to value z and return this function
-- this would make: (changeMyFunc myFunc 1 5) 1 == 5

This is how far i got it:

changeMyFunc2:: (Integer -> Integer) -> Integer -> Integer -> (Integer -> Integer)
changeMyFunc2 x y z | y == z = myFunc
                | y /= z = myFunc where myFunc y = 12
                | otherwise myFunc

the last line causes: parse error on input '|' How can i check multiple cases using where?

5
  • 3
    This looks like an exercise. What have you attempted first? Where are you stuck? Commented Apr 17, 2015 at 21:21
  • I do not know how to change a value. returning it as a function is not a problem though. I tried returning another function that is identical to the first function, but it differs in that one given position. Commented Apr 17, 2015 at 21:24
  • |s need to line up. Commented Apr 17, 2015 at 23:08
  • 1
    @Cubic I don't think this is the problem. Commented Apr 17, 2015 at 23:09
  • 1
    @Cubic They actually don't. |s aren't given any indentation block. (Except with the MultiWayIf syntax extension, where they had to add one for sanity when nesting them.) Commented Apr 18, 2015 at 1:27

1 Answer 1

2

When defining changeMyFunc x, the first step would be to just return the original myFunc x itself. That gets you almost the whole way there.

Then divide that into two cases: one case where x == y and the other case where x /= y, where y is your special input. What should changeMyFunc x return in each case?

Updated hint

Currently you are accepting args x, y, z. Try something with this instead:

changeMyFunc f y z x
    | fill this part in yourself

f is the original function. y is the special input and z is the special output. x is the argument for the new changeMyFunc function that you're defining.

See How do you use a function inside arguments in haskell? for more information on higher-order functions.

Think about which args you should be comparing, and what the output should be in each case. (Hint: I already gave part of the answer above.)

Then you can test it with this:

*Main> let successor = (+) 1
*Main> let successor' = changeMyFunc successor 1 1000
*Main> successor' 100
101
*Main> successor' 101
102
*Main> successor' 500
501
*Main> successor' 1
1000

Solution

changeMyFunc :: (Integer -> Integer) -> Integer -> Integer -> (Integer -> Integer)
changeMyFunc f y z x
| x == y = z
| otherwise = f x

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

5 Comments

This helped me alot and its almost working. The key was to use the "where" function. However, i cannot use the "|" operator in combination with where somehow. The fist line seems to work out, but every following line produces this error: "parse error on input '|'
@MeterPaffay where clause should go after all the guards.
@zudov i thought i could use 'where' to evaluate the return value. It seems this is not possible. How can I manage to do this. To make it clear: i try to return a function that differs in only one given position.
@MeterPaffay I am not sure what you mean by "evaluate the return value", but the purpose of where is to create additional definitions which would be available only in scope of the definition in which where is used. Multiple guards constitute single definition, so there can be only one where for a chain of guards.
I'll be glad to provide an answer with an explanation of these issues if you'll change the question to address specific problems.

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.