You need to learn to let go of type declartions, a lot of the time in F# you have no need for them, you could just write:

1
let quad x = 2.0

The function will return type float because 2.0 is a float. The parameter x is not used so it will be a generic parameter 'a. If you want to take an int and return a float you just need to explicitly convert your int to a float and the compiler will infer that the parameters an int:

1
let quad x = float x * 2.0

Chris Smith's F# in twenty minutes is probably the best tutorial for F# beginners:
[link:blogs.msdn.com]

"Foundations of F#" would probably help too if you were looking to buy a book.

Cheers,
Rob

By on 7/1/2008 5:03 AM ()

Hi Rob

Thanks for reply, But my purpose was different.
I had a function which takes an input X of type ABC.
ABC is user declared type which contains a 1 float and 2 integers.

1
2
3
4
5
6
type ABC=
  {
    a:int;
    b:int;
    c:float;
  }

I want to pass an instance of this type to a function and which returns a type int.

1
let quad x:ABC= (x.a + x.b)

But when i attempt this i get error. Can you please help in same ??

Thanks n Regards
Anu Viswan

By on 7/1/2008 5:10 AM ()

This too is okay without type annotations, because the type of record types can be infered from their members (if the members are unquie within the scope):

1
2
3
4
5
6
type ABC=
  { a:int;
    b:int;
    c:float; }

let quad x = (x.a + x.b)

But if you do need a type annotation for a parameter you need to put brackets round it, without brackets its the return type of the function that your are declaring:

1
let quad (x:ABC) = (x.a + x.b)

Cheers,
Rob

By on 7/1/2008 5:21 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