How to compare in ResizeArray at Fsharp? - f#

How to Sort s by funtion compare : cmp ?
type Point =
{
x : float
y : float
}
let s = ResizeArray<Point>()
s.Add{x=1.1 ; y=1.1}
s.Add{x=2.2 ; y=2.2}
s.Add{x=3.3 ; y=3.3}
s.Add{x=2.2 ; y=2.0}
let cmp (A:Point) (B:Point) =
A.x + A.y < B.x + B.y
s.Sort()
//s.Sort()
s |> printfn("%A")
And this's result , but it's not correct with function : cmp
seq [{x = 1.1;
y = 1.1;}; {x = 2.2;
y = 2.2;}; {x = 2.2;
y = 2.0;}; {x = 3.3;
y = 3.3;}]
I tried using sortWith but it still not correct . Can you help me to fix ?
type Point =
{
x : float
y : float
}
let s = ResizeArray<Point>()
s.Add{x=1.1;y=1.1}
s.Add{x=2.2;y=2.2}
s.Add{x=1.0;y=1.0}
let cmp (A:Point) (B:Point) =
if (A.x+A.y) >= (B.x+B.y) then 1
else 0
List.sortWith cmp s |> List.iter (fun x -> printfn("%f %f\n") x.x x.y)

You have to provide a Comparison delegate for your type:
s.Sort(fun a b -> a.x + a.y - b.x - b.y |> int)

Related

Represent finite coordinates as an F# type

I'm learning F# by implementing a board game engine. In accordance with the principle that invalid states should not be representable, I want to create a type which represents (x, y) coordinates (with x and y integers) where x and y must satisfy 0 <= x <= n and 0 <= y <= m. Creating an instance that does not satisfy those inequalities shouldn't compile. Is this possible?
I don't think compile time assurances on values like this are possible.
I would handle this with a private constructor and the Result or Option type depending whether I wanted to propagate error information.
type Position =
private {
X: int
Y: int
}
static member TryNew(x, y) =
if x >= 0 && x <= 10 then
if y >= 0 && y <=10 then
Ok {
X = x
Y = y
}
else
Error $"Supplied y ({y}) must be between 0 and 10"
else
Error $"Supplied x ({x}) must be between 0 and 10"
module Position =
let moveX1 pos =
Position.TryNew(pos.X + 1, pos.Y)
This does create the issue where Results are now everywhere in your code but they don't have to be visible or cause you a lot of typing. This problem is precisely what computation expression libraries e.g. FsToolkit.ErrorHandling are designed to eliminate.
let example =
result {
let! start = Position.TryNew(0,0)
let! next = start |> moveX1
}
There's no way to do this that takes integers as input, but it can be done with a type that only provides integers as output:
type XCoord = X0 | X1 | X2 with
member this.Int =
match this with
| X0 -> 0
| X1 -> 1
| X2 -> 2
static member (-)(xa : XCoord, xb: XCoord) =
xa.Int - xb.Int
type YCoord = Y0 | Y1 | Y2 with
member this.Int =
match this with
| Y0 -> 0
| Y1 -> 1
| Y2 -> 2
static member (-)(ya : YCoord, yb: YCoord) =
ya.Int - yb.Int
type Point = MkPoint of (XCoord * YCoord) with
member this.Ints =
let (MkPoint (x, y)) = this
x.Int, y.Int
static member (-)((MkPoint (ax, ay)), (MkPoint (bx, by))) =
ax - bx, ay - by
let ptA = MkPoint (X0, Y1)
let ptB = MkPoint (X2, Y0)
printfn "%A" ptA.Ints
printfn "%A" ptB.Ints
printfn "%A" (ptA - ptB)
I think this would be pretty clunky to use in practice, but it might work, depending on exactly what you want to do with it. I guess it's a case of "careful what you ask for". 😀

Nested loop and functional programming

Please consider a C program that, given x, will return y and z such that y + z * 2 = x, for the smallest possible y. Roughly, I could create a nested loop:
for(y = 0; y < x; ++ y){
for(z = 0; z < x; ++z){
if(y + 2 * z == x){
printf("%d + 2 * %d = %d", y, z, x);
}
}
}
How could I translate this kind of nested loop in the functional way? Is it feasible? Is it reasonable or am I just misjudging the approach? My best attempt so far:
let foo x =
let rec aux (y, z, q) =
match (y + z * 2) with
r when r = q -> (y, z)
|_ -> aux(y + 1, z + 1, q) //How to check different values of z
aux(0, 0, x) //for each value of y?
It will not work, since it will just increment both y and z. How can I check different values of z, for every value of y?
You have to add those checks in the match.
See here what your code is missing:
let foo x =
let rec aux (y, z, q) =
match (y + z * 2) with
| r when r = q -> (y, z)
| _ when y = q -> failwith "not found !"
| _ when z = q -> aux (y + 1, 0, q)
| _ -> aux (y, z + 1, q)
aux (0, 0, x)
And here's a different approach, equally functional but without recursion:
let foo2 x =
let s =
{0 .. x} |> Seq.collect (fun y ->
{0 .. x} |> Seq.collect (fun z ->
seq [y, z]))
Seq.find (fun (y, z) -> y + z * 2 = x) s
which in F# can be written using seq expressions:
let foo3 x =
let s = seq {
for y in {0 .. x} do
for z in {0 .. x} do
yield (y, z)}
Seq.find (fun (y, z) -> y + z * 2 = x) s
and it resembles your original C program.

F#: Derivative Function

I'm trying to figure out how to get this function to work. I'm very inept with F#, so explanations are appreciated,
let deriv (f:(float->float), dx: float) =
fun f:(float -> float) * dx:float -> x:float -> float
let (f, dx, x) = ((f(x + dx) - f(x))/dx)
Am I using f:(float->float) wrong?
In stead of trying to fix your problems I will explain it from the ground up.
The symbolic derivative is a function that takes a function and returns a new function. However you are trying to calculate the numeric derivative that returns a value given a function, a value, and a delta.
First we will give the function a name: deriv
and it needs three parameters:
1. A function that takes in a float and returns a float: (f : float -> float)
2. A value of where the derivative is to be evaluated: (x0 : float)
3. A delta: (dx : float)
You had two of the three parameters in your example, but were missing x0.
It should also return a float.
So the signature is
deriv (f : (float -> float)) (x0 : float) (dx : float) : float
Now to calculate the numeric derivative.
I won't explain this, but just reference derivative
For this example we'll use a simple function that has derivative, x^2.
Here is the code in F#
// val deriv : f:(float -> float) -> x0:float -> dx:float -> float
let deriv (f : (float -> float)) (x0 : float) (dx : float) : float =
let (x1 : float) = x0 - dx
let (x2 : float) = x0 + dx
let (y1 : float) = f x1
let (y2 : float) = f x2
let (result : float) = (y2 - y1) / (x2 - x1)
result
// val f : x:float -> float
let f x = x**2.0
and a quick test show it works correctly.
// val it : float = 2.0
deriv f 1.0 0.000005
For a more thorough test of a range of values.
Using Visual Studio and NuGet install FSharp.Charting
In F# Interactive
#I "..\packages"
#load "FSharp.Charting.0.90.13\FSharp.Charting.fsx"
open FSharp.Charting
let xs1 = [ for x in (double)(-3.10) .. 0.05 .. 3.10 do yield x]
let ys1 = xs1 |> List.map f
let values1 = List.zip xs1 ys1
Chart.Line(values1)
.WithXAxis(Min=(-4.0), Max=4.0, MajorTickMark = ChartTypes.TickMark(Interval=2.0, IntervalOffset = 1.0, LineWidth = 2))
.WithYAxis(Min=(0.0), Max=10.0, MajorTickMark = ChartTypes.TickMark(Interval=2.0, IntervalOffset = 1.0, LineWidth = 2))
which can also be confirmed using Wolfram Alpha: x^2
A simpler example:
// val d : x:float -> float
let d x = deriv f x 0.00000005
let xs2 = [ for x in (double)(-3.10) .. 0.05 .. 3.10 do yield x]
let ys2 = xs2 |> List.map d
let values2 = List.zip xs2 ys2
Chart.Line(values2)
.WithXAxis(Min=(-4.0), Max=4.0, MajorTickMark = ChartTypes.TickMark(Interval=2.0, IntervalOffset = 1.0, LineWidth = 2))
.WithYAxis(Min=(-6.0), Max=6.0, MajorTickMark = ChartTypes.TickMark(Interval=2.0, IntervalOffset = 1.0, LineWidth = 2))
which can also be confirmed using Wolfram Alpha: d/dx x^2

can we have more generic type to shorten this code in F#?

I came across this code while reading "Programming F# 3.0":
type BitCounter =
static member CountBits (x : int16) =
let mutable x' = x
let mutable numBits = 0
for i = 0 to 15 do
numBits <- numBits + int (x' &&& 1s)
x' <- x' >>> 1
numBits
static member CountBits (x : int) =
let mutable x' = x
let mutable numBits = 0
for i = 0 to 31 do
numBits <- numBits + int (x' &&& 1)
x' <- x' >>> 1
numBits
static member CountBits (x : int64) =
let mutable x' = x
let mutable numBits = 0
for i = 0 to 63 do
numBits <- numBits + int (x' &&& 1L)
x' <- x' >>> 1
numBits
I attempted to shorten this part by making an auxiliary function:
type BitCounter =
static member CountBitsWithRangedMask x upBound typeConverter =
seq { for i = 0 to upBound do yield 1 <<< i }
|> Seq.map typeConverter
|> Seq.map ((&&&) x)
|> Seq.filter ((<>) (typeConverter 0))
|> Seq.length
static member CountBits (x : int16) =
BitCounter.CountBitsWithRangedMask x 15 int16
static member CountBits (x : int) =
BitCounter.CountBitsWithRangedMask x 31 int
static member CountBits (x : int64) =
BitCounter.CountBitsWithRangedMask x 63 int64
But static member CountBits (x : int) caused a compilation error:
error FS0001: This expression was expected to have type
int16
but here has type
int
So I'm wondering if I can add some constraint like Integral a in Haskell on CountBitsWithRangedMask's first argument.
Or is there any other solution that can simplify the original code?
You can use LanguagPrimitives.GenericOne and LanguagPrimitives.GenericZero combined with inline to acheive this.
let inline CountBitsWithRangedMask x upBound =
seq { for i = LanguagePrimitives.GenericZero to upBound do yield LanguagePrimitives.GenericOne <<< i }
|> Seq.map ((&&&) x)
|> Seq.filter ((<>) (LanguagePrimitives.GenericZero))
|> Seq.length
let test16 (x:System.Int16) = CountBitsWithRangedMask x 15
let test32 (x:System.Int32) = CountBitsWithRangedMask x 31
let test64 (x:System.Int64) = CountBitsWithRangedMask x 63
Using this we also don't need the typeConverter parameter as the compiler does everything automatically.
This is a reasonably general method for these sorts of problems.

Hindley Milner Type Inference in F#

Can somebody explain step by step type inference in following F# program:
let rec sumList lst =
match lst with
| [] -> 0
| hd :: tl -> hd + sumList tl
I specifically want to see step by step how process of unification in Hindley Milner works.
Fun stuff!
First we invent a generic type for sumList:
x -> y
And get the simple equations:
t(lst) = x;
t(match ...) = y
Now you add the equation:
t(lst) = [a] because of (match lst with [] ...)
Then the equation:
b = t(0) = Int; y = b
Since 0 is a possible result of the match:
c = t(match lst with ...) = b
From the second pattern:
t(lst) = [d];
t(hd) = e;
t(tl) = f;
f = [e];
t(lst) = t(tl);
t(lst) = [t(hd)]
Guess a type (a generic type) for hd:
g = t(hd); e = g
Then we need a type for sumList, so we'll just get a meaningless function type for now:
h -> i = t(sumList)
So now we know:
h = f;
t(sumList tl) = i
Then from the addition we get:
Addable g;
Addable i;
g = i;
t(hd + sumList tl) = g
Now we can start unification:
t(lst) = t(tl) => [a] = f = [e] => a = e
t(lst) = x = [a] = f = [e]; h = t(tl) = x
t(hd) = g = i /\ i = y => y = t(hd)
x = t(lst) = [t(hd)] /\ t(hd) = y => x = [y]
y = b = Int /\ x = [y] => x = [Int] => t(sumList) = [Int] -> Int
I skipped some trivial steps, but I think you can get how it works.

Resources