A polyvariadic function is a function with a variable number of arguments, that can have polymorphic or just different types. An example for a polyvariadic function is the common function printf, since the last arguments can be of any type.
Questions tagged [polyvariadic]
26 questions
111
votes
1 answer
How does Haskell printf work?
Haskell's type safety is second to none only to dependently-typed languages. But there is some deep magic going on with Text.Printf that seems rather type-wonky.
> printf "%d\n" 3
3
> printf "%s %f %d" "foo" 3.3 3
foo 3.3 3
What is the deep magic…

Dan Burton
- 53,238
- 27
- 117
- 198
80
votes
5 answers
How to create a polyvariadic haskell function?
I need a function which takes an arbitrary number of arguments (All of the same type), does something with them and afterwards gives a result back. A list of arguments is impracticable in my specific case.
As I looked through the haskell libs, I saw…

fuz
- 88,405
- 25
- 200
- 352
20
votes
3 answers
Haskell "Apply"?
Possible Duplicate:
Why is such a function definition not allowed in haskell?
I'm a newcomer to the world of Haskell, migrating over from Lisp. I'm trying to adjust to Haskell's fundamentally different worldview, and one of the many things that I…

Ord
- 5,693
- 5
- 28
- 42
15
votes
3 answers
Polyvariadic Functions in Haskell
After reading this article on writing polyvariadic functions in Haskell, I tried to write some of my own.
At first I thought I'd try to generalize it - so I could have a function that returned variadic functions by collapsing arguments as given.
{-#…

rampion
- 87,131
- 49
- 199
- 315
10
votes
1 answer
How to define an arbitrary arity function in Haskell, which includes an arity of 0?
My current approach to define a function of arbitrary arity is below, with A being an accumulator, E being the input argument type, and R being the result type.
combine :: A -> E -> A
class X r where
foo :: A -> E -> r
instance X R where
…

Dingfeng Quek
- 898
- 5
- 14
9
votes
4 answers
Haskell Polyvariadic Function With IO
Is it possible to have a function that takes a foreign function call where some of the foreign function's arguments are CString and return a function that accepts String instead?
Here's an example of what I'm looking for:
foreign_func_1 :: (CDouble…

ricree
- 35,626
- 13
- 36
- 27
7
votes
2 answers
In Haskell how can I take an m-ary predicate and an n-ary predicate and construct a (m+n)-ary predicate?
Today I played with using type classes to inductively construct functions of a predicate of any arity taking as inputs any combination of any types, that returned other predicates of the same type but with some basic operation applied. For…

teryret
- 577
- 1
- 5
- 15
6
votes
0 answers
Ambiguous type variable in polyvariadic curry definition
So, I'm trying to implement a polyvariadic ZipWithN as described here. Unfortunately, Paczesiowa's code seems to have been compiled with outdated versions of both ghc and HList, so in the process of trying to understand how it works, I've also…

Lily
- 155
- 1
- 6
6
votes
1 answer
Polyvariadic generalised sum
This answer demonstrates a polyvariadic function that sums its arguments:
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 .…

Dylan
- 1,692
- 1
- 13
- 24
5
votes
4 answers
Specifying "any subclass" in a C# type constraint rather than "one particular subclass"
If I would like to write a method that takes a variable number of "TDerived" where TDerived is any subclass of a class "Base", is there any way to do this?
The following code only works with a single specific specified subclass:
void…

Lucina
- 490
- 4
- 16
5
votes
1 answer
How to create a polyvariadic function which itself uses another polyvariadic function (e.g. printf)?
I'm trying to write the below function so that I can use it with a variable number of arguments to Text.Printf.printf. I tried using typeclasses but I couldn't find any example which wasn't processing the format string character by character and…

rex
- 124
- 7
5
votes
1 answer
function composition with Text.Printf.printf
I would like to define a logger function, like
myPutStrLn = putStrLn . (++) "log: "
main = do myPutStrLn "hello"
which is fine. Now I want to format the provided String with printf, like this
myPutStrLn $ printf "test %d" (23 :: Int)
Great! Since…

lewurm
- 1,103
- 7
- 19
4
votes
1 answer
variadic bind in Haskell
The following code is an attempt to write a variadic function that acts like this:
bind_variadic mx f = mx >>= f
bind_variadic mx my f = do { x <- mx; y <- my; f x y }
I can write it if one expresses the "rest of binding" as a variable k, but in…

gatoatigrado
- 16,580
- 18
- 81
- 143
4
votes
1 answer
Haskell polyvariadic function with no arguments
I'm trying to create a polyvariadic function in Haskell, I used this answer to create a basic function.
Here is the function's code :
class SumRes r where
sumOf :: Integer -> r
instance SumRes Integer where
sumOf = id
instance (Integral…

Artémis Young
- 3,122
- 1
- 12
- 17
3
votes
2 answers
Result type of a polyvariadic function in haskell
While studying polyvariadic functions in Haskell I stumbled across the following SO questions:
How to create a polyvariadic haskell function?
Haskell, polyvariadic function and type inference
and thought I will give it a try by implementing a…

Max Maier
- 985
- 6
- 16