Suppose I have some code:
let listB = [ 1; 2; 3 ]
Using Lisp notation, how do I do a car and cadr against this list? I know cons is ::.
Or in Scheme, first and rest?
List.hd and List.tl will do what you want - but in F# you will find that lists are typically deconstructed using pattern matching. For example, in the following function x matches the head and xs matches the tail of the list passed to the function:
let list = [1;2;3]
let rec f = function
| [] -> 1
| (x::xs) -> x * (f xs)
f list;
List.head: Returns the first element of a nonempty list (The head of the list).
List.tail: Returns all the elements of a nonempty list except the first (The tail or rest of the list).
Example (using F# Interactive Console):
> let sample = [1;2;3;4];;
val sample : int list
> List.head sample;;
val it : int = 1
> List.tail sample;;
val it : int list = [2; 3; 4]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With