Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

list - Split a [Char] by each character in Haskell

I'm new to Haskell and I would like some help into how to split a sentence into separate characters.

Example: "Test sentence" == ["T","e","s","t"," ","s","e","n","t","e","n","c","e"]

I checked everywhere but cant find a solution that don't require import of separate modules and stuff.

Many thanks in advance!

question from:https://stackoverflow.com/questions/65904680/split-a-char-by-each-character-in-haskell

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In Haskell a String is a type synonym for [Char].

If you really want to turn a [Char] into a lists of one-character Strings (you probably don't):

charStrs :: String -> [String]
charStrs = fmap pure

charStrs "hello world" -- ["h","e","l","l","o"," ","w","o","r","l","d"]

edit updated to pure


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...