jth41
jth41

Reputation: 3906

Write method to return fourth element in List

I currently am attempting this.

let L = [2; 4; 6; 8]

let fourth listx  = List.nth(listx 3);;

fourth L;;

but I want 'a -> 'a (list to list) not int -> 'a

How do I fix this?

Upvotes: 0

Views: 143

Answers (2)

let fourth x = (fun x -> [x]) (List.nth x 3)
val fourth : x:'a list -> 'a list

Upvotes: 0

John Palmer
John Palmer

Reputation: 25516

You want something like

let fourth listx = List.nth listx 3

This gives a signature of 'a list -> 'a which is I think what you want. The key difference is the absence of brackets which don't do what you are expecting in this case

Upvotes: 4

Related Questions