I tried to use List.hd and List.tl for this task:
let takeCard fst deck =
fst = List.hd deck
List.tl deck
List.hd
takes two arguments but I don't understand why.
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).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.