【发布时间】:2014-05-17 09:13:14
【问题描述】:
在real world haskell的chapter 3的Incorrectly matching a variable部分,有一个例子如下:
-- file: ch03/BogusPattern.hs
data Fruit = Apple | Orange
apple = "apple"
orange = "orange"
whichFruit :: String -> Fruit
whichFruit f = case f of
apple -> Apple
orange -> Orange
解释在case f of 部分中说,apple 和orange 不被视为函数声明之前定义的全局变量。它们是局部变量。我认为如果没有与全局变量同名的局部变量,则全局变量不会隐藏。
【问题讨论】:
-
“
apple和orange不被视为String类型”这是不正确的,RWH 也没有声称它。全局和本地apple都有String类型,只是不是相同的字符串。
标签: haskell global-variables shadowing