- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做“PureScript by example”一书中的作业,使用递归来计算数组中偶数项的数量。
这是我写的代码
isEven :: Int -> Boolean
isEven 0 = true
isEven 1 = false
isEven n = isEven(n - 2)
countEven :: List Int -> Int
countEven list =
if null list
then 0
else
if isEven(unsafePartial (head list))
then 1
else 0 + countEven(unsafePartial (tail list))
我收到一条错误消息说
Error found:
in module Main
at src/Main.purs line 74, column 17 - line 74, column 42
Could not match type
Maybe Int
with type
Int
while checking that type t0
is at least as general as type Int
while checking that expression unsafePartial (head list)
has type Int
in binding group countEven
对此我有点意外,因为unsafePartial head list
的数据类型是Int,unsafePartial tail list
是List Int
那么为什么感觉某处有一个 Maybe?
最佳答案
您收到此错误的事实意味着您正在使用 head
和 tail
来自 Data.List
,它们实际上不是部分的,而是返回一个 Maybe
。
要使用它们的部分副本,请从 Data.List.Partial
导入它们.
关于PureScript 无法将 Maybe Int 类型与 Int 类型匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48779739/
我有一个使用 HsOpenSsl 的 readPrivateKey 函数读取 Rsa key 的函数,不幸的是,我的函数的签名是这个 String -> IO (Maybe (IO Maybe Rsa
我有一个类型 data Foo = Foo { bar :: Bar, baz :: Baz } 我正在尝试解析文件以构造 Foo .我有函数可以尝试解析 Bar和 Baz成员(member) par
我有一个可能的列表和一个给我节点颜色的函数(如果存在): maybeNeighbors :: [Maybe Node] nodeColor :: Node -> Maybe Color 现在
我有一个可能的列表和一个给我节点颜色的函数(如果存在): maybeNeighbors :: [Maybe Node] nodeColor :: Node -> Maybe Color 现在
我想有一个类型的函数: f :: [Maybe a] -> Maybe [a] 例如 f [Just 3, Just 5] == Just [3, 5] f [Just 3, Nothing] ==
我刚开始学习 Haskell,我发现自己正在为家庭作业编写这段代码: eval :: ExprT -> Integer eval (Lit x) = x eval (Add x y) = (e
我有一个类型为 Integral => a -> a -> Maybe a 的函数 f。这意味着 f 1 2 是有效的代码,但是自然的组合,例如 f (f 3 4) 5 f 6 (f 7 8) f (
我刚开始学习 Haskell,我发现自己正在为家庭作业编写这段代码: eval :: ExprT -> Integer eval (Lit x) = x eval (Add x y) = (e
是否可以轻松(无需重新发明轮子)从 (Maybe a, b) 转换至 Maybe (a,b) .我看了Traversable但不能让它工作。 最佳答案 您也可以使用 Bitraversable : b
我正在处理一个库( ghcjs-dom ),其中每个函数都返回一个 IO (Maybe T) . 我有一个函数a with 返回 IO (Maybe x)和功能 b需要x作为参数并返回 IO (May
我想计算列表的两个 elemIndex 值的差值。 colours = ["blue", "red", "green", "yellow"] ib = elemIndex "blue" colours
有一个标准tryPick如果 F# 返回第一个(从左到右,如果有的话)函数在列表元素上成功应用的函数。我希望在 Haskell 中有一个像这样的标准函数。我 tried胡歌,没有找到任何东西。 我是
我想将 Gen (Maybe Int) 转换为 Maybe Int。我有一个生成 1 到 9 之间的随机 Just Int 的函数。我想使用单元格函数,但我无法更改其类型签名中的任何内容。有什么建议吗
我想将以下回调代码包装到 Maybe public class FooDataSource ... public Maybe get(){ return Maybe.create(emitte
我想将 Gen (Maybe Int) 转换为 Maybe Int。我有一个生成 1 到 9 之间的随机 Just Int 的函数。我想使用单元格函数,但我无法更改其类型签名中的任何内容。有什么建议吗
这可能是一件简单的事情,但我正在编写一个想要返回记录的函数,并且其中几个字段需要执行可能失败的操作,所以现在结果记录看起来像这样(假设它是类型别名为 MyRecord)。 { field1 : May
为什么流程会在这里抛出错误? type MaybeProp = { prop: ?number, } type DefinetlyProp = { prop: number, } const
我想建立一个 Repository返回 Single 的类. 该类应首先查看 Cache返回 Maybe如果 Maybe完成转到我的Service返回 Single interface Cache {
使用 Typescipt 4.x.x 我写了一些代码来实现其他语言 Elm/Rust/Haskell 中常用的 Maybe/Option 类型。 我想写一个可以接受映射类型的通用函数 type MyM
在 Elm 中,将 List (Maybe a) 转换为 Maybe (List a) 的好方法是什么? 逻辑很简单: 如果所有项都是Just a,则返回Just (List a) 否则,返回Noth
我是一名优秀的程序员,十分优秀!