Knight's Challenge in D

Chris Miller lordSaurontheGreat at gmail.com
Wed Apr 2 14:35:47 PDT 2008


bearophile Wrote:

> Chris Miller:
> 
> >> putr( map((int el){ return format("%2d", el);}, row).join("  ") );
> >So does any of that have a practical application for D?
> 
> 1) This is a D forum, and lot of people here like various languages. I like many languages. I don't think adding few extra lines in my post may hurt.
> 2) D is an evolving language, and new things may be added to it in the future. If you don't discuss things you only keep C/C++ as a reference, and you may end with a C++-like-only language.
> 3) I think functional programming will become more important in the future. The last version (the one starting with putr) is actual D code you can run with my D libs. It's hairy, so it shows why a better syntax is very useful for that kind of programming. AST macros may help there. There is an alternative syntax you can use, plus the map of std.algorithms of D 2.x, and the tools by downs, etc.

I just wanted to know if any of those would actually work in D.  My nasty method isn't the best by any stretch of the imagination, and aside from boning back up on regular expressions I couldn't think of a better solution.

If there's a better solution, that is awesome.  I didn't recognize anything as D, and I didn't know if the last line was for D (whether for a library I do know/have or not).

> >I just didn't feel like writing a shell sort by insertion (which I have found to be the fastest for this particular data set) just then.<
> 
> Probably there are other ways to improve that (a priority queue? Fibonacci Heaps? etc), but fastSort is already written...

Perhaps.  I liked the shell sort by data exchange because it was an in-place sort, which makes it safer for larger data sets.

> >I agree that the Perl version is more readable.  I just needed something in D, since I don't know Perl, nor do I have a Perl interpreter handy.  My D version also can handle variable sized boards, which the Perl version cannot.<
> 
> I think a better-looking (and higher-level) D version can be written.

I don't disagree.  I just wanted something that worked, since having lots of tables of numbers on the site was really bland.  The images look a lot better, and help illustrate things far more effectively.

One of the things I'd like to do to the SVG maker is to change the worker function that actually makes a string that describes an SVG is to modify it to accept an array of locations as an array of locations (the struct).  That would remove the necessity of re-parsing the text to make the circle and the ending arrow.  I added the circle and arrow last, so I wasn't really thinking all the way through.  It does beg another iteration.

The original parser could also use another do-over.  It just feels a bit too fault-prone.  I haven't stress-tested it with malformed input to see how it will react, and it could certainly be a lot better.

Specifically I was in want of a C-like scanf function for the board header.

scanf("found solution for %d %d in %d steps\n", x, y, steps);

It would have been much better.  I was attempting to avoid the use of tango.stdc libraries in the spirit of SafeD.  For the life of me I couldn't find a simple exit() function, so I was forced to import stdlib.  I'm still not sure if Tango has one, though honestly I haven't continued my search.

> >I did not feel like learning enough Perl to make it handle variable sized boards, either.<
> 
> I see, then you can translate that Perl code to Python instead to D ;-)

Yeah, but the D version was more fun to write.  In addition, I'm not familiar with Python, and I'd have to make a significant investment in time to locate reliable documentation and to get myself up to speed with a working understanding of how to make things tick in Python.  Not an option, given that I'm under a lot of workload in school right now.  I was sick and missed three days.  It feels like I missed most of the quarter though.



More information about the Digitalmars-d mailing list