Hide this comment

Hi,

(!) is indeed an indentifier name like aa or foo. You can basically use it everywhere you could use aa.

Try this, it should work also:

let (!) = 2 in (!) + 3

match [1..5] with (!)::li ->(!) | _ -> 0

let f (!) = (!) + 1

Laurent

By on 9/21/2009 11:57 AM ()Reply
Hide this comment

Hi,

(!) is indeed an indentifier name like aa or foo. You can basically use it everywhere you could use aa.

Try this, it should work also:

let (!) = 2 in (!) + 3

match [1..5] with (!)::li ->(!) | _ -> 0

let f (!) = (!) + 1

Laurent

Yes and No :-)

No:
MSDN says that "!" is indeed OPERATOR:
[link:msdn.microsoft.com]
So in my factorial example really we are defining prefix operator.

Yes:
What will be done with (!) really depends on the context.
To see this we only need to print AST (abstract syntax tree) for the some examples:

printf "%A" <@ let f (!) = (!) + 1
f 5 @>

printf "%A" <@ let (!) x = x + 1
!5 @>

printf "%A" <@ let (!) = 15
(!) @>

What we get is:

Let (f,
Lambda (op_Dereference,
Call (None, Int32 op_Addition[Int32,Int32,Int32](Int32, Int32),
[op_Dereference, Value (1)])), Application (f, Value (5)))

Let (op_Dereference,
Lambda (x,
Call (None, Int32 op_Addition[Int32,Int32,Int32](Int32, Int32),
[x, Value (1)])), Application (op_Dereference, Value (5)))

Let (op_Dereference, Value (15), op_Dereference)

----
NOTE that (!) is translated into "op_Dereference" keyword into AST. This also shows already that "!" is INDEED operator :-)
But yes - what will be done with ! operator depends on the context of execution.

1. example - operator ! acts as function parameter.
2. example - operator ! acts as lambda function.
3. example - operator ! acts as some value.

So it seems when we are defining operators, in reality we are defining some lambda function.

By on 9/21/2009 11:56 PM ()Reply
Hide this comment

I have a quasi-related question. Does anyone know how to define postfix operators in F#? or are we limited to prefix and infix operators only?

Thanks.

By on 9/21/2009 2:13 PM ()Reply
Hide this comment

Only prefix and infix, see e.g.

[link:research.microsoft.com]

By on 9/21/2009 2:21 PM ()Reply
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper

Logging in...