如何在haskell中组合两个字符串中的字母

时间:2015-02-14 06:49:50

标签: string haskell char list-comprehension

我正在学习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 ...我怎样才能使其工作?

由于

2 个答案:

答案 0 :(得分:9)

++仅适用于列表,但xy仅适用于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

这里的实际问题是,您尝试连接三个字符,并且只为项目列表定义一个函数。 ++实际上会连接两个列表,而不是两个单独的项目并列出一个列表。

  1. 因此,您可以通过将所有字符转换为字符串来修复程序,例如

    > [[x] ++ " " ++ [y] | x <- "ab", y <- "cd"]
    ["a c","a d","b c","b d"]
    

    请注意" ",而不是' '。因为" "表示只包含空格字符的字符串,但' '仅表示空格字符。

  2. 或者,将y转换为字符串,将cons运算符与' '一起使用,并将其连接到x转换为字符串,就像这样< / p>

    > [[x] ++ (' ' : [y]) | x <- "ab", y <- "cd"]
    ["a c","a d","b c","b d"]
    
  3. 或者,更简单直观的as suggested by chi,创建一个字符列表,就像这样

    > [[x, ' ', y] | x <- "ab", y <- "cd"]
    ["a c","a d","b c","b d"]
    
  4. 注意:[]包裹一个字符会使其成为一个只包含一个字符的字符列表。它基本上变成了一个String。