I would dearly love to be able to invoke my "fac" function using a
postfixed bang.
3! = 6, 4! = 24, etc. You get the idea.
Cheers,
Stephan
> Anyone know of a way to get Haskell to accept a postfix operator?
Operator section (modulo the parenthesis) ?
(!) :: Int -> Int
(!) 1 = 1
(!) x = x * (!) (x - 1)
Main> :load /tmp/test.hs
Main> (3!)
6
Main> (6!)
720
Main>
--
Didier Verna, did...@lrde.epita.fr, http://www.lrde.epita.fr/~didier
EPITA / LRDE, 14-16 rue Voltaire Tel.+33 (1) 44 08 01 85
94276 Le Kremlin-Bicętre, France Fax.+33 (1) 53 14 59 22 did...@xemacs.org
Here's what I have at the moment.
I use infixed bang for n-choose-k (a binary function).
e.g.
Main> 4!2
6
But I'd like to be able to invoke factorial (unary function):
Main> 4!
24
...but instead I just get:
Main> 4!
ERROR - Syntax error in expression (unexpected end of input)
This is what my Main.hs looks like:
-- Factorial
fac n = product [1..n]
-- n Choose k
choo n k = div (fac n) ((fac k) * (fac (n - k)))
infixr 5 !
(!) :: Int -> Int -> Int
x ! y = choo x y
-- I tried adding a pattern match (line #16):
13 infixr 5 !
14 (!) :: Int -> Int -> Int
15 x ! y = choo x y
16 x ! = x
but nooo.
Main> :l Main.hs
ERROR "Main.hs":16 - Syntax error in input (unexpected symbol "x")
Yeah, you can't overload a function to have multiple types.
Factorial is Integral a => a -> a, whereas choose is
Integral a => a -> a -> a.
You could define c to be choose and use it like
n `c` k.
--
Aaron Denney
-><-
Didier Verna <did...@lrde.epita.fr> wrote:
>Stephan <sla...@bellsouth.net> wrote:
>> Anyone know of a way to get Haskell to accept a postfix operator?
> Operator section (modulo the parenthesis) ?
>(!) :: Int -> Int
>(!) 1 = 1
>(!) x = x * (!) (x - 1)
>Main> :load /tmp/test.hs
>Main> (3!)
>6
>Main> (6!)
>720
>Main>
ghci doesn't like it:
Prelude> let (!) :: Int -> Int; (!) 1 = 1; (!) x = x * (!) (x - 1)
Prelude> (3!)
<interactive>:1:
Couldn't match `Int' against `t -> t1'
Expected type: Int
Inferred type: t -> t1
In the definition of `it': it = (3 !)
Prelude>
I.e. ghc wants the result of an operator section to be of some type
(a -> b)
Kind regards,
Hannah.
> I.e. ghc wants the result of an operator section to be of some type
> (a -> b)
This is consistent with expanding (x `f`) to \y -> x `f` y rather than f x,
like (`f` y) which has to be expanded to \x -> x `f` y.
GHCI:
| Prelude> (() `undefined`) `seq` ()
| ()
| Prelude> (`undefined` ()) `seq` ()
| ()
Hugs:
| Prelude> (() `undefined`) `seq` ()
|
| Program error: {undefined}
|
| Prelude> (`undefined` ()) `seq` ()
| ()
Hugs doesn't conform to Haskell 98 here.
--
__("< Marcin Kowalczyk
\__/ qrc...@knm.org.pl
^^ http://qrnik.knm.org.pl/~qrczak/
The first seems a tad more useful, though perhaps leading to more opaque
error messages when one does make a mistake.
Second of course, expanding to "f x".
> Yeah, you can't overload a function to have multiple types.
> Factorial is Integral a => a -> a, whereas choose is
> Integral a => a -> a -> a.
>
> You could define c to be choose and use it like
> n `c` k.
I may take this approach. Saves having to type the parens.