简体   繁体   中英

Ocaml functions using List and couples

I have an assignment to finish but I'm having trouble with it. This is in ocaml and lists are the main focus.

The first function takes in a list of couples[(a1,b1);(a2,b2)...] and verifies that every "a" are different.

I tried to do this in two parts:

let rec isNotIn x = function
   |[]-> true
   |(a,_)::l -> a <> x && isNotIn a l;;
let isFunction = function
   |[] -> false
   |(a,_)::l -> isNotIn a l;;

I can't find a way to make it work, isFunction only checks if the following couple has the first element equal to its own. But I need it to check it for every first element of the list, not just the next one.

Well, what you did is almost perfect except you forgot the recursive call :

let rec isFunction = function
   | []  -> true (* if the function is empty it's ok, no ? *)
   | (a, _) :: l -> isNotIn a l && isFunction l;;

And, actually, your first function doesn't only take a list of couples but takes an element x and verifies that x is not the first element of any couple in the list.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM