Home > Blockchain >  Can someone help me understand this recursion in Haskell?
Can someone help me understand this recursion in Haskell?

Time:05-27

Here is the code:

func [] _ = 0
func (head:tail) num
| head > num = func tail num
| head <= num = head   (func tail num)

main = print(func [4,1,2,5,7,6] 2 * 3)

Here is my solution: 4 func[1,2,5,7,6] 6 -> 1 func[2,5,7,6] 6 -> 2 func[5,7,6] 6 -> 5 func[7,6] 6 -> func[6] 6 -> 6 func[] 6 ---> 4 1 2 5 6 = 18 But i checked in online compiler solution is 9, where did i make a mistake i don't get it it's pretty simple code

CodePudding user response:

The expression providing the argument to print should be understood as

(func [4, 1, 2, 5, 7, 6] 2) * 3

not

func [4, 1, 2, 5, 7, 6] (2 * 3)

because function application takes precedence over any infix operator.

  • Related