Re: I like this but I'm getting a warning message.
"Kenneth Miller [email protected] [ocaml_beginners]" <[email protected]> Wed, 23 Mar 2016 16:39:31 +0000 (UTC)
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
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.
#yiv9664832499 #yiv9664832499 -- #yiv9664832499ygrp-mkp {border:1px solid #d8d8d8;font-family:Arial;margin:10px 0;padding:0 10px;}#yiv9664832499 #yiv9664832499ygrp-mkp hr {border:1px solid #d8d8d8;}#yiv9664832499 #yiv9664832499ygrp-mkp #yiv9664832499hd {color:#628c2a;font-size:85%;font-weight:700;line-height:122%;margin:10px 0;}#yiv9664832499 #yiv9664832499ygrp-mkp #yiv9664832499ads {margin-bottom:10px;}#yiv9664832499 #yiv9664832499ygrp-mkp .yiv9664832499ad {padding:0 0;}#yiv9664832499 #yiv9664832499ygrp-mkp .yiv9664832499ad p {margin:0;}#yiv9664832499 #yiv9664832499ygrp-mkp .yiv9664832499ad a {color:#0000ff;text-decoration:none;}#yiv9664832499 #yiv9664832499ygrp-sponsor #yiv9664832499ygrp-lc {font-family:Arial;}#yiv9664832499 #yiv9664832499ygrp-sponsor #yiv9664832499ygrp-lc #yiv9664832499hd {margin:10px 0px;font-weight:700;font-size:78%;line-height:122%;}#yiv9664832499 #yiv9664832499ygrp-sponsor #yiv9664832499ygrp-lc .yiv9664832499ad {margin-bottom:10px;padding:0 0;}#yiv9664832499 #yiv9664832499actions {font-family:Verdana;font-size:11px;padding:10px 0;}#yiv9664832499 #yiv9664832499activity {background-color:#e0ecee;float:left;font-family:Verdana;font-size:10px;padding:10px;}#yiv9664832499 #yiv9664832499activity span {font-weight:700;}#yiv9664832499 #yiv9664832499activity span:first-child {text-transform:uppercase;}#yiv9664832499 #yiv9664832499activity span a {color:#5085b6;text-decoration:none;}#yiv9664832499 #yiv9664832499activity span span {color:#ff7900;}#yiv9664832499 #yiv9664832499activity span .yiv9664832499underline {text-decoration:underline;}#yiv9664832499 .yiv9664832499attach {clear:both;display:table;font-family:Arial;font-size:12px;padding:10px 0;width:400px;}#yiv9664832499 .yiv9664832499attach div a {text-decoration:none;}#yiv9664832499 .yiv9664832499attach img {border:none;padding-right:5px;}#yiv9664832499 .yiv9664832499attach label {display:block;margin-bottom:5px;}#yiv9664832499 .yiv9664832499attach label a {text-decoration:none;}#yiv9664832499 blockquote {margin:0 0 0 4px;}#yiv9664832499 .yiv9664832499bold {font-family:Arial;font-size:13px;font-weight:700;}#yiv9664832499 .yiv9664832499bold a {text-decoration:none;}#yiv9664832499 dd.yiv9664832499last p a {font-family:Verdana;font-weight:700;}#yiv9664832499 dd.yiv9664832499last p span {margin-right:10px;font-family:Verdana;font-weight:700;}#yiv9664832499 dd.yiv9664832499last p span.yiv9664832499yshortcuts {margin-right:0;}#yiv9664832499 div.yiv9664832499attach-table div div a {text-decoration:none;}#yiv9664832499 div.yiv9664832499attach-table {width:400px;}#yiv9664832499 div.yiv9664832499file-title a, #yiv9664832499 div.yiv9664832499file-title a:active, #yiv9664832499 div.yiv9664832499file-title a:hover, #yiv9664832499 div.yiv9664832499file-title a:visited {text-decoration:none;}#yiv9664832499 div.yiv9664832499photo-title a, #yiv9664832499 div.yiv9664832499photo-title a:active, #yiv9664832499 div.yiv9664832499photo-title a:hover, #yiv9664832499 div.yiv9664832499photo-title a:visited {text-decoration:none;}#yiv9664832499 div#yiv9664832499ygrp-mlmsg #yiv9664832499ygrp-msg p a span.yiv9664832499yshortcuts {font-family:Verdana;font-size:10px;font-weight:normal;}#yiv9664832499 .yiv9664832499green {color:#628c2a;}#yiv9664832499 .yiv9664832499MsoNormal {margin:0 0 0 0;}#yiv9664832499 o {font-size:0;}#yiv9664832499 #yiv9664832499photos div {float:left;width:72px;}#yiv9664832499 #yiv9664832499photos div div {border:1px solid #666666;height:62px;overflow:hidden;width:62px;}#yiv9664832499 #yiv9664832499photos div label {color:#666666;font-size:10px;overflow:hidden;text-align:center;white-space:nowrap;width:64px;}#yiv9664832499 #yiv9664832499reco-category {font-size:77%;}#yiv9664832499 #yiv9664832499reco-desc {font-size:77%;}#yiv9664832499 .yiv9664832499replbq {margin:4px;}#yiv9664832499 #yiv9664832499ygrp-actbar div a:first-child {margin-right:2px;padding-right:5px;}#yiv9664832499 #yiv9664832499ygrp-mlmsg {font-size:13px;font-family:Arial, helvetica, clean, sans-serif;}#yiv9664832499 #yiv9664832499ygrp-mlmsg table {font-size:inherit;font:100%;}#yiv9664832499 #yiv9664832499ygrp-mlmsg select, #yiv9664832499 input, #yiv9664832499 textarea {font:99% Arial, Helvetica, clean, sans-serif;}#yiv9664832499 #yiv9664832499ygrp-mlmsg pre, #yiv9664832499 code {font:115% monospace;}#yiv9664832499 #yiv9664832499ygrp-mlmsg * {line-height:1.22em;}#yiv9664832499 #yiv9664832499ygrp-mlmsg #yiv9664832499logo {padding-bottom:10px;}#yiv9664832499 #yiv9664832499ygrp-msg p a {font-family:Verdana;}#yiv9664832499 #yiv9664832499ygrp-msg p#yiv9664832499attach-count span {color:#1E66AE;font-weight:700;}#yiv9664832499 #yiv9664832499ygrp-reco #yiv9664832499reco-head {color:#ff7900;font-weight:700;}#yiv9664832499 #yiv9664832499ygrp-reco {margin-bottom:20px;padding:0px;}#yiv9664832499 #yiv9664832499ygrp-sponsor #yiv9664832499ov li a {font-size:130%;text-decoration:none;}#yiv9664832499 #yiv9664832499ygrp-sponsor #yiv9664832499ov li {font-size:77%;list-style-type:square;padding:6px 0;}#yiv9664832499 #yiv9664832499ygrp-sponsor #yiv9664832499ov ul {margin:0;padding:0 0 0 8px;}#yiv9664832499 #yiv9664832499ygrp-text {font-family:Georgia;}#yiv9664832499 #yiv9664832499ygrp-text p {margin:0 0 1em 0;}#yiv9664832499 #yiv9664832499ygrp-text tt {font-size:120%;}#yiv9664832499 #yiv9664832499ygrp-vital ul li:last-child {border-right:none !important;}#yiv9664832499