Domanda

I have a function that returns a (char list) list option, and I'm trying to get the size of the list:

let c = recherche m ledico in
  match c with
    | None -> Printf.printf "Non."
    | Some [] -> Printf.printf "Oui."
    | _ ->
      let n = List.length c in
(...)

recherche is the function that returns me the (char list) list option, and it can return either None, Some [], or Some [[...] ; ... ; [...]]. How do I find this length? I saw this solution but it didn't work:

Error: The function applied to this argument has type 'a list -> 'a list
This argument cannot be applied with label ~f

How do I get the size of a list option?

È stato utile?

Soluzione

You just need to give the list a name.

| Some l -> let n = List.length l in ...

Altri suggerimenti

To apply a function to 'a option, you just need a function that works on values of type 'a, and a value to return when the input is None. There is a function in the Core library named value_map that does this. The basic implementation is very simple:

let value_map x default f =
    match x with
    | None -> default
    | Some sx -> f sx

In your case, you need to choose a default. The function you want to apply is List.length.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top