haskell - List of Strings to single String -
i'm trying exercise in book,
define function:
onseperatelines :: [string] -> string
which takes list of string , returns single string when printed shows strings on separate lines.
i'm struggling converting list single string
onseperatelines :: [string] -> string onseperatelines ls = [x | x <- ls]
i can write functions take string , convert list , list outputs list, can't figure out how take list , convert single string.
a string
nothing list of characters:
type string = [char]
hence,
onseperatelines :: [[char]] -> [char]
now, if need application, it's idea first ask hoogle if there's there. whole lot of results:
unlines :: [string] -> string
-- that's exactly function you're trying implement!
unwords :: [string] -> string
-- similar, insert spaces, not newlines
joinpath :: [filepath] -> filepath
-- not relevant here
concat :: [[a]] -> [a]
-- general task of flattening nested list simple 1 string.
concat
choice if don't want use standard function specific task, don't want make life more difficult necessary.
of course, can't hurt write function once completely yourself. that, you'll need recursively† deconstruct list. extremely simple pattern matching:
onseperatelines [] = ... --- no lines concatenate... what's result? onseperatelines (l:ls) = ... otherlines = onseperatelines ls
think little goes in these gaps.
†the equivalent solution foldr
excercise.
Comments
Post a Comment