[Haskell-beginners] Lions, Wolves and Goats
Francesco Ariis
fa-ml at ariis.it
Sun Jun 8 06:33:56 UTC 2014
On Sat, Jun 07, 2014 at 08:04:09PM -0400, Elric wrote:
> Hi,
>
> I came across this article: http://unriskinsight.blogspot.co.at/2014/06/fast-functional-goats-lions-and-wolves.html
> a couple of days ago. This compares performance of solving a problem
> (which I will get to) using the functional constructs alone in
> languages like C++11 and Java 8.
> Since, Haskell is my first foray into FP, I thought I should try
> solving this in Haskell.
>
Hello Elric,
I gave a go at the problem, managed to get a result (23).
I attach the .hs file (not my best Haskell, but hopefully clear enough).
The crucial point in my solution lies in this lines:
carnage :: [Forest] -> [Forest]
let wodup = nub aa in
-- etc. etc.
Which means after every iteration I call |nub| on my list of possible
states; nub is a function from |Data.List| and removes duplicate
elements from a list.
If I omit that nub call, the program doesn't reach a solution (as it
is computationally quite inefficient). I think that's the problem
with your versions.
Let me know if this helps
-------------- next part --------------
A non-text attachment was scrubbed...
Name: carnage.hs
Type: text/x-haskell
Size: 1226 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140608/e4e52128/attachment-0001.hs>
More information about the Beginners
mailing list