Re: I like this but I'm getting a warning message.
"Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> Wed, 23 Mar 2016 12:17:24 -0500
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <CAM0XMJRNcvrYTsEZXOL+trek=EHbbShwhYcq2TMc-DH+sxm0Qw@mail.gmail.com> |
Hey Ken, Yes, I tried playing around with the code, but.... well maybe I'm just tired! I think I still need that line, but I looked up *Pervasives.ignore* and it works like magic! Thanks for the recommendation. For the time being I am willing to live with a "minor amount of error" as long as my code runs! ( Is my Java background showing through at this point? LOL. ) I get the impression that Ocaml, Scala, Haskell, etc are languages that appeal mainly to people with a very strong math background. These days a lot of non-math people are studying computer science. A lot depends on the department. Some CS programs require students to take a lot of math courses. Other CS programs are less rigorous and are basically training students to go into IT, computer security, web development, and other areas where there is less emphasis on strong mathematical rigor. I think it just depends on a person's long-term goals. I get the feeling that the functional languages really come into their own when it comes to AI and related fields. But for a lot of other stuff it seems that imperative and object-oriented styles are more than sufficient to get the job done. Although to me it's really interesting that the financial industry ( or at least a part of it ) has adopted Ocaml with such passion, especially since C and C++ used to rule Wall Street, or at least that's what older programmers have told me. On Wed, Mar 23, 2016 at 12:05 PM, Kenneth Miller [email protected] [ocaml_beginners] <[email protected]> wrote: > > > Well actually if everything works fine, just delete that line. In fact, it > might be better if you turned warnings into errors, because it requires you > to think about your program more. The functional and strongly typed > communities have a big place in their hearts for correctness. If you take > your warnings to any of those communities, they'll tell you that the > compiler was engineered with rigor in mind. > > OCaml probably isn't more popular because it is hard. The compiler won't > put up so much will other stuff, whereas Java/python/xyz will run will all > manner of wrong input. It just blows up when it hits it, preventing you > from finding errors at compile time but allowing you to move forward for > the time being with your assignment. > > Thinking about math isn't what very many people want to do with their time > because it's hard, you're going to have to acknowledge that. Rigor is what > it is: rigor. > > > On Wednesday, March 23, 2016 12:55 PM, "Douglas Lewit [email protected] > [ocaml_beginners]" <[email protected]> wrote: > > > > 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. > > > > > > > >