简体   繁体   English

在haskell中将字符串拆分为字符串

[英]split string into string in haskell

How can I split a string into another string without using Data.List.Split function? 如何在不使用Data.List.Split函数的情况下将一个string拆分为另一个string To be more concrete: to turn "Abc" into "['A','b','c']" 更具体地说:将"Abc"转换为"['A','b','c']"

If you want literally the string "['A','b','c']" as opposed to the expression ['A','b','c'] which is identical to "Abc" since in Haskell the String type is a synonym for [Char] , then something like the following will work: 如果您要从字面上获取字符串"['A','b','c']" ,而不是与['A','b','c'] "Abc" ['A','b','c']相同的表达式['A','b','c'] ,因为在Haskell中, String类型是[Char]的同义词,然后将执行以下操作:

'[': (intercalate "," $ map show "Abc") ++ "]"

The function intercalate is in Data.List with the type intercalate函数在Data.List ,类型为

intercalate :: [a] -> [[a]] -> [a]

It intersperses its first argument between the elements of the list given as its second argument. 它将其第一个参数散布在作为第二个参数给出的列表元素之间。

I assume you meant how to turn "Abc" into ["A", "b", "c"] . 我假设您的意思是如何将"Abc"转换为["A", "b", "c"] This is quite simple, if the string to split is s , then this will do the trick: 这很简单,如果要拆分的字符串是s ,那么就可以解决问题:

map (\x -> [x]) s

Fire up ghci to see that the expressions you wrote are the same: 启动ghci,以查看您编写的表达式是相同的:

Prelude> ['A','b','c']
"Abc"

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

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