忘记数组索引:通常不需要它们.您为自己的任务所要做的就是获取列表中包含非空字符串的最长前缀.
takeWhile (not . null) xs -- or takeWhile (/= "") xs
然后你想连接这些字符串.
concat $ takeWhile (/= "") xs
如果n
由于某种原因想要在字符串之后启动,只需n
在开始之前删除第一个:
concat $ takeWhile (/= "") $ drop n xs
如果你真的想做一个自定义的"循环",请使用递归:
g xs n = f $ drop n xs f [] = "" f ("":xs) = "" f (x:xs) = x ++ f xs