Lisp vs. C++ (not off-topic)
Lionello Lunesu
lio at lunesu.remove.com
Thu Oct 12 03:58:09 PDT 2006
Sean Kelly wrote:
> Oh, a quick comment on loops, since the article mentions those as well.
> Here is the Lisp code:
>
> (loop for word in (gethash n *dict*) do
>
>
> His C++ version:
> for (HashMap::iterator i = gDict.equal_range(n).begin();
> i != gDict.equal_range(n).end(); i++)
>
> In D, if the words were stored in a sorted array, I would write:
>
> foreach( word; dict[lowerBound(n) .. upperBound(n)] )
>
>
> And if they were in an AA (char[][][int] dict):
>
> foreach( word; dict[n] )
>
> Note that this expects the lookup for n to succeed or the current AA
> code will throw an exception. A more robust version:
>
> char[][]* list = n in dict;
> if( list ) foreach( word; *list )
>
> seems unnecessarily wordy and somewhat unreadable. It would be nice if
> AAs had some lookup method that returned a default value on failure
> instead of requiring the pointer check above.
Perhaps foreach could always check for 'null' if you pass it a pointer
to an array.
L.
More information about the Digitalmars-d
mailing list