[英]How to split a list of numbers into a set of list with all the numbers in Haskell
我如何將 Haskell 中的列表(例如,“222 33244”)拆分為 ["222","33","2","444"] 僅通過前奏的遞歸和函數?
我目前的嘗試是:
list xs
|length xs == 0 = ""
|otherwise = listSplit xs
listSplit (x:xs)
|x == head xs = x : ListSplitNext x xs
|otherwise = x:[]
listSplitNext a (x:xs)
|a == x = a : listSplitNext x xs
|otherwise = listSplit xs
因此,由於我不太了解您的方法,並且 ghci 在您的代碼中列出了 18 個編譯錯誤,恐怕我無法幫助您嘗試解決方案。
正如評論中所指出的,可能的解決方案是:
listSplit xs = listSplit' [] (filter (`elem` ['0'..'9']) xs)
listSplit' ws (x:xs) = listSplit' (ws ++ [x : takeWhile (==x) xs]) (dropWhile (==x) xs)
listSplit' ws [] = ws
過濾字符串中不是數字的每個元素(Data.Char.isNumber 也會這樣做,但前提是只使用 Prelude 函數)並在過濾后的列表上調用listSplit'
。
(ws ++ [x : takeWhile (==x) xs])
收集xs
所有內容,直到遇到一個不等於x
的字母,將其包裝在一個列表中並將其附加到ws
。
(dropWhile (==x) xs)
刪除了每個在信xs
,直到它到達一個字母不等於x
。
最后,該函數使用更新的ws
和減少的xs
調用自身
如果沒有更多剩余元素,則函數返回ws
如果您的目標是使用很少的預定義函數,這可能會給您一些想法:
listSplit :: String -> [String]
listSplit xs =
let (as, a) = foldr go ([], []) numbers
in a : as
where
isNumber x = x `elem` ['0'..'9']
numbers = filter isNumber xs
go cur (res, []) = (res, [cur])
go cur (res, lst@(a:_))
| a == cur = (res, a : lst)
| otherwise = (lst : res, [cur])
當然,您也可以用自己的遞歸替換foldr
:
numberSplit :: String -> [String]
numberSplit xs =
let numbers = filter (`elem` ['0'..'9']) xs
in listSplit numbers
listSplit :: Eq a => [a] -> [[a]]
listSplit =
reverse . go [] []
where
go acc as [] = as : acc
go acc [] (x:xs) = go acc [x] xs
go acc as@(a:_) (x:xs)
| a == x = go acc (a : as) xs
| otherwise = go (as : acc) [x] xs
我有時間來實現這一點,但我認為這就是你正在尋找的。
listSplit s = go filtered
where filtered = [c | c <- s, elem c ['0'..'9']]
go [] = []
go (x:xs) = (x : takeWhile (== x) xs) : (go $ dropWhile (== x) xs)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.