Ocaml: error type -
can explain why have error in code ?
it take 2 list a1, a2, a3 ... , b1 b2 b3 ... produce a1, b1, a2, b2, a3, b3 ...
but error appears
error: expression has type int expression expected of type int list
example of use: append [1; 2; 3] [4; 5; 6];;
code:
let rec append b = if list.length == 0 && list.length b == 0 [] else (list.hd :: list.hd b) :: append (list.tl a) (list.tl b);;
this should work properly, doesn't... how can fix ?? thanks!
this precedence problem. can't write this:
(1 :: 2) :: []
the right hand operand of ::
must list.
you have write this:
1 :: (2 :: [])
if remove parentheses around (list.hd :: list.hd b)
should past problem.
as side comment, more idiomatic use pattern matching list.length, list.hd, , list.tl.
what code supposed if lists different lengths?
Comments
Post a Comment