How to generate the alphabet with white space between the letters?

6

In Haskell I can generate the alphabet as follows:

alfabeto = ['a'..'z']

To display it simply:

alfabeto

"abcdefghijklmnopqrstuvwxyz"

However, I'd like to know how I can put a space between the letters, like this:

"a b c d e f g h ..."

Questions

  • Is there any way to do this?
  • Is there a specific operator I can use? If so, how should I use it?
  • asked by anonymous 17.07.2018 / 16:35

    2 answers

    4

    The module Data.List has the function intersperse that does just that. See it at GHCi:

    Prelude> :m + Data.List
    Prelude Data.List> intersperse ' ' ['a'..'z']
    "a b c d e f g h i j k l m n o p q r s t u v w x y z"
    
        
    18.07.2018 / 23:03
    2

    If you can keep the space at the end, after the z character, you can map the characters by concatenating with the white space and then merge all into a string with unwords :

    > unwords (map(: " ")['a'..'z'])
    "a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z "
    

    See working at Repl.it

        
    17.07.2018 / 17:10