Is there any haskell function to concatenate list with separator?

前端 未结 5 1533
庸人自扰
庸人自扰 2020-12-07 16:06

Is there a function to concatenate elements of a list with a separator? For example:

> foobar \" \" [\"is\",\"there\",\"such\",\"a\",\"function\",\"?\"]
[         


        
5条回答
  •  青春惊慌失措
    2020-12-07 17:01

    If you wanted to write your own versions of intercalate and intersperse:

    intercalate :: [a] -> [[a]] -> [a]
    intercalate s [] = []
    intercalate s [x] = x
    intercalate s (x:xs) = x ++ s ++ (intercalate s xs)
    
    intersperse :: a -> [a] -> [a]
    intersperse s [] = []
    intersperse s [x] = [x]
    intersperse s (x:xs) = x : s : (intersperse s xs)
    

提交回复
热议问题