Lisp vs. C++ (not off-topic)

Sean Kelly sean at f4.ca
Wed Oct 11 11:03:06 PDT 2006


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.


Sean



More information about the Digitalmars-d mailing list