- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Bool isEven x =-6ren">
我是一名 C++/Java 程序员,我正在尝试学习 Haskell(以及一般的函数式编程),而且我一直在努力学习。我尝试过的一件事是:
isEven :: Int -> Bool
isEven x =
if mod x 2 == 0 then True
else False
isOdd :: Int -> Bool
isOdd x =
not (isEven x)
main =
print (isEven 2)
print (isOdd 2)
ghc --make doubler.hs -o Main
[1 of 1] Compiling Main ( doubler.hs, doubler.o )
doubler.hs:11:5: error:
• Couldn't match expected type ‘(a0 -> IO ()) -> Bool -> t’
with actual type ‘IO ()’
• The function ‘print’ is applied to three arguments,
but its type ‘Bool -> IO ()’ has only one
In the expression: print (isEven 2) print (isOdd 2)
In an equation for ‘main’: main = print (isEven 2) print (isOdd 2)
• Relevant bindings include main :: t (bound at doubler.hs:10:1)
make: *** [all] Error 1
isEven :: Int -> Bool
isEven x =
if mod x 2 == 0 then True
else False
isOdd :: Int -> Bool
isOdd x =
not (isEven x)
main = do
print (isEven 2)
print (isOdd 2)
最佳答案
Why doesn't the first code snippet work?
do
之外 block ,换行符没有任何意义。所以你对
main
的第一个定义相当于
main = print (isEven 2) print (isOdd 2)
,失败是因为
print
只接受一个论点。
print
工作吗?
print
是一个接受字符串并产生
IO ()
类型结果的函数.
IO
是一种表示可能产生副作用的操作的类型。
main
生成此类型的值,然后将执行该值描述的操作。虽然没有一个接一个地调用函数的有意义的概念,但是一个接一个地执行一个 IO 值的操作是一个有意义的概念。为此,我们使用
>>
运算符,它将两个 IO 值链接在一起。
I saw something about "monads" on the internet related to the "do" keyword, does that have something to do with this?
Monad
是一个类型类(如果你还不知道它们是什么:它们类似于 OO 语言中的接口(interface)),它(除其他外)提供函数
>>
和
>>=
.
IO
是该类型类的一个实例(在 OO 术语中:一种实现该接口(interface)的类型),它使用这些方法将多个操作链接在一起。
do
语法是使用
>>
的更方便的方式和
>>=
.具体来说,您对 main 的定义等同于以下没有
do
:
main = (print (isEven 2)) >> (print (isOdd 2))
main
产生一个执行
print (isEven 2)
步骤的IO值,其次是
print (isOdd 2)
.
关于haskell - Haskell 的 "do"关键字有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40832232/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!