views:

379

answers:

5

I need a function which takes an arbitrary number of arguments (All of the same type), does something whith them and afterwards gives a result back. A list of arguments is impracticable in my specific case.

As I looked through the haskell libs, there is the function printf from module Text.Printf, which uses a similar trick. Poorly I couldn't understand that magic by looking at the source.

Can somebody explain how to archieve this or knows some webpage/paper/whatever, where can I find a good description for this?

Reason:

The reason is really easy going. For school (computer science class), we should write a module which is able to "record" a mathematical expression and express it as a string (Via writing an instance of Num/Real/etc for an own datatype) and can perform various operations on it.
This datatype contains a special constructor for a variable, which may be replaced by a value or whatever by a specified function. One of the goals is to write a function, which takes such an expression, the variables as pairs of type (Char,Rational) and calculates the result of the expression. We should look, how to express the goal of the function best. (My idea: The function returns another function which takes exactly as many arguments as vars are defined in the function - seems to be impossible).

+4  A: 

In the wiki article on variadic functions, this article was referenced. I suppose this is what printf does, but I don't understand it either. Anyway, this is certainly an overkill, especially since your arguments are all of the same type. Just put them all in one list. That's what lists are good for - an arbitary number of things of the same type. Fine, it's not very beautiful, but it will hardly be uglier than a complete polyvariadic function.

delnan
Although this is a bit confusing, this seems to be that, what I need.
FUZxxl
+18  A: 

The key points of printf is the ability to either return a String or a function. Copied from http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/src/Text-Printf.html,

printf :: (PrintfType r) => String -> r
printf fmts = spr fmts []

class PrintfType t where
    spr :: String -> [UPrintf] -> t

instance (IsChar c) => PrintfType [c] where
    spr fmts args = map fromChar (uprintf fmts (reverse args))

instance (PrintfArg a, PrintfType r) => PrintfType (a -> r) where
    spr fmts args = \a -> spr fmts (toUPrintf a : args)

and the basic structure we can extract out is

variadicFunction :: VariadicReturnClass r => RequiredArgs -> r
variadicFunction reqArgs = variadicImpl reqArgs mempty

class VariadicReturnClass r where
   variadicImpl :: RequiredArgs -> AccumulatingType -> r

instance VariadicReturnClass ActualReturnType where
   variadicImpl reqArgs acc = constructActualResult reqArgs acc

instance (ArgClass a, VariadicReturnClass r) => VariadicReturnClass (a -> r) where
   variadicImpl reqArgs acc = \a -> variadicImpl reqArgs (specialize a `mappend` acc)

For instance:

class SumRes r where 
    sumOf :: Integer -> r

instance SumRes Integer where
    sumOf = id

instance (Integral a, SumRes r) => SumRes (a -> r) where
    sumOf x = sumOf . (x +) . toInteger

then we could use

*Main> sumOf 1 :: Integer
1
*Main> sumOf 1 4 7 10 :: Integer
22
*Main> sumOf 1 4 7 10 0 0  :: Integer
22
*Main> sumOf 1 4 7 10 2 5 8 22 :: Integer
59
KennyTM
Thanks KennyTM! That was exactly, what I was looking for!
FUZxxl
+1 Brilliant trick!
Dario
@KennyTM: Do you know, whether there is a tag for polyvariadic functions?
FUZxxl
@FUZxxl: On this site? There is one for [variadic-functions](http://stackoverflow.com/questions/tagged/variadic-functions) but it is C/C++-centric.
KennyTM
Hm... AFAIK (poly)variadic functions are a big topic in haskell programming. I don't no the guidelines, but maybe just create a tag for this purpose. (Or should I use variadic-functions)?
FUZxxl
@FUZxxl: If it is distinct enough from variadic functions of C, create a new tag.
KennyTM
I haven't the required reputation to create a new tag.
FUZxxl
It actually is far away from C-style polyvariadic stuff, because in C they`re unsafe, threaten as a list and builtin, in Haskell they are rather a programming technique than a real language feature, and also extremely different in their handling.
FUZxxl
@FUZxxl: Added [polyvariadic] in, together with [variadic-functions].
KennyTM
Thank you very much!
FUZxxl
In C it's unsafe (but then the language is, as a whole), but in C++0x variadic templates allow for type safe polyvariadic functions.
Matthieu M.
+2  A: 

I took a look at an example linked from the article that delnan referenced. After staring at it for a bit, I think I finally comprehend what is going on:

It starts with this type class:

class BuildList a r  | r-> a where
    build' :: [a] -> a -> r

That bit after the pipe (|) is a functional dependency. It says that the type represented by a can be determined by the type represented by r. In other words, you are prevented from defining two instances of the BuildList typeclass with the same r (return type), but different a.

Jumping ahead a bit to where the build' function is actually used:

> build True :: [Bool]

Since build is just calling build' with an empty list as the first parameter, this is the same as:

> build' [] True :: [Bool]

In this example, build' is clearly returning a list. Because of the functional dependency, we can only be binding to this instance of the BuildList type class:

instance BuildList a [a] where
    build' l x = reverse$ x:l

Pretty straightforward. The second example is more interesting. Expanding the definition of build, it becomes:

> build' [] True False :: [Bool]

What's the type of build' in this case? Well, the precedence rules of Haskell mean that the above could also be written like this:

> (build' [] True) False :: [Bool]

Now it becomes clear that we are passing two parameters to build' and then applying the result of that expression to a parameter with value 'False'. In other words, the expression (build' [] True) is expected to return a function of type Bool -> [Bool]. And that binds us to the second instance of the BuildList typeclass:

instance BuildList a r => BuildList a (a->r) where
    build' l x y = build'(x:l) y

In this invocation, l = [] and x = True and y = False, so the definition expands to build' [True] False :: [Bool]. That signature binds to the first instance of build', and it's fairly obvious where it goes from there.

Daniel Pratt
This description is extremely straightforward and very usefull. The only thing which remains unclear for me is that definition `build' l x = reverse $ x:l`. But in my opinion, the answer of KennyTM is more usefull, because I rather want to iterate over the parameters than having them presented as a list.
FUZxxl
+6  A: 

Lots of people are telling you how to create variadic functions, but I think in this case you're actually better off just using a list of type [(Char,Rational)].

Cale Gibbard
Please look, delnan has said exactly this. Yes, I know, that this would be easier, but it's very usefull to understand, how such mechanics works by implementing it in an own program.
FUZxxl
Also something to note: This is the *idiomatic* way to write it as well. The only variadic function that I think I've used is `printf`; I'd expect your function to take a list.
Antal S-Z
A: 

This is just an answer to allow me to edit the tag-wiki. Please don't downvote for this.

FUZxxl
Hm?! This doesn't works...
FUZxxl