Haskell通过递归重新定义单词功能

我需要通过递归重新定义单词function。我有2个辅助函数,但我无法真正将最后一个函数放在一起。有什么想法吗?

takeWord :: String -> String
takeWord  ""      = ""
takeWord (' ':xs) = ""
takeWord (x  :xs) = x : takeWord xs

dropWord :: String -> String
dropWord  ""      = ""
dropWord (' ':xs) =  ' ' : xs
dropWord (x  :xs) =  dropWord xs

words' :: String -> [String]
words' "" = []
words' (' ':xs) = takeWord xs : words' xs
words' (x:xs) =  takeWord (x:xs) : words' (dropWord xs)

此输入" Correct answer is this"的结果应为["Correct","answer","is","this"]。现在,我得到以下输出:["","","Correct","this","this"]

panda781124 回答:Haskell通过递归重新定义单词功能

从@chi获得工作解决方案

“也许我会改用单词'('':xs)=单词'xs来跳过前导空格,然后看看会发生什么。”

本文链接:https://www.f2er.com/3158970.html

大家都在问