Re: I like this but I'm getting a warning message.
"Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> Wed, 23 Mar 2016 11:55:41 -0500
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <CAM0XMJRJbcq2Z+LUrS78UhXzfb=eCagjFoFrky564YY-Oo+6Ww@mail.gmail.com> |
Ken, Thanks! I'll see what I can do to fix it. It's probably not a big deal because everything still works okay. I just hope INRIA never turns that warning message into an error message that blocks compilation. Then THAT would be a real problem! Also, a quick question here. Where I go to school Java is the #1 language. Not a bad choice at all. I think Java rocks, but there are other languages of course. In my CS department Python is the #2 language. Why hasn't Ocaml become more popular? It's actually an interesting language, and can really help CS students break through the "recursion barrier" that makes the Data Structures course insurmountably difficult for some students. Best, Douglas. On Wed, Mar 23, 2016 at 11:39 AM, Kenneth Miller [email protected] [ocaml_beginners] <[email protected]> wrote: > > > I'm guessing that at the line: > > *getFrequency n lst ; * > You're discarding the value resolved by that function call - it will not > get used, and the computation will be wasted because the garbage collector > will munch it up. The compiler is telling you that unit is the type it > should return, because it resumes that if you call a function with a > non-unit return value, that that unit is something that you should pay > attention to, or explicitly tell the compiler to shut up with > Pervasives.ignore. > > > On Wednesday, March 23, 2016 12:34 PM, "Douglas Lewit [email protected] > [ocaml_beginners]" <[email protected]> wrote: > > > > 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. > > > > >