我正在学习Haskell并遵循http://learnyouahaskell.com/starting-out上的指南。我正处于显示的位置:
ghci> let nouns = ["hobo","frog","pope"]
ghci> let adjectives = ["lazy","grouchy","scheming"]
ghci> [adjective ++ " " ++ noun | adjective <- adjectives, noun <- nouns]
["lazy hobo","lazy frog","lazy pope","grouchy hobo","grouchy frog",
"grouchy pope","scheming hobo","scheming frog","scheming pope"]
我想要实现的,它是类似的,但结合两个字符串中包含的字母,因为字符串基本上是Haskell中的char列表,这就是我尝试过的:
[x ++ ' ' ++ y | x <- "ab", y <- "cd"]
但编译器抱怨:
Prelude> [y ++ ' ' ++ y | x <- "abd", y <- "bcd"]
<interactive>:50:2:
Couldn't match expected type ‘[a]’ with actual type ‘Char’
Relevant bindings include it :: [[a]] (bound at <interactive>:50:1)
In the first argument of ‘(++)’, namely ‘y’
In the expression: y ++ ' ' ++ y
<interactive>:50:7:
Couldn't match expected type ‘[a]’ with actual type ‘Char’
Relevant bindings include it :: [[a]] (bound at <interactive>:50:1)
In the first argument of ‘(++)’, namely ‘' '’
In the second argument of ‘(++)’, namely ‘' ' ++ y’
In the expression: y ++ ' ' ++ y
<interactive>:50:14:
Couldn't match expected type ‘[a]’ with actual type ‘Char’
Relevant bindings include it :: [[a]] (bound at <interactive>:50:1)
In the second argument of ‘(++)’, namely ‘y’
In the second argument of ‘(++)’, namely ‘' ' ++ y’
我做了很多尝试,例如将表达式包装在括号中以获取列表,将空格更改为String而不是char ...我怎样才能使其工作?
由于
答案 0 :(得分:9)
++
仅适用于列表,但x
和y
仅适用于Char
。毕竟,它们是来自String
(= [Char]
)的元素,而LYAH示例包含Char
列表:[String] = [[Char]]
:
-- [a] -> [a] -> [a]
-- vv vv
[y ++ ' ' ++ y | x <- "abd", y <- "bcd"]
-- ^ ^ ^
-- Char Char
-- vs
-- [String] [String]
-- vvvvvvvvvv vvvvv
[adjective ++ " " ++ noun | adjective <- adjectives, noun <- nouns]
-- ^^^^^^^ ^^^^
-- String String
相反,使用(:)
将彼此的字符合并到空列表中:
[x : ' ' : y : [] | x <- "abd", y <- "bcd"]
答案 1 :(得分:5)
x ++ ' ' ++ y
这里的实际问题是,您尝试连接三个字符,并且只为项目列表定义一个函数。
++
实际上会连接两个列表,而不是两个单独的项目并列出一个列表。
因此,您可以通过将所有字符转换为字符串来修复程序,例如
> [[x] ++ " " ++ [y] | x <- "ab", y <- "cd"]
["a c","a d","b c","b d"]
请注意" "
,而不是' '
。因为" "
表示只包含空格字符的字符串,但' '
仅表示空格字符。
或者,将y
转换为字符串,将cons
运算符与' '
一起使用,并将其连接到x
转换为字符串,就像这样< / p>
> [[x] ++ (' ' : [y]) | x <- "ab", y <- "cd"]
["a c","a d","b c","b d"]
或者,更简单直观的as suggested by chi,创建一个字符列表,就像这样
> [[x, ' ', y] | x <- "ab", y <- "cd"]
["a c","a d","b c","b d"]
注意:用[]
包裹一个字符会使其成为一个只包含一个字符的字符列表。它基本上变成了一个String。