在haskell中用foldr拆分

chr*_*sso 2 haskell

我需要split使用foldr

split :: Eq a ? a ? [a] ? [[ a ]]
Run Code Online (Sandbox Code Playgroud)

例子:

split '/' ”hello/my/friends” ----> [”hello”,”my”,”friends”]
Run Code Online (Sandbox Code Playgroud)

这是我尝试过的:

split :: Eq a ? a ? [a] ? [[ a ]]
split str delim = let (start, end) = break (== delim) str
                in start : if null end then [] else groupBy (tail end) delim 
Run Code Online (Sandbox Code Playgroud)

rya*_*hza 5

这样的事情应该工作:

foldr (\c (x:xs) ->
    if c == '/'
    then "":x:xs
    else (c:x):xs
  ) [""] "hello/my/friends"
Run Code Online (Sandbox Code Playgroud)

概括:

split on = foldr (\c (x:xs) ->
    if c == on
    then []:x:xs
    else (c:x):xs
  ) [[]]
Run Code Online (Sandbox Code Playgroud)