cod*_*ife 0 haskell functional-programming
我想创建一个while循环来连接xs
列表中的字符串,直到找到一个空字符串,但似乎我们既没有机会增加Int也没有创建while循环.
所以这看起来像是Haskell的伪代码,但我怎样才能真正实现我的解决方案呢?
prt :: String -> [String] -> Int -> String
prt str xs x = do
while((xs !! (x)) /= "")
str = str ++ (xs !! (x++))
Run Code Online (Sandbox Code Playgroud)
忘记数组索引:通常不需要它们.您为自己的任务所要做的就是获取列表中包含非空字符串的最长前缀.
takeWhile (not . null) xs
-- or
takeWhile (/= "") xs
Run Code Online (Sandbox Code Playgroud)
然后你想连接这些字符串.
concat $ takeWhile (/= "") xs
Run Code Online (Sandbox Code Playgroud)
如果n
由于某种原因想要在字符串之后启动,只需n
在开始之前删除第一个:
concat $ takeWhile (/= "") $ drop n xs
Run Code Online (Sandbox Code Playgroud)
如果你真的想做一个自定义的"循环",请使用递归:
g xs n = f $ drop n xs
f [] = ""
f ("":xs) = ""
f (x:xs) = x ++ f xs
Run Code Online (Sandbox Code Playgroud)