Edin Citaku
Edin Citaku

Reputation: 23

Unexpected error in nested function

Hey, Im pretty new to functional programming and am trying to write a function that gets a list as a input. This function should return the last element and the remaining list in a tuple.

let poplast l = let rec temp acc t = match t with
    |[] -> failwith "Error"
    |[x]-> (acc,x)
    |x::xs -> temp x::acc xs
    in temp [] l;;

This is the error I get is this:

Error: This expression has type 'a list but an expression was expected of type 'b * 'c

right after the third match. I really dont see whats wrong.

Upvotes: 0

Views: 60

Answers (1)

gallais
gallais

Reputation: 12123

OCaml parses temp x::acc xs as (temp x) :: (acc xs) but what you mean is temp (x::acc) xs.

Upvotes: 3

Related Questions