简体   繁体   English

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

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

I am learning Haskell and following the guide on http://learnyouahaskell.com/starting-out . 我正在学习Haskell并遵循http://learnyouahaskell.com/starting-out上的指南。 I am at the point where it is shown: 我正处于显示的位置:

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"]   

What I'd like to achieve, it is something similar, but combining the letters contained in two strings, and since strings are basically lists of char in Haskell, this is what I tried: 我想要实现的,它是类似的,但结合两个字符串中包含的字母,因为字符串基本上是Haskell中的char列表,​​这是我尝试的:

 [x ++ ' ' ++ y | x <- "ab", y <- "cd"]

But the compiler is complaining: 但编译器抱怨:

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’

I did a number of tries, such as wrapping the expression in brackets to get a list, changed the space to be a String rather than a char... How can I get it working? 我做了很多尝试,例如将表达式包装在括号中以获取列表,将空间更改为String而不是char ...我怎样才能使它工作?

Thanks 谢谢

++ works only for lists, but x and y are only Char . ++仅适用于列表,但xy仅为Char After all, they're elements from a String (= [Char] ), whereas the LYAH example had lists of lists of Char : [String] = [[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

Instead, use (:) to cons the characters on each other and onto the empty list: 相反,使用(:)将彼此的字符合并到空列表中:

[x : ' ' : y : [] | x <- "abd", y <- "bcd"]
x ++ ' ' ++ y

The actual problem here is, you are trying to concatenate three characters, with a function defined only for list of items. 这里的实际问题是,您尝试连接三个字符,并且只为项目列表定义一个函数。 ++ will actually concatenate two lists, not two individual items and give a list. ++实际上会连接两个列表,而不是两个单独的项目并给出一个列表。

  1. So, you can fix your program either by converting all the characters to strings, like this 因此,您可以通过将所有字符转换为字符串来修复程序,就像这样

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

    Note the " " , not ' ' . 注意" " ,而不是' ' Because " " means a string with just a space character, but ' ' means just the space character. 因为" "表示只包含空格字符的字符串,但' '表示只是空格字符。

  2. Or, convert y to a String, use cons operator with the ' ' , and concatenate it to x converted to a string, like this 或者,将y转换为String,使用带有' ' cons运算符,并将其连接到x转换为字符串,就像这样

     > [[x] ++ (' ' : [y]) | x <- "ab", y <- "cd"] ["ac","ad","bc","bd"] 
  3. Or, even simpler and intutive, as suggested by chi , create a list of characters, like this 或者, 如chi所建议的那样 ,甚至更简单和直观地创建一个字符列表,就像这样

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

Note: Wrapping a character with [] makes it a list of characters with just one character in it. 注意:[]包装一个字符使其成为一个只包含一个字符的字符列表。 It basically becomes a String. 它基本上变成了一个String。

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

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