Skip to content

Instantly share code, notes, and snippets.

@barthr
Created April 20, 2017 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barthr/28e9f4d9e08809e6e88a2a59ba9231c5 to your computer and use it in GitHub Desktop.
Save barthr/28e9f4d9e08809e6e88a2a59ba9231c5 to your computer and use it in GitHub Desktop.
// Exercise 1
let isLeap (year:int) :bool =
year % 4 = 0 && year % 100 <> 0 || year % 400 = 0
// Exercise 3
let rec map mapFn list =
match list with
| [] -> []
| x::xs -> [mapFn x] @ map mapFn xs
// Exercise 4
type Expression =
| Constant of int
| Sum of Expression * Expression
| Product of Expression * Expression
| Divide of Expression * Expression
| Minus of Expression * Expression
let rec evaluate (expr: Expression) :int =
match expr with
| Constant(x) -> x
| Sum(x,y) -> (evaluate x + evaluate y)
| Product(x,y) -> (evaluate x * evaluate y)
| Minus(x, y) -> (evaluate x - evaluate y)
| Divide(x, y) -> (evaluate x / evaluate y)
// Exercise 5
type Input =
{
Name: string
Code: int
State: bool
}
type InputController =
{
Keyboard: Input list
Mouse: Input list
}
let getKeysPressed(input: InputController): Input list =
let pressedKeys (event: Input list) =
event
|> List.filter (fun (x: Input) -> x.State)
pressedKeys input.Keyboard @ pressedKeys input.Mouse
[<EntryPoint>]
let main argv =
// Exercise 1
printfn "%A" (isLeap 1700)
printfn "%A" (isLeap 1800)
printfn "%A" (isLeap 1900)
printfn "%A" (isLeap 1600)
printfn "%A" (isLeap 2000)
// Exercise 3
printfn "%A" (map (fun x -> x * 2) [1;2;3])
// Exercise 4
printfn "%A" (evaluate (Divide(Sum(Constant(3),Constant(1)),Constant(2))))
// Exercise 5
let smallController = { Keyboard = [{ Name = "Up"; Code = 0; State = false };{ Name = "Down"; Code = 1; State = true };{ Name = "Left"; Code = 2; State = false };{ Name = "Up"; Code = 0; State = true }]; Mouse = [{ Name = "LeftClick"; Code = 10; State = false };{ Name = "RightClick"; Code = 10; State = true }]}
printfn "%A" (getKeysPressed smallController)
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment