list - Haskell List With Guards错误

标签 list haskell guard list-comprehension take

我正在尝试在 haskell 中编写一个非常简单的函数来根据输入更改列表中的值,如下所示

update_game :: [Int] -> Int -> Int -> [Int]
update_game (x:xs) row take_amnt | row == 1 = x - take_amnt:xs
                                 | row == 2 = x : head(xs) - take_amnt : tail(xs)
                                 | row == 3 = x : head(xs) : last(xs) - take_amnt`

前两个案例工作正常,但是最后一个案例给我带来了问题,我不确定为什么,我得到的错误是:

http://i.stack.imgur.com/jpT8b.png

http://i.stack.imgur.com/tlz5t.png

最佳答案

: 的第二个参数应该是一个列表,但是 last(xs) - take_amnt 显然只产生一个元素。尝试

row == 3 = x : head(xs) : [last(xs) - take_amnt]

关于list - Haskell List With Guards错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13921676/

相关文章:

c# - 如何在不获取 NULLREFERENCEEXCEPTION 的情况下添加到列表?

haskell - 快速排序的比较次数

ruby-on-rails - 如何在文件中写入 guard-rubocop 结果?

ruby-on-rails - 守卫找不到 libnotify 也无法禁用通知但抛出错误

ruby - 如何禁用 Guard (Ruby) 中的通知?

c# - 使用 Linq 对 List<T>[] c# 进行排序或排序

python - 如何返回列表中出现次数最多的元素?

python - 如何从比值列表长的键列表中制作字典

haskell - 无法将预期类型 ‘[Integer]’ 与实际类型 ‘Integer’ 匹配

haskell - 使用模板 Haskell 获取关联类型同义词