Calling function with unknown number and type arguments

I have to write a function which calls a function with unknown (i.e. given in runtime) number and type of arguments. 1. Use closure implementation from Gtk2Hs. Pros: Written Cons: Not in haskell 2. Use unsafeCoerce. Something like: f a b = return (a + b) f' = unsafeCoerce (f :: Int -> Int -> IO Int) :: IO () unsafeCoerce (unsafeCoerce ((unsafeCoerce f' :: Int -> IO ()) 5) :: Int -> IO ()) 4 :: IO Int (of course it is an example in fact it will be not only Int etc.) Personally I'd prefere in-haskell solution as: - Rts API used by Gtk2Hs is 'semi-public' - It is GHC-specific (?) but I'm not sure if my solution is safe. Regards

On Aug 16, 2009, at 06:45 , Maciej Piechotka wrote:
I have to write a function which calls a function with unknown (i.e. given in runtime) number and type of arguments.
Take a look at the implementation of Text.Printf. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

On Sun, Aug 16, 2009 at 17:00, Brandon S. Allbery wrote:
On Aug 16, 2009, at 06:45 , Maciej Piechotka wrote:
I have to write a function which calls a function with unknown (i.e. given in runtime) number and type of arguments.
Take a look at the implementation of Text.Printf.
Or Text.XFormat.Show in xformat while you're at it. (Warning: blatant self-promoting.) http://hackage.haskell.org/package/xformat
From the tests in the package:
showf (Show % Int % String) 4.05 20 " blah" == "4.0520 blah" showf (Wrap '(' Int ')') 24 == "(24)" Sean

On Sun, Aug 16, 2009 at 3:07 PM, Sean Leather
On Sun, Aug 16, 2009 at 17:00, Brandon S. Allbery wrote:
On Aug 16, 2009, at 06:45 , Maciej Piechotka wrote:
I have to write a function which calls a function with unknown (i.e. given in runtime) number and type of arguments.
Take a look at the implementation of Text.Printf.
Or Text.XFormat.Show in xformat while you're at it. (Warning: blatant self-promoting.)
http://hackage.haskell.org/package/xformat
From the tests in the package:
showf (Show % Int % String) 4.05 20 " blah" == "4.0520 blah" showf (Wrap '(' Int ')') 24 == "(24)"
But with those, the number of arguments is still known at compile time, correct? Antoine

On Sunday 16 August 2009 8:35:19 pm Antoine Latter wrote:
But with those, the number of arguments is still known at compile time, correct?
{-# LANGUAGE Rank2Types #-} import Text.Printf import System.Environment nprintf :: PrintfType t => Int -> Int -> t nprintf n e = aux (printf str) n where str = concat $ replicate n "%d" aux :: PrintfType t' => (forall t. PrintfType t => t) -> Int -> t' aux pf 0 = pf aux pf n = aux (pf e) (n-1) main = do (n:e:_) <- map read `fmap` getArgs nprintf n e :: IO () ------------------------------------------------------------------------ % ./Var 2 5 55 % ./Var 6 5 555555 Voila. -- Dan
participants (5)
-
Antoine Latter
-
Brandon S. Allbery KF8NH
-
Dan Doel
-
Maciej Piechotka
-
Sean Leather