Quantcast
Viewing latest article 11
Browse Latest Browse All 517

How do I create a dictionary in OCaml that associates to each element of the first list the number of its occurences in the second list?

I have two lists, ["0","1"] and ["0","1","0"], and I want to get a list, [(0,2),(1,1)], - which associates to each element of the first list the number of its occurrences in the second list. I tried this:

let partialSolution = List.map(    fun x ->         let subsubList = List.for_all  (fun y -> (String.compare x y)=0) ) ("0"::("1"::("0"::[]))) in (x, List.length ( List.filter ( fun z -> z = true) subsubList ) ) ) ;;

, but it's not good: it gives me these errors:

# let partialSolution = # List.map(#     fun x -> #         let subsubList = List.for_all  (fun y -> (String.compare x y)=0) )File "", line 4, characters 73-74:Error: Syntax error: operator expected.#  ("0"::("1"::("0"::[]))) inFile "", line 4, characters 99-101:Error: Syntax error#  (x, List.length ( List.filter ( fun z -> z = true) subsubList ) ) # )File "", line 6, characters 0-1:Error: Syntax error#  ;;File "", line 6, characters 2-4:Error: Syntax error

I would like to understand how I can fix this - I am a total newbie to OCaml.


Viewing latest article 11
Browse Latest Browse All 517

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>