[Haskell-beginners] Lions, Wolves and Goats

Ben Gamari ben at smart-cactus.org
Sun Jun 8 20:52:27 UTC 2014


Elric <elric at kiosa.org> writes:

> Hi,
> 
> Disclaimer: I have been learning Haskell for a month and there are still 
> several things about this wonderful language I know nothing of, so 
> please bear with me. Also, I apologize for this (somewhat) long mail./
>
...
>
> Below are the two versions of the code I came up with to solve this. 
> Neither of them converge to the 'endStates' even after about 15 minutes. 
> So there is definitely something wrong with what I have done. But after 
> banging my head on the keyboard for more then a day with this, I would 
> appreciate some pointers or help.
>
For one, you don't appear to be removing duplicates from the search set
resulting in a blow-up in your search space.

>
> I thought using the ADT was causing the performance issue and reverted 
> to using a plain 3-termed list which holds [Lion count, Wolf Count, 
> Sheep Count] :: [Int]
>
Your problem here isn't the use of ADTs, it's the use of lists. Why not
instead define a forest as follows?

    data Forest = Forest { wolfs, lions, goats :: !Int }

Note how I used a strictness annotation !Int here to ensure that the
compiler unboxes these members (at least with GHC >= 7.8), which is
almost certainly what you want in this case.

Anyways, I took a quick stab at the problem myself. My approach can be
found here[1]. Performance isn't great (a bit better than Javascript)
but then again the code is pretty much as naive as one could get. I'm
sure things could be improved.

Cheers,

- Ben


[1] https://gist.github.com/anonymous/e4a2ccd8df05255d5ed5
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 472 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140608/7cab5c33/attachment.sig>


More information about the Beginners mailing list