Skip to content

Instantly share code, notes, and snippets.

@murasesyuka
Created March 30, 2017 12:29
Show Gist options
  • Select an option

  • Save murasesyuka/90228b887a2a702ef49e797a9f323387 to your computer and use it in GitHub Desktop.

Select an option

Save murasesyuka/90228b887a2a702ef49e797a9f323387 to your computer and use it in GitHub Desktop.
read & write. memo F# Cheatsheet
/// https://dungpa.github.io/fsharp-cheatsheet/
open Microsoft.FSharp.Core
open Microsoft.FSharp.Collections
let result = 1 + 1 = 2
let hello = "hello" + "world"
let verbatimXml = @"<book title=""Paradise Lost"">"
let tripleXml = """<book title="Paradise Lost">"""
let poem =
"The lesser world was daubed\n\
By a colorist of modest skill\n\
A master limned you in the finest inks\n\
And with a fresh-cut quill."
let b, sb, i, l = 86uy, 86y, 86, 86L
let s, f, d, bi = 4.14F, 4.14, 0.7833M, 9999I
let negate x = x * -1
let square x = x * x
let print x = printfn "The number is: %d" x
let squareNegateThenPrint x =
print (negate (square x))
squareNegateThenPrint 5
let ``square, negate, then print`` x =
x |> square |> negate |> print
``square, negate, then print`` 4
let sumOfLength (xs : string []) =
xs
|> Array.map (fun x -> x.Length)
|> Array.sum
sumOfLength (List.toArray ["foo";"bar";"xyzzy"])
let squareNegateThenPrint' =
square >> negate >> print
squareNegateThenPrint' 6
let squareNegateThenPrint'' x =
print (negate (square x))
squareNegateThenPrint'' 6
let rec even x =
if x = 0 then true
else odd (x-1)
and odd x =
if x = 1 then false
else even (x-1)
let rec fib n =
match n with
| 0 -> 0
| 1 -> 1
| _ -> fib (n-1) + fib (n-2)
let sign x =
match x with
| 0 -> 0
| x when x < 0 -> -1
| x -> 1
sign -5
let fst' (x, _) = x
fst (7,4)
let seq1 =
seq {
yield 1
yield 2
yield! [5..10]
}
let xs = [1..2..9]
let ys = [|for x in 0..4 -> x * 2 + 1|]
let zs = List.init 5 (fun x -> x * 2 + 1)
let xs' = Array.fold (fun s n -> sprintf "%s, %d" s n) "" [|0..9|]
let last xs = List.reduce (fun acc x -> x) xs
let ys' = Array.map (fun x -> x*x ) [|0..9|]
let _ = List.iter (printfn "%i") [0..9]
let a = List.reduce
let b = Array.fold
let c = Array.map
let d = List.iter
last [0..9]
let zs' =
seq {
for i in 0..9 do
printfn "Adding %d" i
yield i
}
zs'
// Recode
type Person = { Name : string; Age : int }
let paul = { Name = "Paul"; Age = 28 }
let paulsTwin = { paul with Name = "Jim" }
type Person with
member x.Info = (x.Name, x.Age)
paul.Info
paulsTwin.Info
let isPaul person =
match person with
| {Name = "Paul"} -> true
| _ -> false
isPaul paul
isPaul paulsTwin
type Tree<'T> =
| Node of Tree<'T> * 'T * Tree<'T>
| Leaf
let rec depth = function
| Node(l, _, r) -> 1 + max (depth l) (depth r)
| Leaf -> 0
depth (Node(Leaf, 3, Leaf))
depth (Node(Node(Leaf,1,Leaf), 3, Leaf))
type OrderId = Order of string
let orderId = Order "12"
let (Order id) = orderId
exception InnerError of string
exception OuterError of string
let handleErrors x y =
try
try
if x = y then raise (InnerError("inner"))
else raise (OuterError("outer"))
with InnerError(str) ->
printfn "Error1 %s" str
finally
printfn "Always print this."
handleErrors 5 5
type Vector(x : float, y : float) =
let mag = sqrt(x * x + y * y)
member foo.X = x
member this.Y = y
member this.Mag = mag
member this.Scale(s) =
Vector(x * s, y * s)
static member (+) (a : Vector, b : Vector) =
Vector(a.X + b.X, a.Y + b.Y)
type Animal() =
member this.Rest() = 21
type Dog() =
inherit Animal()
member __.Run() =
base.Rest()
//Upcasting is denoted by :> operator.
let dog = Dog()
dog.Run ()
let animal = dog :> Animal
//Dynamic downcasting (:?>) might throw an InvalidCastException if the cast doesn't succeed at runtime.
let shouldBeADog = animal :?> Dog
type IVector =
abstract Scale : float -> IVector
type Vector'(x, y) =
interface IVector with
member __.Scale(s) =
Vector'(x * s, y * s) :> IVector
member __.X = x
member __.Y = y
type ICustomer =
abstract Name : string
abstract Age : int
let createCustomer name age =
{ new ICustomer with
member __.Name = name
member __.Age = age }
/// Active Pattern // http://7shi.hateblo.jp/entry/2012/12/16/010942
let (|Even|Odd|) i =
if i % 2 = 0 then Even else Odd
(|Even|Odd|) 3
let testNumber i =
match i with
| Even -> printfn "%d is even" i
| Odd -> printfn "%d is odd" i
testNumber 5
testNumber -2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment