So I have to number each string in list of strings
Like this:
numberLines ["one", "two", "three"]
==> ["1: one", "2: two", "3: three"]
I tried this:
numberLines = map (\g -> (x + 1) : g) where
x = 0
but of course it didn't work
In Haskell variables are immutable, so you can not change the value of a variable like in an imperative language like Python.
You can for example use zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] to zip the list of strings together with a list of Ints:
numberLines :: [String] -> [String]
numberLines = zipWith f [1 :: Int .. ]
where f i s = show i ++ ' ' : ':' : s
Here f thus takes an Int and a String and produces a string "i: s" with i the string representation of the number, and s the string.
Others suggested you try using zipWith or mapAccumL to implement the function. Here are two implementations using those functions:
addNumber n string = show n ++ ": " ++ string
numberLines = snd . mapAccumL (\n string -> (n + 1, addNumber n string)) 1
numberLines1 = zipWith addNumber [1..]
zipWith.mapAccumL.