I am not really understand about the function (parse_list) at
None -> List.rev is and None -> []
let try_parse parse x = try Some (parse x) with Error _ -> None;;
let parse_list parse =
let rec aux is = function
| [] -> List.rev is, []
| (hd :: tl) as xs ->
match try_parse parse hd with
| Some i -> aux (i::is) tl
| None -> List.rev is, xs
in aux [];;
and
let parse_list parse =
let rec aux is = function
| [] -> List.rev is, []
| (hd :: tl) as xs ->
match try_parse parse hd with
| Some i -> aux (i::is) tl
| None -> [], xs
in aux [];;
Are they different? could you please give me an example if they are different? Thank you very much
Yes, they are different.
In the first one, when the parse function will fail, the function
parse_listwill return a partial list of “parsed” expression (List.rev is).In the second one, when the parse function will fail, you’ll get an empty list from
parse_list([]).Look this example with a parse function which will keep only integers lesser than
3:With the first implementation you’ll get:
wit the second one, you’ll get: