Haskell - Concat a list of strings -
im trying create list of strings using recursion.
basically want take part of string point. create list , process rest of string through 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 liststr = [getline] ++ splitlines getrest getline = (takewhile (/='\n') str) getrest = (dropwhile (=='\n') (dropwhile (/='\n') str))
thats got. concats strings since list of characters themselves. want create list of strings.
["test","123"] if input "test\n123\n"
thanks
if try compile code, you'll error message telling in line
liststr = [getline] ++ splitlines getrest
splitlines getrest
has type document
, should have type [string]
. easy enough understand, since [getline]
list of strings (well list of 1 string) , can concatenated list of strings, not list of int-string-tuples.
so fix can use map replace each int-string-tuple in document string list of strings, i.e.:
liststr = [getline] ++ map snd (splitlines getrest)
after changing line above code compile , run fine.
but concats strings since list of characters themselves.
i'm not sure why think that.
the reason code did not compile because of type of splitlines
explained above. once fix error, code behaves want to, returning list of integer-string-tuples. @ no point strings concatenated.
Comments
Post a Comment