我声明我的数据是这样的:
data Op = Plus | Minus | Mul | Div | Pow
deriving (Eq, Show)
type Name = String
data Variable a = Variable Name (Expression a)
deriving (Eq, Show)
data Declaration a = Declaration (Variable a)
deriving (Eq, Show)
{- The core symbolic manipulation type -}
data Expression a =
Number a -- Simple number, such as 5
| Expression Op (Expression a) (Expression a)
deriving (Eq, Show)
Run Code Online (Sandbox Code Playgroud)
在GHCi中,我想通过键入来创建一个声明实例:
Declaration Variable "var1" 2+3
但它不起作用,我想这只是一个错误的语法,但我无法弄清楚如何.
另外我想知道什么时候需要使用实例?这是我从书中得到的代码:
instance Num a …Run Code Online (Sandbox Code Playgroud) 我想在Haskell中使用递归.我定义:
pf:: Int -> Int
pf 1 = 1
pf n = pf 1 + sum[pf 1..pf n-1]
Run Code Online (Sandbox Code Playgroud)
但总和不正确!总结一系列功能的正确方法是什么?
问题:翻译成"Pig Latin"的简单规则是采用以元音开头并添加"yay"的单词,同时接受任何与一个或多个辅音开始的单词并在附加之前将其转移到后面" AY".例如,"able"变为"ableyay","stripe"变为"ipestray".编写一个函数,将一串字母转换为Pig-Latin翻译.
执行:
-- define function to detect vowel
isVowel :: Char -> Bool
isVowel c = elem c ['u','e','o','a','i']
-- define function Latin Pig
lp ::String -> String
lp str = if (isVowel (head str)) then do {str ++ "yay"}
else
do {
str ++ (head str)
tail str
lp str
}
Run Code Online (Sandbox Code Playgroud)
问题:到目前为止,我没有看到我的代码(逻辑)有任何问题.老实说,这是我为Haskell课程介绍的作业.但编译器给我错误:
**Couldn't match expected type `t0 -> t1 -> t2 -> t3 -> [Char]'
with actual type `Char'
Expected type: [t0 -> t1 -> t2 -> …Run Code Online (Sandbox Code Playgroud)