繁体   English   中英

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

[英]How to combine the letters in two strings in haskell

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

谢谢

++仅适用于列表,但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"]
x ++ ' ' ++ y

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

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

     > [[x] ++ " " ++ [y] | x <- "ab", y <- "cd"] ["ac","ad","bc","bd"] 

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

  2. 或者,将y转换为String,使用带有' ' cons运算符,并将其连接到x转换为字符串,就像这样

     > [[x] ++ (' ' : [y]) | x <- "ab", y <- "cd"] ["ac","ad","bc","bd"] 
  3. 或者, 如chi所建议的那样 ,甚至更简单和直观地创建一个字符列表,就像这样

     > [[x, ' ', y] | x <- "ab", y <- "cd"] ["ac","ad","bc","bd"] 

注意:[]包装一个字符使其成为一个只包含一个字符的字符列表。 它基本上变成了一个String。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM