I like this but I'm getting a warning message.
"Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> Wed, 23 Mar 2016 11:34:13 -0500
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <CAM0XMJR6zFnvj0W_=U=tb7FPmT1LGFzU00PrYW6WDHk5cXa6ig@mail.gmail.com> |
Hi everyone, I wrote these functions, and they are pretty good in my humble opinion. ( Forgive the lack of modesty, but I really worked hard on these. ) However, I'm getting "Warning 10: this function should have output unit( )" or something like that. Is that really a bad thing? Is there a workaround? Thanks for sharing. *(* Use the following functions to get the frequencies from a list. *)* *let get_frequency n lst = let count = ref 0 in * *let rec getFrequency m l = match l with * *|[ ] -> [ ]* *|head :: tail -> if head = m then begin incr count ; getFrequency m tail end else getFrequency m tail in * *getFrequency n lst ; * *[ n ; !count ] ;;* *let rec makeSet lst = let rec mem num l = match l with * *|[ ] -> false* *|head :: tail -> if head = num then true else mem num tail in * *match lst with * *|[ ] -> [ ]* *|head :: tail -> if mem head tail then makeSet tail else head :: ( makeSet tail ) ;;* *let frequency_list lst = let set = makeSet lst in * *let rec freq_list s l = match s with * *|[ ] -> [ ]* *|head :: tail -> ( get_frequency head l ) :: ( freq_list tail l ) in * *freq_list ( List.sort compare set ) lst ;;* *(* Use the following function to generate a list of random integers between "lower" and "upper", inclusive. *)* *let rec random_sequence lower upper size = if size = 0 then [ ] else ( lower + Random.int( upper - lower + 1 ) ) :: * *( random_sequence lower upper ( size - 1 ) ) ;;* So for example, if I apply the function *frequency_list* to [ 1; 1; 1; 1; 1; 2; 2; 2; 2; 2; 2 ] Ocaml should return [ [1; 5] ; [2; 6] ] ( Interpretation: The original list contains 5 1's and 6 2's. ) Thanks for the help! I really struggled with this one. There could be an easier way to find the frequencies of a list, but that's my solution to the problem. It works! But not sure how really "efficient" or fast my code is compared to other solutions to the same problem. Best, Douglas.