【发布时间】:2019-11-04 07:07:38
【问题描述】:
我正在努力完成以下工作:
slice :: Int -> Int -> [a] -> [a]
slice from to xs = take (to - from + 1) (drop from xs)
trimBoard :: Board -> Int -> Board
trimBoard s y = slice ((y*3)) (((y+1)*3)-1) s
getBox :: Board -> Int -> Int -> [Sequence]
getBox s y x = [ (trimBoard c x) | c <- (trimBoard s y)]
具体来说,我正在尝试运行一个函数,获取结果[[int]],然后将另一个函数映射到该结果上。哪个 haskell 显然令人厌恶,我需要结合使用“lambda 函数”和其他我根本无法阅读或理解的魔法来实现这一点。
有什么简单的方法可以做到这一点,不需要 7 个月的数学函数语法课程?
如上所述,结果board是[[int]],sequence就是[int]。
它产生的错误是
sudoku.hs:139:19: error:
• Couldn't match type ‘[Int]’ with ‘Int’
Expected type: Sequence
Actual type: Board
• In the expression: (trimBoard c x)
In the expression: [(trimBoard c x) | c <- (trimBoard s y)]
In an equation for ‘getBox’:
getBox s y x = [(trimBoard c x) | c <- (trimBoard s y)]
sudoku.hs:139:29: error:
• Couldn't match type ‘Int’ with ‘[Int]’
Expected type: Board
Actual type: Sequence
• In the first argument of ‘trimBoard’, namely ‘c’
In the expression: (trimBoard c x)
In the expression: [(trimBoard c x) | c <- (trimBoard s y)] Failed, modules loaded: none.
【问题讨论】:
-
除非
Board是Sequence的同义词,否则您的类型不会一致。 -
getBox s y x = [ (trimBoard c x) | c <- (trimBoard s y)]被 ghc 拒绝,出现大量我不理解的错误。我认为他们抱怨 x 应该是一个 int,但不知何故变成了一个 [int],但我不确定。 -
请编辑您的问题并添加错误。还要添加
Board和Sequence类型的定义。这是为了未来的读者和可搜索性。 -
c在表达式c <- (trimBoard s y)的类型为[Int],但trimBoard的第一个参数需要类型Board,即[[Int]],不匹配。另外,getBox终于有类型[Board]而不是[Sequence]`。 -
@RNPF,列表理解
[ ... | c <- trimBoard... ]表示对于每个元素c的列表trimBoard。所以c具有您要绑定到的任何列表的 元素类型。
标签: haskell