我在emacs文件中定义了以下函数(取自http://www.happylearnhaskelltutorial.com/1/shop_for_food_with_list.html#s9):
firstOnesOrEmpty :: [String] -> String
firstOnesOrEmpty [] = ""
firstOnesOrEmpty [x] = x
firstOnesOrEmpty (x:y:_) = x ++ ", " ++ y
但是当我将文件加载到GHCi并将:t firstOnesOrEmpty
写入GHCi时,我收到以下错误:
<interactive>:1:1: error: Variable not in scope: firstOnesOrEmpty
出了什么问题?
我的emacs文件中定义的另一个函数也存在类似的问题(同样来自上面的网站):
joinedWithCommas :: [String] -> String
joinedWithCommas [] = ""
joinedWithCommas [x] = x
joinedWithCommas (x:xs) = x ++ ", " ++ joinedWithCommas xs
尝试在GHCi中使用此功能我得到:
"ghci>" joinedWithCommas []
<interactive>:40:1: error:
Variable not in scope: joinedWithCommas :: [a0] -> t
"ghci>" joinedWithCommas [x]
<interactive>:41:1: error:
Variable not in scope: joinedWithCommas :: [a0] -> t
<interactive>:41:19: error: Variable not in scope: x
"ghci>" joinedWithCommas ["x"]
<interactive>:42:1: error:
Variable not in scope: joinedWithCommas :: [[Char]] -> t
我希望有人可以提供帮助。
我已经查看了有关此主题的问题的先前答案,但我无法看到它们如何为我的问题提供答案。
如果有人能指出我之前相关答案的方向并解释这个答案如何实际回答我的问题(我再说一遍,我不清楚),我将非常感激。
答案 0 :(得分:6)
要在GHCi中键入多行定义,您需要将其括在:{...:}
Prelude> :{
Prelude| joinedWithCommas :: [String] -> String
Prelude| joinedWithCommas [] = ""
Prelude| joinedWithCommas [x] = x
Prelude| joinedWithCommas (x:xs) = x ++ ", " ++ joinedWithCommas xs
Prelude| :}
Prelude> joinedWithCommas []
""
否则,每一行都是孤立处理的。