Here's another approach that more closely models what's going on in the C++ version. I defined an ordNub rather than using nub as nub is O(n^2) as it only requires Eq.
import qualified Data.Set as S
import Data.List (partition)
import System.Environment (getArgs)
data LWG = LWG { _lion, _wolf, _goat :: {-# UNPACK #-} !Int }
deriving (Show, Ord, Eq)
lionEatGoat, lionEatWolf, wolfEatGoat :: LWG -> LWG
lionEatGoat (LWG l w g) = LWG (l - 1) (w + 1) (g - 1)
lionEatWolf (LWG l w g) = LWG (l - 1) (w - 1) (g + 1)
wolfEatGoat (LWG l w g) = LWG (l + 1) (w - 1) (g - 1)
stableState :: LWG -> Bool
stableState (LWG l w g) = length (filter (==0) [l, w, g]) >= 2
validState :: LWG -> Bool
validState (LWG l w g) = all (>=0) [l, w, g]
possibleMeals :: LWG -> [LWG]
possibleMeals state =
filter validState .
map ($ state) $ [lionEatGoat, lionEatWolf, wolfEatGoat]
ordNub :: Ord a => [a] -> [a]
ordNub = S.toList . S.fromList
endStates :: [LWG] -> [LWG]
endStates states
| not (null stable) = stable
| not (null unstable) = endStates (concatMap possibleMeals unstable)
| otherwise = []
where (stable, unstable) = partition stableState (ordNub states)
main :: IO ()
main = do
[l, w, g] <- map read `fmap` getArgs
mapM_ print . endStates $ [LWG l w g]