Skip to content

Instantly share code, notes, and snippets.

@jacqueline-homan
Created February 8, 2016 21:51
Show Gist options
  • Save jacqueline-homan/6e8cd43070d6a2a7f4ac to your computer and use it in GitHub Desktop.
Save jacqueline-homan/6e8cd43070d6a2a7f4ac to your computer and use it in GitHub Desktop.
open System
open System.IO
let printNumbers() =
printfn "Welcome to the coding challenge where your program should successfully"
printfn "accept the input of two integers and output their sum and product"
printfn ""
printfn "Enter an integer: "
let a = Console.ReadLine() |> int
printfn "Enter another integer: "
let b = Console.ReadLine() |> int
printfn "%d" (a+b)
printfn "%d" (a*b)
printNumbers()
printfn "Demonstrating Fizz-Buzz with pattern-matching using a when guard"
let nums = [1..100]
nums
|> Seq.map (function
| x when x%5=0 && x%3=0 -> "FizzBuzz"
| x when x%3=0 -> "Fizz"
| x when x%5=0 -> "Buzz"
| x -> string x)
|> Seq.iter (printfn "%s")
printfn "Demonstrating Fizz-Buzz with more effective use of pattern-matching"
let nums2 = [1..100]
nums2
|> Seq.map (fun x ->
match x%3, x%5 with
| 0,0 -> "FizzBuzz"
| 0,_ -> "Fizz"
| _,0 -> "Buzz"
| _ -> string x)
|> Seq.iter (printfn "%s")
let fizzBuzz modFizz modBuzz =
[1..100]
|> Seq.map (fun x ->
match x%3, x%5 with
| 0,0 -> "FizzBuzz"
| 0,_ -> "Fizz"
| _,0 -> "Buzz"
| _ -> string x)
|> Seq.iter (printfn "%s")
let fizzbuzz modFizz modBuzz =
[1..100]
|> Seq.map (fun x->
match x%modFizz, x%modBuzz with
| 0,0 -> "FizzBuzz"
| 0,_ -> "Fizz"
| _,0 -> "Buzz"
| _ -> string x)
fizzbuzz 3 5
|> Seq.iter (printfn "%s")
printfn "Demonstrating Fizz-Buzz by passing funtions as parameters to a function"
let FizzBuzz modFizz modBuzz =
[1..100]
|> Seq.map (fun x->
match modFizz(x:int), modBuzz(x:int) with
| 0,0 -> "FizzBuzz"
| 0,_ -> "Fizz"
| _,0 -> "Buzz"
| _ -> string x)
FizzBuzz (fun x -> x%3) (fun x -> x%5)
|> Seq.iter (printfn "%s")
printfn"Printing the 5th Fibonnaci number"
let rec fib x =
if x < 2 then 1
else fib (x - 1) + fib (x - 2)
printfn "The 5th Fibonnaci nimber is: %d" (fib 5)
printfn "The Riemann zeta function is a function of a complex variable s"
printfn "that analytically continues the sum of the infinite series"
printfn"which converges when the real part of s is greater than 1. "
let zeta k e =
let inner(i) = 1.0/(double(i)**k)
let rec seqInfinite i xp =
let x = xp + inner(i)
match abs(xp - x) with
| dx when dx <= e -> x
| _ -> seqInfinite(i + 1) x
seqInfinite 1 0.0
printfn "%f" (zeta 15.0 0.0) //OK, I went nuts...had to have my math fix
let factorial n =
let rec loop i acc =
match i with
| 0 | 1 -> acc
| _ -> loop (i - 1) (acc * i)
loop n 1 //Tail recursive factorial
printfn "%d" (factorial 5)
printfn "Demonstrating function composition"
let compose (f:'b -> 'c) (g:'a -> 'b) : 'a -> 'c =
fun a -> a |> g |> f
[<EntryPoint>]
let main argv =
//fizzBuzz 3 5
0 // return an integer exit code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment