Getting started with strings in Haskell -


i need write function takes string of gggggggeeeeetttttt , can count how many times letter repeats , output g7e5t6.

i've started haskell not sure @ start.

the function group group identical elements in list. since string list of characters, have:

group "ggggeeetttt" = ["gggg","eee","tttt"] 

to letter, can use map head, since head takes first element of string (or list):

map head ["gggg","eee","tttt"] = ['g','e','t'] 

now, want know how many elements there in each substring. can done using map length:

map length ["gggg","eee","tttt"] = [4,3,4] 

let's turn these numbers strings:

map show [4,3,4] = ["4","3","4"] 

now need combine original list , length list somehow. can follows:

zipwith (:) ['g','e','t'] ["4","3","4"] 

zipwith apply specified function paired elements in 2 lists. here i've used (:), adds element start of list.

this gives building blocks need want. example, should work, although haven't tested it:

f s = concat $ zipwith (:) letters lengths             groups  = group s         letters = map head groups         lengths = map (show . length) groups 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -