Reputation: 123
I want a function that can turn an array into a string while inputting some new lines in there too.
Essentially let's say I have the list: [1234567890] I would like to pass that in with an integer and return that list as a String that is formatted like such:
(if the Int was 5, it would be)
"12345"
"67890"
I'm thinking I can do something like:
split :: Int -> [char] -> String
split n (x:xs) = split (take n-1 (x:xs)) -- I'm trying to make this recursive
I'm also going to assume that whatever length the array is will be divisible by the Int I pass in.
Upvotes: 0
Views: 102
Reputation: 3489
To do this you have to make split
recursive, but in your recursive step it did not receive the same number of parameters, and you tried to pass parts of the argument. The n
can stay the same for every step, and when you take n
out of the list in each step you also have to drop these n
elements for the remaining steps.
splitN _ [] = ""
splitN n l = (take n l) ++ "\n" ++ (splitN n $ drop n l)
Upvotes: 2