
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 26/07/13 22:08, Micah Cowan wrote:
So, just for fun, I came up with a way to abuse the language in order to define a "function" whose argument is optional:
-- dirty-trick.hs - A sneaky way to do var args fns in Haskell
{-# LANGUAGE FlexibleInstances #-}
class Hello a where hello :: a
instance Hello (String -> String) where hello = (\str -> "Hello " ++ str)
instance Hello String where hello = hello "World"
In ghci,
putStrLn $ hello
gives "Hello World", while
putStrLn $ hello "There"
gives "Hello".
.
I was wondering if there was a way to do it in "pure" Haskell (i.e., no GHC pragmas required), and also the specific reason for why the above example doesn't work without the pragma (I think it's just that in general a -> b is not syntactically allowed for type specifiers within instance declarations)?
I'm also interested in alternative approaches to creating variable-argument functions, if there are any.
.
If anyone's curious, this was prompted by a discussion with a friend (copied), about Haskell and Clojure. He mentioned that Clojure can accept variable arguments, though AFAICT all Clojure functions basically act like they take a list (that supports variable types), so accepting an empty argument list is a bit analogous to Haskell accepting an empty list, rather than no arguments.
Part of the reason Haskell can't really take "variable arguments" is that all Haskell functions really just take one argument. But I figured you could use the contextually expected type to decide whether to return a simple value (not /technically/ a function in that case), or a function expecting further arguments, which could then be extended to define a function taking any arbitrary number of arguments.
Cheers! -mjc
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
You might be interested in Oleg's http://okmij.org/ftp/Haskell/polyvariadic.html - -- Mateusz K. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.20 (GNU/Linux) iQIcBAEBAgAGBQJR8ujnAAoJEM1mucMq2pqXe00P/jdRyQCD3CSwW2/7vqpFyQsn HGx4RAxAQWssyQ9UP6vv+rXAVay7uzVARYUgHoP76FcVrgIN2T+5F9pAIn9YOZK6 MCXCRMpsX8qRB83s11RiydUyFGTUiTKPrw+SDz4ZSK2zy4M6kTWA1X2CwnYtQpwM ghqxuTSSJbmLCyDxSqMbg/BORJdcmPmWAuNNEI2euhVanDaRJGb7uqBsAW/scqcr D//HNPs81wG7weDE8f3ANAsh6cP/91wQ5VFPigQSKwiwlG8ZQZ6QUT8GnDotvwMx gc8mVu9ArHGgJcrTc6q6tw17P2BvrDeOKyQeoc6RVLcA+Usp9McdcUb9K55nHfdP TZUO1i3+YgefpIVEkfnMW9S0lRkU9Avd0K3KTVeAa3u2TxqBLQ592+rbvZPbJ5Fd XvAkNZdh3SRIIZ2TFCQXTcQIi4TmuC/GG7SwfFvpPQrow8WrF8C+UwKPJ1CpAlpb CXp8WWtagJQ/NL0GunygXwK2WiI5OhWUdfyoz5OVzAzztA1q+Ld0b3dBsWlmRPHj XH1jBgxFB8cNi9GzkC51LE4u0p13k3IS0x/dzesYFEBPW9Cn3wl9AtgItyhF1Kyx TnNrkKfiJD/Vhq5c0/Lky4C99/kx/g78lGVdpwzV8INwgCH1AVGGQi0jjN0PRUbF sCuWGqPXDD1hvYvaOEE1 =o29R -----END PGP SIGNATURE-----