Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First and last element of list OCaml

I am trying to get first and last element of the list in OCaml. I expect that my function will be like

'a list -> 'a * 'a

What I am trying to do is

let lista = [1;2;3;4;6;0];;

let rec first_last myList =
        match myList with
        [x] -> (List.hd lista,x)
        | head::tail ->     
                  first_last tail;;

first_last lista;;

Of course because of I made list as integer then I am doing this syntax like

*int list -> int * 'a

The point is that I dont have idea how to do this function for 'a.

Whats the direction?

like image 606
miechooy Avatar asked Dec 08 '22 23:12

miechooy


1 Answers

The direction is to write two different functions first and last and implement the first_and_last function as:

let first_and_last xs = first xs, last xs
like image 88
ivg Avatar answered Jan 09 '23 17:01

ivg