Unfortunately it looks like you've stumbled upon a bug; that code should work just fine according to the spec. This issue has been logged as 2541, "Unable to define symbolic operator (!!)" in our database.

By on 6/27/2008 2:22 PM ()

Hi,

This is not a bug, this is a feature. :)

The compiler uses the first character (ignoring leading . and $) in operators name to determine their precedence, and if they are prefix/infix. The "!" operator is prefix (think of the 'a ref type), and so are all operators starting with "!" and "?". Have a look in the manual for more details about it: [link:research.microsoft.com]

So, the solution you're looking for is to rename your operator (or at least, change its first character).

There's a more F#-ish solution. You access arrays with the array.[ n ] notation. So, why not overloading the .[] operator, so that it works with lists too?

1
2
3
4
5
6
type List<'a> with
  member x.Item with get n = List.nth x n

> [1;2;3].[1];;

val it : int = 2

Then you might wonder why this is not done in the F# standard library. The thing is you don't have random access on lists. Accessing the nth element requires n operations. Most of the time, you don't want to access the nth element in a list. If you need random access, you should consider using an array (or ResizableArray). Each time you write "List.nth", just think if it's the right thing to do. :)
I agree, it's sometimes useful, but it's quite rare, actually.

Laurent.

By on 6/27/2008 4:38 PM ()

Thanks. I also posted this as a question on the F# mailing list and Don's reply was:

"F# and Ocaml pre-choose fixities for symbolic operators. All operators beginning with ! are prefix."

Given that I've never had a "Fixation for fixities" as it were, here is some more information:

....Operators are more flexible in their behavior than functions. Using brackets any desired execution order can be completely specified. However, relying on their associativity and fixity most brackets can be saved. Fixity is what is also called binding power or priority in other languages. It tells us which operator to apply first when in an expression operators with different fixities appear (such as "*" and "+")...

Taken from: [link:users.informatik.uni<WBR>-halle.de]

Thanks again,

Dan

By on 6/27/2008 5:10 PM ()

This is by design - operators beginning with "!" are always prefix operators.

See the table at [link:research.microsoft.com]

Kind regards

don

By on 6/27/2008 4:32 PM ()

Interestingly enough, ($!!) works as well, with

1
2
>[1;2;3;4;5] $!! 2;;
val it: int = 3

(@!! also works, as does +!!...)

By on 6/27/2008 12:19 PM ()
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