unwords with optional record separator is hard to write?
Submitted by metaperl on Thu, 05/11/2006 - 10:29am.
::
I was chatting with Cale on #haskell and said that unwords should be configurable: it should take an optional argument specifying what to separate the elements of the list with.
He said that would be hard.
It is a common thing in Perl and Lisp for a function to assume defaults if you don't specify things.
I guess the best thing to do in Haskell is to always expect the separator to be specified and have the function curryable on the input list to be unworded:
unwords :: String -> [a] -> String
unwords sep lis = blah blah
- metaperl's blog
- Login to post comments
An ugly disgusting solution could be,
class Unwords arg where unwords :: arg -> String instance Unwords [String] where unwords xs = ... instance Unwords (String,[String]) where unwords (sep,xs) = ...or other variations on this.
UsingRecords also illustrates a scheme for default arguments, though it is still more verbose than a built-in feature would be.
unwords sep = foldr1 (\a b -> a ++ sep : b)