Function that applies a function x times on an argument
Write a doItX :: (a -> a -> a) -> a -> Int -> a function that applies the first parameter f x times on the second parameter.
doItX (*) 2 1 == 2
doItX (++) "a" 6 == "aaaaaa"
doItX (*) 2 3 == 8
I have the following code:
doItX f a x
| x==1 = a
| x>1 = doItX f (f a a) (x-1)
That works fine if x is 2 or less, but for the third example, it returns 16 instead of 8.
from Recent Questions - Stack Overflow https://ift.tt/3J1Efuf
https://ift.tt/eA8V8J
Comments
Post a Comment