[Haskell-beginners] Lions, Wolves and Goats
elric at kiosa.org
Fri Jun 13 15:37:11 UTC 2014
Thank You Ariis,
I was using nub in a wrong way, like so:
meal :: Forest -> [Forest]
meal  = 
meal f@[Lion l, Wolf w, Goat g]
| endState f = 
| l == 0 = [f] ++ weg
| w == 0 = [f] ++ leg
| g == 0 = [f] ++ lew
| (l /= 0) && (w /= 0) && (g /= 0) = [f] ++ leg ++ lew ++ weg
| otherwise = 
leg = nub $ meal $ ionEatGoat f
lew = nub $ meal $ lionEatWolf f
weg = nub $ meal $ wolfEatGoat f
After looking at your solution, I realized I was essentially generating
every possible state, and THEN trying to remove the duplicates, whereas
in your solution at each step you remove possible duplicates states of
the forest and propagate to the next step only from there.
On 06/08/2014 02:33 AM, Francesco Ariis wrote:
> On Sat, Jun 07, 2014 at 08:04:09PM -0400, Elric wrote:
>> 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
> Beginners mailing list
> Beginners at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners