Haskell - Concatenated String List

I am trying to create a list of strings using some recursion.

Basically, I want to take part of the string to a specific point. Create a list from this, and then process the rest of the string with recursion.

type DocName = FilePath
type Line = (Int,String)
type Document = [Line]

splitLines :: String -> Document
splitLines [] = []
splitLines str | length str == 0 = []
                             | otherwise = zip [0..(length listStr)] listStr
                                    where 
                                        listStr = [getLine] ++ splitLines getRest
                                        getLine = (takeWhile (/='\n') str)
                                        getRest =  (dropWhile (=='\n') (dropWhile (/='\n') str))

Here is what I got. But it just concatenates the lines back, since they are a list of characters. But I want to create a list of strings.

["test", "123"] if the input was "test \ n123 \ n"

thank

+3
source share
2 answers

If you try to compile your code, you will get an error message telling you that the line

listStr = [getLine] ++ splitLines getRest

splitLines getRest Document, [String]. , [getLine] - ( ), , .

, , , int-string- , , ..:

listStr = [getLine] ++ map snd (splitLines getRest)

, , .

, .

, .

, , - splitLines, . , , . .

+3

, , , , sepp2k. -

splitLines str = zip [0..] (lines str)

splitLines = zip [0..] . lines
+3

Source: https://habr.com/ru/post/1767218/


All Articles