【发布时间】:2016-08-31 01:57:23
【问题描述】:
我不明白为什么以下练习在 Haskell Programming from First Principles 中“有效”:
type Subject = String
type Verb = String
type Object = String
data Sentence =
Sentence Subject Verb Object
deriving (Eq, Show)
s1 = Sentence "dogs" "drool"
s2 = Sentence "Julie" "loves" "dogs"
将其加载到 ghci 中表明它的类型检查很好,但为什么 s1 的定义甚至有意义?我对 Haskell 还是很陌生,所以起初我认为这是因为在 s1 Haskell 中隐含地让 Object 字符串为空。但是后来……
*Main> s1
<interactive>:13:1:
No instance for (Show (Object -> Sentence))
arising from a use of `print'
Possible fix:
add an instance declaration for (Show (Object -> Sentence))
In a stmt of an interactive GHCi command: print it
我仍在学习如何正确解释这些错误消息,所以请多多包涵。但是有人能解释一下No instance for (Show (Object -> Sentence)) 是什么意思吗?更具体地说,在s1 中省略Object 字符串如何导致(Object -> Sentence) 事情?
我确信这很简单,但我不认为这本书已经让我能够理解这一点......
【问题讨论】:
-
查找函数柯里化并记住像
Sentence这样的数据构造函数的处理方式与常规函数非常相似。
标签: haskell currying partial-application