I am basically looking for the Haskell equivalent of the following python code:
' '.join(['_','a','b','1'])
I understand that python treats those as strings instead of chars, but... I digress.
MRE:
[if <some condition is true> then '#' else chr elem | elem <- lst] -- lst is [Integer] (appropriate Integer -> Int conversion function applied, but not specified here)
expected output:
['#',' ','a',' ','b',... you get the idea]
What I currently have:
['#','a','b',...]
Best I could achieve was:
concat [if <some condition is true> then "# " else [chr elem] ++ " " | elem <- lst]
which seems like overkill. Is there an easier way to achieve this?
PS: unwords
does not take Char
s.
The intersperse
function will insert a single Char
between every pair of characters in a String
(or more generally, a single element of type a
between every pair of elements in a list [a]
). For example:
> intersperse ' ' ['a','b','c']
"a b c"
> intersperse ' ' "abc"
"a b c"
You can also convert a list of characters into a list of single-character strings by mapping the function singleton
over the list, like so:
> map singleton ['a','b','c']
["a","b","c"]
which would allow you to apply unwords
, or use intercalate
to insert a multi-character string between the characters, as in:
> intercalate ", " $ map singleton ['a','b','c']
"a, b, c"