简体   繁体   English

Haskell同时接受和丢弃

[英]Haskell take and drop at the same time

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). 取字符串的前n个字符然后++ (与它们连接)删除这些前n个并取下一个n依此类推(不用切词)。 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. 我试图将给定列宽(n)左边的文本对齐,这就是为什么我尝试不剪切单词,但如果在数字n处有一个单词,只需在它之前取一些字符然后使用\\ n开始再次为下一行。 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 到目前为止我的主要问题是检查cut-words条件(我可以使用!!但是我应该在带有map(-1)的防护中使用它还是如何)并实现递归,因为我作为一个基础我得到了

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

and also the case where n is smaller than the longest word: 以及n小于最长字的情况:

leftAlign n str = if n < ((maximum . map length . words) str) then "" else leftAlign n str

Data.List.Split.chunksOf执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM