Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell:预期类型:[Char]实际类型:[[Char]]_Haskell - Fatal编程技术网

Haskell:预期类型:[Char]实际类型:[[Char]]

Haskell:预期类型:[Char]实际类型:[[Char]],haskell,Haskell,我最近开始学习Haskell,在我的程序中,我发现以下错误: Couldn't match type ‘[Char]’ with ‘Char’ Expected type: [Char] Actual type: [[Char]] In the first argument of ‘head’, namely ‘input’ In the first argument of ‘magi’, namely ‘(head input)’ 我的代码如下所示: vocals = ["a",

我最近开始学习Haskell,在我的程序中,我发现以下错误:

    Couldn't match type ‘[Char]’ with ‘Char’
Expected type: [Char]
  Actual type: [[Char]]
In the first argument of ‘head’, namely ‘input’
In the first argument of ‘magi’, namely ‘(head input)’
我的代码如下所示:

vocals = ["a", "e", "i", "o", "u","y"]
vocal o
  | elem o vocals == True = True --return true if the letter is a vowel
  | elem o vocals == False = False --else false

magi konsonant = [konsonant] ++ "o" ++ [konsonant]

rovarsprak input
  |length input == 0 = ""
  |length input > 0 && vocal (head input) == False = magi (head input) ++ rovarsprak (tail input)
  |length input > 0 && vocal (head input) == True = head input : rovarsprak (tail input)
据我所知,之所以出现错误,是因为我对head函数的输入是[[char]],而不是[char],但我不明白为什么head的输入是[[char]]。
谢谢

问题在于,
人声
具有以下类型:

vocal :: String -> Bool
或更短:

vocals = "aeiouy"
也就是说,你的代码相当混乱。我们可以将其改写为:

vocals :: [Char]
vocals = "aeiouy"

vocal :: Char -> Bool
vocal = flip elem vocals                   -- pointfree function

magi :: Char -> String
magi konsonant = konsonant : 'o' : [konsonant]

rovarsprak :: String -> String
rovarsprak "" = ""                         -- use patterns
rovarsprak (h:t)                           -- use patterns to unpack
    | vocal h = h : rovarsprak t           -- == True is not necessary
    | otherwise = magi h ++ rovarsprak t   -- use otherwise
vocal=flip elem vocals
的工作原理如下:
flip
将函数
f
作为输入,该函数包含两个参数
x
y
。然后将其转换为一个函数,该函数接受两个参数
y
x
(因此参数被翻转)


我们想要的是调用
elem o vocals
。这相当于
翻转元素人声o
。现在通过使用eta缩减,我们可以省略
o
(在子句的头部和正文中)。

问题在于
vocal
具有类型:

vocal :: String -> Bool
或更短:

vocals = "aeiouy"
也就是说,你的代码相当混乱。我们可以将其改写为:

vocals :: [Char]
vocals = "aeiouy"

vocal :: Char -> Bool
vocal = flip elem vocals                   -- pointfree function

magi :: Char -> String
magi konsonant = konsonant : 'o' : [konsonant]

rovarsprak :: String -> String
rovarsprak "" = ""                         -- use patterns
rovarsprak (h:t)                           -- use patterns to unpack
    | vocal h = h : rovarsprak t           -- == True is not necessary
    | otherwise = magi h ++ rovarsprak t   -- use otherwise
vocal=flip elem vocals
的工作原理如下:
flip
将函数
f
作为输入,该函数包含两个参数
x
y
。然后将其转换为一个函数,该函数接受两个参数
y
x
(因此参数被翻转)


我们想要的是调用
elem o vocals
。这相当于
翻转元素人声o
。现在,通过使用eta缩减,我们可以省略
o
(在条款的开头和正文中)。

谢谢您的回答!但是我不太清楚这句话是什么意思?谢谢你的回答!但是我不太清楚这行
vocal=flip elem vocals
有什么作用?