I wrote this function which was supposed to give me the before last element of a list, but it doesn't work? Do you know why? thanks!
let rec exo1= match l with |List.length l = 0 or 1 -> failwith exo1 |List.length l > 2 -> List.tl l in exo1 l |List.length l = 2 -> List.hd l ;;
Try something like below.. I commented out the answer.
let rec exo1 lst =
match lst with
| [] -> failwith "List is too short!"
(*| (*the magic happens here*) -> (*ans*)*)
| hd::tl -> exo1 tl
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.