I have the addition function:
let addition a b = a + b
but I want specify the types:
let addition(a: int, b: int): int = a + b
The question is I want specify the type without use a tuple to build the function. For example, with a incorrect syntax:
let addition a:int,b:int :int = a + b
The syntax you want is:
let addition (a : int) (b : int) : int = a + b
I've put spaces before and after each colon because that's my preference, but you don't have to have those spaces. You could also write:
let addition (a: int) (b: int): int = a + b
and that would work as well.
Related
I want to define an overloaded member function in F#
member o.m1(?x: int) =
o.m1("bugs bunny", x) // <- error, expects a straight int, but x is int option
member o.m1(s: string, ?x: int) =
42
but the code above fails. I can solve this so:
member o.m1(?x: int) =
match x with
| Some x -> o.m1("bugs bunny", x)
| _ -> o.m1("bugs bunny")
I wonder if it is possible to avoid this switch.
You can do it by explicitly naming the optional parameter, like this:
member o.m1(?x: int) =
o.m1("bugs bunny", ?x = x)
I was trying to create a random fractional number as follows:
type Fraction=
{
num: int
den: int
}
let makeRandomFraction i =
let n= fun i -> System.Random(i).Next(-50, 51)
let d= fun i -> System.Random(i*3).Next(-50, 51)
{num=n; den=d}
but I am getting an error:
error FS0001: This expression was expected to have type
'int'
but here has type
'int -> int'
Could you please explain the error and the correct way of doing the required.
The error is saying that you're passing a function (of type int -> int) where an int is expected. You can see this more clearly in something like:
let add a b = a + b
let inc x = x + 1
inc add
// ^^^
// This expression was expected to have type 'int' but has type 'int -> int -> int'
The solution here is just to take out the fun i -> parts. That's the (other) syntax for lambdas, but since your i variable is already in scope there's no need to create a function around it.
Out of curiosity, do you have any requirements on what range and distribution of fractions do you want your function to generate? The way the code is currently written - by composing two random numbers in a range -50 .. 50, you will get a distribution with most numbers being close to zero.
Here is a simple histogram built using the XPlot F# library:
open XPlot.GoogleCharts
type Fraction=
{ num: int
den: int }
let makeRandomFraction i =
let n = System.Random(i).Next(-50, 51)
let d = System.Random(i*3).Next(-50, 51)
{num=n; den=d}
[ for i in 0 .. 100000 -> let f = makeRandomFraction i in float f.num / float f.den ]
|> Seq.filter (System.Double.IsInfinity >> not)
|> Seq.countBy (fun f -> int f)
|> Chart.Column
type Test() =
member t.A(a: int -> int) (b: int) = ()
let cl = Test()
let f a b =
(^a : (member A: (int -> int) -> int -> unit) cl, a, b)
The above says that it cannot find the member or object contructor A. Is it possible to get the above to work somehow?
No, there's no way to call it without changing its definition.
I'm not sure if it's in the spec but you can find that restriction in the source code where it filters out curried members.
Anyway if you want to use curried arguments you can use a lambda function and it will work:
type Test() =
member t.A(a: int -> int) = fun (b: int) -> ()
let cl = Test()
let f a b =
(^a : (member A: (int -> int) -> (int -> unit)) cl, a) b
I have the following and I need to call the "functiontocall" in another script but need to constrain the inputs to ints. However the maths in the functions it calls all need float types.
let functionone (x: float) (y :float) = x/y
let functiontocall (a: int) (b: int) = functionone a b
|> functiontwo
|> functionthree
What would be the best thing to do to cast these as float?
Casting in F# is just using the type name in an expression:
functionone (float a) (float b)
I have three functions that ought to be equal:
let add1 x = x + 1
let add2 = (+) 1
let add3 = (fun x -> x + 1)
Why do the types of these methods differ?
add1 and add3 are int -> int, but add2 is (int -> int).
They all work as expected, I am just curious as to why FSI presents them differently?
This is typically an unimportant distinction, but if you're really curious, see the Arity Conformance for Values section of the F# spec.
My quick summary would be that (int -> int) is a superset of int -> int. Since add1 and add3 are syntactic functions, they are inferred to have the more specific type int -> int, while add2 is a function value and is therefore inferred to have the type (int -> int) (and cannot be treated as an int -> int).