[Solved] OCaml: How can I return the first element of a list and after that remove it from the list?


I think there are a couple of misunderstandings here.

First, most types in OCaml are immutable. Unless you use mutable variables you can’t “remove it from the list”, you can only return a version of the list that doesn’t have that first item. If you want to return both things you can achieve that using a tuple.

let takeCard deck = (List.hd deck, List.tl deck)

Second, List.hd only takes one element. OCaml leverages currying. When reading an OCaml type signature the first parameters are what the function takes in and the last parameter is what the function returns. So List.hd’s signature 'a list -> 'a means that it takes in a list that contains ('a is used as a placeholder) and returns something of the type of stuff the list contains (in this case the first element).

solved OCaml: How can I return the first element of a list and after that remove it from the list?