Haskell : Error in split function -
i new @ haskell. want implement split function, splites list 2 parts:
split 2 [1,2,3] → ([1,2], [3]) --means first part has length 2, second - length x-2 split 2 [1] → ([1], []) split :: int -> [a] -> ([a],[a]) split 0 x = ([], x) split n x = splith n x [] splith :: int -> [a] -> [a] -> ([a], [a]) splith n (x:xs) begin | n == 0 = (begin, xs) | otherwise = splith n-1 xs (x:begin) -- here error main = print(split 2 [1,2,3] )
but code not compile. error
`couldn't match expected type ‘([a], [a])’ actual type ‘[a0] -> [a0] -> ([a0], [a0])’ relevant bindings include begin :: [a] (bound @ jdoodle.hs:6:17) xs :: [a] (bound @ jdoodle.hs:6:13) x :: (bound @ jdoodle.hs:6:11) splith :: int -> [a] -> [a] -> ([a], [a]) (bound @ jdoodle.hs:6:1) probable cause: ‘splith’ applied few arguments in first argument of ‘(-)’, namely ‘splith n’ in expression: splith n - 1 xs (x : begin)`
what cause error ?
put parentheses around expression n-1
:
splith (n-1) xs (x:begin)
look @ section 7, "function application has precedence on operators" of this article explanation:
so if see this:
f b c + g d e
you know adding result of 2 function calls rather calling 1 function 1 of arguments being sum of 2 terms.
Comments
Post a Comment