Ocaml - parameter type when checking duplicates in a list

I have a basic function that checks the list for duplicates and returns true if they are found, otherwise false.

    # let rec check_dup l = match l with
        [] -> false
      | (h::t) ->
        let x = (List.filter h t) in
        if (x == []) then
           check_dup t
        else
           true
    ;;

But when I try to use this code, I get an error

    Characters 92-93:
          let x = (List.filter h t) in
                                 ^
    Error: This expression has type ('a -> bool) list
           but an expression was expected of type 'a list

I really don’t understand why this is happening, where did the list type a-> bool come from?

+5
source share
2 answers

('a -> bool) list filter h::t. , h, t. ML . filter : 'a -> bool, 'a , 'a list, 'a - , . h 'a -> bool t 'a list.

h::t, , 'b , h 'b t 'b list. , :

'a -> bool == 'b
'a list == 'b list

, , 'a == 'b,

'a -> bool == 'a

- , .

, .


, List.filter (fun x -> x = h) t, , , List.exists.

+8

:

let lstOne = [1;5;4;3;10;9;5;5;4];;

let lstTwo = [1;5;4;3;10];;

let rec check_dup l = match l with
    [] -> false
    | (h::t) ->
       let x = (List.filter (fun x -> x = h) t) in
         if (x == []) then
            check_dup t
         else
       true;;

:

# check_dup lstOne

- : bool = true

# check_dup lstTwo

- : bool = false

# 
0

All Articles