let projectEulerProblemOne = [1 .. 999] |> List.filter(fun x -> x % 3 = 0 || x % 5 = 0) |> List.sum
module Tests = begin
open Fuchu
let suite =
testList "Solution" [
testCase "Example Test" <|
fun _ -> Assert.Equal("Project Euler - problem 1", 233168, projectEulerProblemOne)
]
end
type Person = { Name : string; Gender : string }
let alice = { Name = "Alice"; Gender = "Female" }
let bob = { Name = "Bob"; Gender = "Male" }
let femaleOrMale p =
match p with
| { Gender = "Female" } -> p.Name + " is female."
| { Gender = "Male" } -> p.Name + " is male."
| _ -> "???"
module Tests = begin
open Fuchu
let suite =
testList "Solution" [
testCase "Test 1" <| fun _ -> Assert.Equal("Alice", "Alice is female.", femaleOrMale alice);
testCase "Test 2" <| fun _ -> Assert.Equal("Bob", "Bob is male.", femaleOrMale bob)
]
end
type SafeStringNumbers() =
member this.Bind(x, f) =
match System.Int32.TryParse(x) with
| (true, v) -> f v
| _ -> f 0
member this.Return(x) =
x
let safeStringNumbers = new SafeStringNumbers()
let v1 = safeStringNumbers {
let! a = "1"
let! b = "2"
let! c = "3"
return a + b + c
}
let v2 = safeStringNumbers {
let! a = "1"
let! b = "foo"
let! c = "3"
return a + b + c
}
module Tests = begin
open Fuchu
let suite =
testList "Solution" [
testCase "Test 1" <| fun _ -> Assert.Equal("1+2+3", 6, v1);
testCase "Test 2" <| fun _ -> Assert.Equal("1+foo+3", 4, v2)
]
end
let sq = seq {
yield 1
yield! seq { 2 .. 6 }
yield 7
yield! seq [8; 9; 10]
}
let count = Seq.length sq
let list = List.ofSeq sq
module Tests = begin
open Fuchu
let suite =
testList "Solution" [
testCase "Test 1" <|
fun _ -> Assert.Equal("Count of sq", 10, count)
testCase "Test 1" <|
fun _ -> Assert.Equal("List of sq", [1; 2; 3; 4; 5; 6; 7; 8; 9; 10], list)
]
end
let evens = [2 .. 2 .. 10]
let odds = [for i in evens -> i - 1]
let fizzBuzzNumbers = [for i in 1 .. 100 do if i % 3 = 0 && i % 5 = 0 then yield i]
printfn "%A" evens
printfn "%A" odds
printfn "%A" fizzBuzzNumbers
let getSign x = if x > 0 then "+" elif x < 0 then "-" else "0"
let foo = [-3..3] |> List.map getSign |> String.concat(" ")
printfn "%s" foo
let areEqualsIgnoreCase (s : string * string) =
match s with
| (s1, s2) when s1.ToUpper() = s2.ToUpper() -> true
| _ -> false
printfn "%b" (areEqualsIgnoreCase ("Hello", "hello"))
printfn "%b" (areEqualsIgnoreCase ("hello", "HELLO"))
printfn "%b" (areEqualsIgnoreCase ("hello", "ello"))
let detectType v =
match box v with
| :? int -> printfn "int"
| :? string -> printfn "string"
| _ -> printfn "?"
detectType 1
detectType "1"
detectType 1.0
let tryParseInt s =
try
let i = System.Int32.Parse s
Some i
with _ -> None
let v1 = tryParseInt "1"
printfn "%O" v1
printfn "%O" v1.Value
printfn "%O" v1.IsSome
printfn "%O" v1.IsNone
printfn ""
let v2 = tryParseInt "?"
printfn "%O" v2
printfn "%O" v2.IsSome
printfn "%O" v2.IsNone
[for i in 1 .. 30 -> if i % 3 = 0 && i % 5 = 0 then "FizzBuzz" elif i % 3 = 0 then "Fizz" elif i % 5 = 0 then "Buzz" else i.ToString()] |> List.map(fun v -> printfn "%s" v)
let add n x = x + n
let times n x = x * n
let f1 = add 1 >> times 2
let f2 = add 1 << times 2
printfn "%i" (f1 2) // (2+1)*2
printfn "%i" (f2 2) // 2*2+1
let add x y = x + y
let add1 = add 1
let add2 = add 2
let add3 = add 3
let add1and2and3 = add1 >> add2 >> add3
printfn "%i" (0 |> add1and2and3)
let add x y = x + y
let add1 = add 1
let add2 = add 2
let add3 = add 3
printfn "%i" (add1 1)
printfn "%i" (add2 1)
printfn "%i" (add3 1)
printfn "%*s" 5 "*"
printfn "%*s" 6 "***"
printfn "%*s" 7 "*****"
printfn "%*s" 8 "*******"
printfn "%*s" 9 "*********"
let (|Int|_|) str = match System.Int32.TryParse(str) with | (true, int) -> Some(int) | _ -> None
let (|Bool|_|) str = match System.Boolean.TryParse(str) with | (true, bool) -> Some(bool) | _ -> None
let parse s =
match s with
| Bool b -> printfn "%-10s%b" "Bool" b
| Int i -> printfn "%-10s%i" "Int" i
| _ -> printfn "%-10s%O" "???" s
parse "0"
parse "1"
parse "2"
parse "3"
parse "123"
parse "-123"
parse "True"
parse "true"
parse "False"
parse "False"
parse "hello"