Skip to content

Instantly share code, notes, and snippets.

@Shibe
Created June 19, 2017 17:34
Show Gist options
  • Save Shibe/039a8d45addf0224968a203bd31e0a07 to your computer and use it in GitHub Desktop.
Save Shibe/039a8d45addf0224968a203bd31e0a07 to your computer and use it in GitHub Desktop.
//Write a function that, given two numbers, prints "Fizz"
//if the first number is divisible by 4,
//"Buzz" if the second is divisible by 4,
//"FizzBuzz" if both are divisible by 4.
let divisible (n : int) = if (n % 4 = 0) then true else false
let questionOne (first:int) (second:int) =
if divisible first && divisible second then
"FizzBuzz"
else if divisible second then
"Buzz"
else if divisible first then
"Fizz"
else
""
//Write a function "zip" that takes two lists of equal length
//and returns the list of pairs containing
//the elements in the same position. For example:
let rec zip list1 list2 =
match list1, list2 with
| [], [] -> []
| h1::t1, h2::t2 -> (h1, h2) :: zip t1 t2
// Write the function "unzip" that does the opposite:
let rec unzip list =
match list with
| [] -> [],[]
| (h1,h2)::t1 ->
let l1, l2 = unzip t1
(h1 :: l1),(h2 :: l2)
//An arithmetic expression can be a number or sum, product, difference,
//or division of two arithmetic expressions. Define an arithmetic
//expression as a discriminate union and write a function that evaluates it.
type expression =
| Number of int
| Sum of expression * expression
| Product of expression * expression
| Difference of expression * expression
let rec eval (shit:expression) =
match shit with
| Number(x) -> x
| Sum(a,b) -> eval a + eval b
| Product(a,b) -> eval a * eval b
| Difference(a,b) -> eval a - eval b
// Maak zelf maar af, geen zin om te typen
//Write a function that, given three numbers, returns
//- "first" if the first number is the greatest of all three;
//- "second" if the second number is the greatest of all three;
//- "third" if the third number is the greatest of all three.
let exe1 (first:int) (second:int) (third:int) :string =
if first > second && first > third then
"first"
else if second > first && second > third then
"second"
else
"third"
//Write a function "tau0" that takes an instance of Option<'a>
//and converts it into an instance of List<'a>.
let tau0 (x:Option<'a>) =
match x with
| None -> []
| Some(a) -> [a]
//3 and 4. Write a recursive function "tau1" with type (Option<'a> -> Option<'b>) -> List<'a> -> List<'b>.
//The function should run the input function on every element of the list and puts the result in the output list.
let rec tau1 func l =
match l with
| [] -> []
| h::t ->
match (func (Some(h))) with
| None -> tau1 func t
| Some(r) -> r::tau1 func t
// Ignore
let tau3 (x:Option<'a>) =
match x with
| None -> None
| Some(a) -> Some(a+a)
//A person is a record containing a name, a surname, and a date of birth. Define
//the date and person records and an "age" function that takes as input a person and
//returns the same person, older by one year, one month, and one day.
type Date = {
Day:int;
Month:int;
Year:int;
}
type Person = {
Name:string;
Surname:string;
DOB:Date;
}
let getOlder person:Person = {person with DOB ={Day = person.DOB.Day + 1; Month = person.DOB.Month + 1; Year = person.DOB.Year + 1}}
let barld = {Name="Barld";Surname="Boot";DOB={Day=8;Month=8;Year=1994}}
[<EntryPoint>]
let main argv =
printfn "%A" ()
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment