This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Direction = | |
| Up | |
| Down | |
| Left | |
| Right | |
override this.ToString() = | |
match this with | |
| Up -> "Up" | |
| Down -> "Down" | |
| Left -> "Left" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Example() = | |
member __.Getter = doComputation() | |
// It's clearer if you use the long syntax: | |
type Example() = | |
member __.Getter with get() = doComputation() | |
// What you probably wanted to do is: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type TickTack = Tick | Tack | |
let ticker x = | |
match x with | |
| Tick -> printfn "Tick" | |
| Tock -> printfn "Tock" | |
| Tack -> printfn "Tack" | |
ticker Tick | |
ticker Tack |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let main() = | |
let myIdent = "something ..." | |
... | |
... | |
... | |
let myFunc() = | |
let myIdent = "something else ..." | |
... | |
... | |
myIdent |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async { | |
let wc = new WebClient() | |
let html = wc.DownloadString(uri) | |
printfn "%s" html } | |
// To make code in F# async workflow actually asynchronous, | |
// we need to use a primitive operation of type Async<'T> | |
// (implemented usually in the F# library as an extension) and call it using let! or do!: | |
async { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let plusOne (s : seq<_>) = | |
let e = s.GetEnumerator() | |
seq { | |
while e.MoveNext() do | |
yield e.Current + 1 | |
} | |
let r = plusOne [1; 2; 3] | |
printfn "%A" r // prints [2; 3; 4] | |
printfn "%A" r // prints [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let func = | |
printfn "hi" | |
1 | |
// vs. | |
let func() = | |
printfn "hi" | |
1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rec loop () = async { | |
do! Async.Sleep(1000) | |
do! loop() } // Wrong! | |
// In order to write tail-recursive calls in F# async workflows, we should use return!: | |
let rec loop () = async { | |
do! Async.Sleep(1000) | |
return! loop() } // Correct :-) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Preassign the divider with some configurations. | |
member x.preassignBB(level, threadId) = | |
for l in 1..level do | |
if threadId &&& (pown 2 l) <> 0 then | |
path.[depth] <- 1uy | |
totalValues.[1] <- totalValues.[1] + itemValues.[depth] | |
totalUnassigned <- totalUnassigned - itemValues.[depth] | |
depth <- depth + 1 | |
else | |
path.[depth] <- 0uy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using branch and bound method to prune unused branches. | |
member x.divideBB() = | |
if depth >= numItems then | |
if abs(totalValues.[0] - totalValues.[1]) < bestDifference then | |
Array.Copy(totalValues, bestTotalValues, size) | |
bestDifference <- abs(totalValues.[0] - totalValues.[1]) | |
else () | |
elif abs(totalValues.[0] - totalValues.[1]) - totalUnassigned <= bestDifference then | |
for i in 0..1 do | |
x.selectBB(i) |