For loop linguistic abstraction (usage)

Filip Konvička filip.konvicka at logis.cz
Mon Feb 26 21:27:22 CET 2007


Raphael Collet 26.2.2007 9:28:
> Filip Konvička wrote:
>>
>> 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
>
> There is no concurrency at all in the function.  So you don't need the 
> operation Dictionary.condExchange to update the dictionary.  You can 
> separate read and write operations in the dictionary, which makes it a 
> bit more readable, IMHO:
I don't think it's more readable (maybe because I'm used to it :-))...in 
fact, your code is just a bit more inefficient, since there are multiple 
dictionary lookups per iteration. I've also found out that the A.B:=C 
notation is a very common cause of runtime errors, so I prefer using the 
standard procedure/function call syntax to avoid this.
>
> fun {CalcFreqMap L}
>    D={NewDictionary}
> in
>    for X in L do
>       D.X := {CondSelect D X 0} + 1
>    end
>    {Dictionary.toRecord freqmap D}
> end
>
> Note that the call to CondSelect returns D.X if it exists, and 0 
> otherwise.
I did not know that CondSelect works for dictionaries as well, thanks 
for the info!

Cheers,
Filip



More information about the mozart-users mailing list