Maybe the title is a bit confusing so let me explain with an example:
Let's say i want to "print" lines of strings in a nice way. as input i would give a list with pair of ints and strings (in the case below: [(1,A),(2,B),(3,C)]). This would give the output:
|NUMBER|LETTER|
---------------
|1 |A |
|2 |B |
|3 |C |
---------------
My try (where i use pseudocode functions printLine and printRow, which do what the name says):
prettyString :: [(Int,String)] -> [String]
prettyString [] = [printLine]
prettyString t@(top:rows) = printRow : prettyString rows
Now to finally get to my point: the code above goes in recursion and will give an output like
|1 |A |
|2 |B |
|3 |C |
---------------
So it doesn't take the header with it (which makes sense, because i didn't do anything with it). But i'm stuck of thinking of a way how to manually add those lines at the top. So that, before going in recursion, i can do an "initial command" and already add those strings to the list.
Hope someone understands my problem .. thanks anyway !
CodePudding user response:
You render the lines in a helper function, and you define an outer one that writes the header, so:
prettyString :: [(Int, string)] -> [String]
prettyString xs = "|NUMBER|LETTER|" : printLine : prettyString' xs
prettyString' :: [(Int,String)] -> [String]
prettyString' [] = [printLine]
prettyString' t@(top:rows) = printRow : prettyString' rows
CodePudding user response:
Make another function that appends the header to the output of prettyString
, like so:
prettyTable :: [(Int, String)] -> [String]
prettyTable t = ["|NUMBER|LETTER|", printLine] prettyString t