Can you post the full code giving trouble, and what error you get?

By on 4/12/2010 11:26 AM ()

Strangely I can not reproduce it. The error message was produced by F# editor (it was not a compilation error message; I was just reading Programming F# and using FSI) and was something like: "unexpected '-'..." in these snippets:

let (|Paragraph|Sentence|Word|WhiteSpace|) (input : string) =
let input = input.Trim()

if input = "" then
WhiteSpace
elif input.IndexOf(".") <> -1 then
// Paragraph contains a tuple of sentence counts and sentences.
let sentences = input.Split([|"."|], StringSplitOptions.None)
Paragraph (sentences.Length, sentences)
elif input.IndexOf(" ") <> -1 then
// Sentence contains an array of string words
Sentence (input.Split([|" "|], StringSplitOptions.None))
else
// Word contains a string
Word (input)

And

people.Sort(
{
new IComparer with
member this.Compare(l, r) =
if l.First > r.First then 1
elif l.First = r.First then 0
else -1
} )

But after I have closed Visual Studio and was doing some other stuff; and after re-opening VS (2008), It disappeared. I tried to reproduce it by creating a new project and performing same steps. But it did not worked.

Note for today 12 April: It was some years ago I saw F# and I hoped to see it as a Microsoft product. I'v played around with F# (like [link:cs.hubfs.net] and enjoyed it very much. Years passed and I was away from F#. I'v got matured (at least up to what I could!) and let go of many delusions of a dreamer. Today is that day that I can use F# as a Microsoft product and enjoy it. These are just simple and humble words to thank F# team (esp. Don Syme) and all others to make it a reality! I couldn't wait for VS 2010, so on last Friday I have downloaded VS 2008 extension and started to play with it. I am not sure if here is the proper place for this but

Cheers!

By on 4/12/2010 12:32 PM ()

I hit that again (The F# April 2010 Community Technology Preview, 2.0.0.0). With this:

people.Sort(
{
new IComparer with
member this.Compare(l, r) =
if l.First > r.First then 1
elif l.First = r.First then 0
else −1
} )

And this:

people.Sort(
{
new IComparer with
member this.Compare(l, r) =
if l.First > r.First then 1
elif l.First = r.First then 0
else −1
} )

And error message is:

Error 2 Unexpected character '−' in if/then/else expression C:\Users\...\C6DotNetProgramming.fs 111 45 Funtastic

This is the content of fs file:

module C6DotNetProgramming

// CLI
// ...

// Garbage Collection
// ... (IDisposable)

//*****************************************
// Interfaces
//*****************************************

// Example: Interfaces in F#
type Tastiness =
| Delicious
| SoSo
| TrySomethingElse

type IConsumable =
abstract Eat : unit -> unit
abstract Tastiness : Tastiness

// Protip: Eat one of these a day
type Apple() =
interface IConsumable with
member this.Eat() = printfn "Tastey!"
member this.Tastiness = Delicious

// Not that tastey, but if you are really hungry will do a bind
type CardboardBox() =
interface IConsumable with
member this.Eat() = printfn "Yuck!"
member this.Tastiness = TrySomethingElse

//-----------------------------------------
// Using Interfaces

(*
Unlike other .NET languages, in F#, the methods and properties of implemented interfaces
are not in scope by default, so in order to call interface methods, you must cast
the type to the corresponding interface. Once you have an instance of the interface,
that object can do exactly what the interface specifies, no more and no less.
*)

let apple = new Apple()
// apple.Tastiness;; -> error
let iconsumable = apple :> IConsumable
// iconsumable.Tastiness;;

//-----------------------------------------
// Defining Interfaces

(*
To create an interface, you simply define a new class type with only abstract methods.
The type inference system will infer the type to be an interface. Alternatively, you can
use the interface keyword, followed by abstract members and properties, followed by
the end keyword.
*)

// Define a type inferred to be an interface
type IDoStuff =
abstract DoThings : unit -> unit

// Define an interface explicitly
type IDoStuffToo =
interface
abstract member DoThings : unit -> unit
end

(*
Interfaces can inherit from one another, with the derived interface extending the contract
of the first. However, consumers must implement the entire inheritance chain,
implementing each interface fully.
*)

//*****************************************
// Object Expressions
//*****************************************

//-----------------------------------------
// Object Expressions for Interfaces

// Example 6-3. Sorting a list using IComparer<'a>
open System.Collections.Generic

type Person =
{ First : string; Last : string }
override this.ToString() = sprintf "%s, %s" this.Last this.First

let people =
new List<_>(
[|
{ First = "Jomo"; Last = "Fisher" }
{ First = "Brian"; Last = "McNamara" }
{ First = "Joe"; Last = "Pamer" }
|] )

let printPeople () =
Seq.iter (fun person -> printfn "\t %s" (person.ToString())) people

printfn "Initial ordering:"
printPeople ()

(*
Error 2 Unexpected character '−' in if/then/else expression C:\Users\ShahKaveh\Documents\Visual Studio 2008\Projects\Funtastic\Funtastic\C6DotNetProgramming.fs 111 45 Funtastic
*)

// Sort people by first name
people.Sort(
{
new IComparer with
member this.Compare(l, r) =
if l.First > r.First then 1
elif l.First = r.First then 0
else −1
} )

printfn "After sorting by first name:"
printPeople ()

</i><b>How to resolve (hack?):</b><i> I have wrote this code: let minusOne = fun () -> -1 // Sort people by first name people.Sort( { new IComparer with member this.Compare(l, r) = if l.First > r.First then 1 elif l.First = r.First then 0 else minusOne () } ) And the compiled the solution; It compiled. Then I have changed it to this: // Sort people by first name people.Sort( { new IComparer with member this.Compare(l, r) = if l.First > r.First then 1 elif l.First = r.First then 0 else -1 } ) And It compiled without error again. Note: I have typed this at first (time): people.Sort( { new IComparer with member this.Compare(l, r) = if l.First > r.First then 1 elif l.First = r.First then 0 else -1 } ) And then added tabs (in my VS == 4 spaces) by selecting lines and pressing tabs (if it helps). I apologize in advance if a too basic mistake presented here.

By on 4/14/2010 5:10 AM ()

I'm getting exactly the same problem, but only when I initially paste the code above into a project. Then, once I solve the problem by any means, it goes away.

I can usually (but not always) solve it by surrounding the -1 in parens: (-1). That causes two errors, but then when I remove the parens, the error stops. I can also solve it by doing as you did and making the -1 into a function, compiling it, and then making the -1 back into a literal.

I'm trying to find the simplest set of circumstances that will reproduce it.

But, like you, I'm also fairly new to F#. Perhaps as you indicated, it's something fairly basic that I'm missing?

-Neil

By on 4/14/2010 7:36 AM ()

I think I may have figured it out. The character being pasted in front of the "1" is not ASCII "-" (0x2D); the actual string in front of the "1" is 0xE2 0x88 0x92 (some MBCS hyphen). Editing it just has the effect of getting those characters out and usual ASCII 0x2D hyphen in.

-Neil

By on 4/14/2010 8:01 AM ()

It is not just that characters that my eyes can't see; ultra violet too! ;) I'm sure you are totally correct! (Of-course that's it!)

Thank you!

By on 4/14/2010 8:07 AM ()

You're welcome; I hope it works out.

I used to help prep test files from various clients for input to a document processing system. My mind now naturally jumps to the "bad-character-not-revealed-by-text-editor" hypothesis, lol.

-Neil

By on 4/16/2010 7:35 AM ()
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