Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 527

Pattern matching on rest of list

$
0
0

I'm trying to pattern match on a list of pairs, where I'm trying to return a list from the list of pair, however I'm having trouble figuring out where to make the recursive call. Without the recursive call I have this:

let countriesInChart (cht: chart) =   match cht with  | [] -> []  | (x,y)::tt -> [x;y];;

But naturally this only applies to the first pair in the list and simply returns ["countryA"; "countryB"] without the rest of the list.

With the recursive call this simply only returns an empty list:

let rec countriesInChart (cht: chart) =   match cht with  | [] -> []  | (x,y)::tt -> [x;y]::countriesInChart tt ;;

How would I make the recursive call such that all the pairs in the list would return as a list?


Viewing all articles
Browse latest Browse all 527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>