Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# If Statement List.exists

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?

like image 941
Nulle Avatar asked Oct 05 '16 10:10

Nulle


1 Answers

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.

like image 154
Bartek Kobyłecki Avatar answered Nov 13 '22 07:11

Bartek Kobyłecki