Is there any haskell function to concatenate list with separator?

前端 未结 5 1531
庸人自扰
庸人自扰 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 16:42

    Some other ideas of implementations of intersperse and intercalate, if someone is interested:

    myIntersperse :: a -> [a] -> [a]
    myIntersperse _ [] = []
    myIntersperse e xs = init $ xs >>= (:[e])
    
    myIntercalate :: [a] -> [[a]] -> [a]
    myIntercalate e xs = concat $ myIntersperse e xs
    

    xs >>= f is equivalent to concat (map f xs).

提交回复
热议问题