For loop linguistic abstraction (usage)

Filip Konvička filip.konvicka at logis.cz
Mon Feb 26 07:57:35 CET 2007


Hi Craig,
> Hello,
>
>    The following code calculates the frequency of elements in a list.  
> How
> would the second function be written using the for loop linguistic
> abstraction for lists if possible?  Thanks - Craig.
>
>
> declare
>
> fun {IncMapForFeature FreqMap X}
>   if {HasFeature FreqMap X} == false then
>      {AdjoinAt FreqMap X 1}
>   else
>      {AdjoinAt FreqMap X FreqMap.X+1}
>   end
> end
You can use {CondSelect FreqMap X 1} here.
> fun {CalcFreqMap List}
>   local
>      fun {CalcFreqMap2 List FreqMap}
>  case List of nil then FreqMap
>  [] X|Xr then {CalcFreqMap2 Xr {IncMapForFeature FreqMap X}}
>  end
>      end
>   in
>      {CalcFreqMap2 List freqmap()}
>   end
> end
Beware - you are using List as a local variable name, but it is a 
builtin module name. I suggest you turn on the "variable shadowing" 
compiler flag.

You can do this like this

fun {CalcFreqMap L}
   D={NewDictionary}
in
   fun X in L do
       O N
   in
       {Dictionary.condExchange D X O N}
       N=O+1
   end
   {Dictionary.toRecord freqmap D}
end

Cheers,
Filip



More information about the mozart-users mailing list