有任何haskell函数来连接列表与分隔符?
有一个函数来连接列表的元素与分隔符? 例如:
> foobar " " ["is","there","such","a","function","?"] ["is there such a function ?"] 谢谢你的回复!
是的, 有 :
 Prelude> import Data.List Prelude Data.List> intercalate " " ["is","there","such","a","function","?"] "is there such a function ?" 
  intersperse是有点更一般: 
 Prelude> import Data.List Prelude Data.List> concat (intersperse " " ["is","there","such","a","function","?"]) "is there such a function ?" 
 另外,对于想要与空格字符结合的具体情况,还有一些unwords : 
 Prelude> unwords ["is","there","such","a","function","?"] "is there such a function ?" 
  unlines工作方式是相似的,只是string使用换行符(因为某种原因换行符也被加到了末尾)。 
 joinBy sep cont = drop (length sep) $ concat $ map (\w -> sep ++ w) cont 
 如果你想写你自己的intercalate和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) 
使用foldr写一行不难
 join sep xs = foldr (\a b-> a ++ if b=="" then b else sep ++ b) "" xs join " " ["is","there","such","a","function","?"]