Computers and Technology

Write a function buckets : ('a -> 'a -> bool) -> 'a list -> 'a list list that partitions a list into equivalence classes. That is, buckets equiv lst should return a list of lists where each sublist in the result contains equivalent elements, where two elements are considered equivalent if equiv returns true. For example: buckets (=) [1;2;3;4] = [[1];[2];[3];[4]] buckets (=) [1;2;3;4;2;3;4;3;4] = [[1];[2;2];[3;3;3];[4;4;4]] buckets (fun x y -> (=) (x mod 3) (y mod 3)) [1;2;3;4;5;6] = [[1;4];[2;5];[3;6]] The order of the buckets must reflect the order in which the elements appear in the original list. For example, the output of buckets (=) [1;2;3;4] should be [[1];[2];[3];[4]] and not [[2];[1];[3];[4]] or any other permutation. The order of the elements in each bucket must reflect the order in which the elements appear in the original list. For example, the output of buckets (fun x y -> (=) (x mod 3) (y mod 3)) [1;2;3;4;5;6] should be [[1;4];[2;5];[3;6]] and not [[4;1];[5;2];[3;6]] or any other permutations. Assume that the comparison function ('a -> 'a -> bool) is commutative, associative and idempotent. Just use lists. Do not use sets or hash tables. List append function @ may come in handy. [1;2;3] @ [4;5;6] = [1;2;3;4;5;6].

let buckets p l =

(* YOUR CODE HERE *)

raise (Failure "Not implemented")

assert (buckets (=) [1;2;3;4] = [[1];[2];[3];[4]]);

assert (buckets (=) [1;2;3;4;2;3;4;3;4] = [[1];[2;2];[3;3;3];[4;4;4]]);

assert (buckets (fun x y -> (=) (x mod 3) (y mod 3)) [1;2;3;4;5;6] = [[1;4];[2;5];[3;6]])

answer
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 11:30, LindseyN1
One subtask in the game is to roll the dice. explain why is roll the dice an abstraction.
Answers: 3
image
Computers and Technology, 22.06.2019 15:00, miracle9704
Atool that matches persoal skills qualities interests and talets to a career is called a
Answers: 1
image
Computers and Technology, 23.06.2019 20:40, bobby3350
On nba 2k 19, every time i try to join a my park game, it leads ro a website telling my dad that he needs ps plus. i already have ps plus though. how do i fix this?
Answers: 2
image
Computers and Technology, 24.06.2019 09:00, king514
Technician a says that a new replacement part is always good. technician b says that sometimes recent repair work will be the cause of a complaint. who is correct? a. both technicians a and b b. technician a c. technician b d. neither technician a nor b
Answers: 3
Do you know the correct answer?
Write a function buckets : ('a -> 'a -> bool) -> 'a list -> 'a list list that partitions...

Questions in other subjects:

Konu
Mathematics, 29.09.2019 12:10
Konu
Mathematics, 29.09.2019 12:20