只想澄清这是我编程的第一天,我意识到这个问题是多么愚蠢:D
1)为什么不起作用?
ghci>''hello'' ++ ''world''
<interactive>:40:1: error
* Syntax error on ''hello''
Perhaps you intended to use TemplateHaskell or TemplateHaskellQuotes
* In the Template Haskell quotation ''hello''
ghci>''hello'' ++ '' '' ++ ''world''
<interactive>:41:17: error: parse error on input '''
Run Code Online (Sandbox Code Playgroud)
我应该在texteditor中添加一些内容以使其正常工作吗?或者我只是做了一些菜鸟错误?
2)
ghci> ''Steve'' !! 2
<interactive>:42:2: error
* Syntax error on ''Steve''
Perhaps you intended to use TemplateHaskell or TemplateHaskellQuotes
* In the Template Haskell quotation ''Steve''
ghci>[1,2,3,5,8] !! 2
3
Run Code Online (Sandbox Code Playgroud)
当我使用数字执行这些命令时,它可以工作,但不能使用字符.我一定做错了什么,但我不确定是什么:/
提前致谢!
完成以下函数定义:
-- maxi x y returns the maximum of x and y
Run Code Online (Sandbox Code Playgroud)
我应该在不使用Haskell中的'maximum'函数的情况下完成此任务.
maxi :: Integer -> Integer -> Integer
maxi x y
|x > y = x
|y > x = y
Run Code Online (Sandbox Code Playgroud)
然后我不明白该怎么做.如何测试代码是否有效?它看起来有点正确吗?
我正在尝试在Haskell中创建一个代码,当k是偶数时,n ^ k =(n*n)^(k/2),如果k,则n ^ k = n*(n ^(k-1))很奇怪.我做错了什么,但我无法弄清楚如何解决它.
power2 :: Integer -> Integer -> Integer
power2 n 0 = 1
power2 n k
| k < 0 = error "negative argument"
| k `mod` 2 == 0 = even -- Am I supposed to write = even here?
| otherwise = odd
if k even = (n 2) ( div k 2) -- (n^2)^(k/2) ???
if k odd = n * (n) (k-1)) -- Is this n^(k-1) ???
Run Code Online (Sandbox Code Playgroud) 我对Haskell(和编程)很陌生.我的任务是定义一个功能,返回一整套卡(又名52卡).试图将我的思维过程包含在代码旁边的注释中.
- 我定义的值
data Suit = Club | Diamond | Heart | Spade deriving (Show, Enum)
data Value = Two | Three | Four | Five | Six | Seven
| Eight | Nine | Ten | Jack | Queen
| King | Ace deriving (Show, Enum)
type Card = (Suit, Value) -- A card must have a suit and a value
type Deck = [Card] -- A deck consists of a list of cards
fullDeck :: Deck -- My …Run Code Online (Sandbox Code Playgroud) haskell ×4