7
votes

This is a School Assignment but everything i am about to post is done by me and me only. Therefore I only require your help for a tiny step in my assignment at which i am stuck.

let rec removeDuplicates2 xs =
  match xs with
  |[]->[]
  |y::ys -> if y = (List.exists y ys) then
              (removeDuplicates2 ys)
            else
              y::(removeDuplicates2 ys)

printfn "%A" (removeDuplicates2 [3;1;3;2;1]) // result must be [3;1;2] 

What i require help for is making the if statement that checks if element y is a member of list ys

at the moment i get the error saying: "This expression was expected to have type ''a -> bool' but here has type 'bool'"

can someone tell me what i am doing wrong?

1
First, you are comparing y with List.exists y ys which should return true or false. You probacly wanted to write if (List.exists y ys) then - Panagiotis Kanavos
Second, List.exists expects a function. If you want to check whether a list contains an item use List.contains - Panagiotis Kanavos
I think the exercise does not imply the use of standard functions :) - FoggyFinder

1 Answers

9
votes

List.exists expects the first argument to be a function which will be checked on the element and returns boolean value. You want to check if element is on the list you could write:

if List.exists ((=) y) ys then

or even:

if List.contains y ys then

following Panagiotis suggestion.