在我的Haskell代码中找不到错误

时间:2011-07-05 09:02:11

标签: haskell river-crossing-puzzle

我尝试将卷心菜 - 山羊 - 狼谜的解决方案从Scala转换为Haskell,但是在head中调用findSolutions时代码抛出并出错,因为解决方案列表是空的,所以问题似乎在某个循环中。 findMoves似乎工作正常。

import Data.Maybe(fromMaybe)

data Item = Farmer | Cabbage | Goat | Wolf deriving (Eq, Show)

type Position = ([Item], [Item]) 

validPos :: Position -> Bool
validPos p = valid (fst p) && valid (snd p) where   
   valid list = elem Farmer list || notElem Goat list || 
                (notElem Cabbage list && notElem Wolf list) 

findMoves :: Position -> [Position]
findMoves (left,right) = filter validPos moves where
    moves | elem Farmer left = map (\item -> (delItem item left, addItem item right)) left 
          | otherwise = map (\item -> (addItem item left, delItem item right)) right
    delItem item = filter (\i ->  notElem i [item, Farmer]) 
    addItem Farmer list = Farmer:list      
    addItem item list = Farmer:item:list      

findSolution :: Position -> Position -> [Position]
findSolution from to = head $ loop [[from]] where
    loop pps = do
          (p:ps) <- pps
          let moves = filter (\x -> notElem x (p:ps)) $ findMoves p
          if elem to moves then return $ reverse (to:p:ps)
                           else loop $ map (:p:ps) moves  

solve :: [Position]
solve = let all = [Farmer, Cabbage, Goat, Wolf]
        in findSolution (all,[]) ([],all)

当然,我也很欣赏有关与实际错误无关的改进的提示。

[更新]

为了记录,我按照建议使用了Set。这是工作代码:

import Data.Set

data Item = Farmer | Cabbage | Goat | Wolf deriving (Eq, Ord, Show)

type Position = (Set Item, Set Item)

validPos :: Position -> Bool
validPos p = valid (fst p) && valid (snd p) where
   valid set = or [Farmer `member` set, Goat `notMember` set, 
                   Cabbage `notMember` set && Wolf `notMember` set]

findMoves :: Position -> [Position]
findMoves (left,right) = elems $ Data.Set.filter validPos moves where
    moves | Farmer `member` left = Data.Set.map (move delItem addItem) left
          | otherwise = Data.Set.map (move addItem delItem) right
    move f1 f2 item = (f1 item left, f2 item right)
    delItem item = delete Farmer . delete item 
    addItem item = insert Farmer . insert item 

findSolution :: Position -> Position -> [Position]
findSolution from to = head $ loop [[from]] where
    loop pps = do
          ps <- pps
          let moves = Prelude.filter (\x -> notElem x ps) $ findMoves $ head ps
          if to `elem` moves then return $ reverse $ to:ps
                             else loop $ fmap (:ps) moves

solve :: [Position]
solve = let all = fromList [Farmer, Cabbage, Goat, Wolf]
        in findSolution (all, empty) (empty, all)

head中对findSolution的调用可以更安全,应该使用更好的方法来打印解决方案,但除此之外,我对此非常满意。

[更新2]

我认为这些职位的前任陈述对于这类问题并不是最理想的。我切换到以下数据模型,这使得移动等稍微冗长,但很多更具可读性:

data Place = Here | There deriving (Eq, Show)

data Pos = Pos { cabbage :: Place
               , goat :: Place
               , wolf :: Place
               , farmer :: Place 
               } deriving (Eq, Show)

1 个答案:

答案 0 :(得分:4)

问题是[Farmer,Goat,Cabbage,Wolf][Farmer,Cabbage,Goat,Wolf]不同,使用elemnotElem时不会检查findMoves。一种解决方案总是对元素列表进行排序,例如在您可以使用的函数import Data.List(ord) import Control.Arrow((***)) data Item = Farmer | Cabbage | Goat | Wolf deriving (Eq, Show, Ord) findMoves (left,right) = map (sort***sort) $ filter validPos moves where -- .... solve = let all = sort [Farmer, Cabbage, Goat, Wolf] -- .... 中:

{{1}}

或者您可以使用一组Item而不是Item列表。