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

Submitted by Derek Elkins on Fri, 05/12/2006 - 2:32pm.

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.

Submitted by evan on Sun, 06/04/2006 - 5:34pm.

unwords sep = foldr1 (\a b -> a ++ sep : b)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.