4

I was wondering how can I achieve:

Taking the first n characters of a string then ++(concatenating them with) drop these first n and take the next n and so on (without cutting words). I have tried function composition and $ but the only thing I get, is errors.

EDIT

I am trying to align the text left for a given column width (n), that's why I try not to cut words, but if there is a word at the number n , just take some chars before it and then use \n to start again for the next line. My main problems so far are checking for the cut-words condition(I can use !! but should I use it in guards with map(-1) or how else) and implementing the recursion because as a base I got

take n s ++ "\n" ++ take n (drop n s)

and also the case where n is smaller than the longest word:

leftAlign n str = if n < ((maximum . map length . words) str) then "" else leftAlign n str
Dimitar
  • 4,402
  • 4
  • 31
  • 47
  • 1
    Can you show us what you tried and where you stuck? – utdemir Jun 13 '15 at 17:27
  • 1
    I have removed your last sentence; it is too easy to interpret it as a snarky comment about Haskell and its practitioners. In case it was intended to be that way: don't do that again. – Daniel Wagner Jun 13 '15 at 17:30
  • 6
    At chi's prompting: what exactly does "without cutting words" mean? I'm not sure I understand the question yet. – Daniel Wagner Jun 13 '15 at 17:45
  • I am trying to align the text left for a given column width, that's why I try not to cut words, but if there is a word at the number n , just take some chars before it and then use `\n` to start again for the next line. – Dimitar Jun 14 '15 at 08:35
  • 1
    @user4325010 Define "word"; it may be obvious to you, but "word" is a very fluid concept. Also, are you sure you don't want to write a (simple) parser for this kind of task, instead of relying only on `take` and `drop`? – jub0bs Jun 14 '15 at 12:01
  • @Jubobs Words are substrings divided with space (`" \t\n"`). Well, such parser implementation would be interesting to see and I have turned to `take` and `drop` only because it was the obvious. – Dimitar Jun 14 '15 at 12:05
  • 1
    I think @Jubobs is right. You basically want a parser whose state includes a letter count along with the string. – dfeuer Jun 21 '15 at 15:40
  • In answer to the question *title*: yes, there is a function that drops and takes at the same time: it's called `splitAt`. There's also an equivalent function for `dropWhile`/`takeWhile`. – SwiftsNamesake Jul 16 '17 at 19:48
  • This works, although it's a tad convoluted: https://gist.github.com/SwiftsNamesake/4a3080fb7de41cb8e89604445d712940 – SwiftsNamesake Jul 17 '17 at 05:53

1 Answers1

-1

Data.List.Split.chunksOf does this.

Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380