Joofsie
Joofsie

Reputation: 51

How does this function operate with partial application?

let all_contain_true l =
  not (List.mem false (List.map (List.mem true) l))

How is partial application used to make this function work to see if all the lists in the giant list contain true

Upvotes: 1

Views: 107

Answers (1)

Chris
Chris

Reputation: 36496

The only partial application I see is List.mem true. When we evaluate this at the toplevel, we see:

# List.mem true;;
- : bool list -> bool = <fun>

This expression returns a function which maps a list of bool values to a bool based on whether the list contains the value true.

We can see this is action:

# let f = List.mem true;;
val f : bool list -> bool = <fun>
# f [false; true; false];;
- : bool = true
# f [false; false];;
- : bool = false

List.map takes a function and a list. List.mem true provides the function.


Style points

For what it's worth, the |> operator may be useful in streamlining this function, and we can give a name to that function.

let all_contain_true l =
  let contains_true = List.mem true in
  l |> List.map contains_true |> List.mem false |> not

You could also locally open the List module.

let all_contain_true l =
  List.(
    let contains_true = mem true in
    l |> map contains_true |> mem false |> not
  )

Upvotes: 2

Related Questions