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 = []
  where
    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.

Thank You,
Praveen

On 06/08/2014 02:33 AM, Francesco Ariis wrote:
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








_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners