join list delim = ((foldr (++) "" (map (++ delim) list)))
I can't help but think it must be possible (and much simpler) using just a fold. Here is what I tried. It won't compile and is probably a schoolboy error. Is it possible to combine two concatenation operations in some way so that I can do this with just a fold?
join list delim = foldr (++ delim ++) "" list
I also tried this, which doesn't compile either.
join3  delim = "" join3 list delim = head(list) ++ delim ++ join3((tail list) delim)
The error message is below, and suggests I have done something silly with types, though I have no idea what.
Couldn't match expected type `[Char]' with actual type `[Char] -> [Char]' In the return type of a call of `join3' In the second argument of `(++)', namely `join3 ((tail list) delim)' In the second argument of `(++)', namely `delim ++ join3 ((tail list) delim)' Compilation failed.
Any insight as to how I could fix these pieces of code would be much appreciated.