Skip to content

Instantly share code, notes, and snippets.

@follesoe
Created February 28, 2013 20:08
Show Gist options
  • Save follesoe/5059689 to your computer and use it in GitHub Desktop.
Save follesoe/5059689 to your computer and use it in GitHub Desktop.
Extreme Startup Answers in F#
module Answers
open System
open System.Text
let (|Regex|_|) regex str =
let m = RegularExpressions.Regex(regex).Match(str)
if m.Success
then Some (List.tail [ for x in m.Groups -> x.Value ])
else None
let isPrime n =
if n = 1 then false
else
let max = n |> float |> sqrt |> int
let rec test = function
| x when x > max -> true
| x -> if (n % x) = 0 then false else test (x+1)
test 2
let isSquare n =
let f = Math.Sqrt n
if (Convert.ToDouble(Convert.ToInt32(f))) = f then true else false
let isCube n =
let f = Math.Pow(n, 1.0/3.0)
if (Convert.ToDouble(Convert.ToInt32(f))) = f then true else false
let fib n =
let rec loop acc1 acc2 = function
| n when n = 0 -> acc1
| n -> loop acc2 (acc1 + acc2) (n - 1)
loop 0 1 n
let toInts (s: String) =
s.Split([","]
|> Seq.toArray, StringSplitOptions.RemoveEmptyEntries)
|> Seq.map (fun n -> Convert.ToInt32(n))
let toFloats (s: String) =
s.Split([","]
|> Seq.toArray, StringSplitOptions.RemoveEmptyEntries)
|> Seq.map (fun n -> Convert.ToDouble(n))
let toS o = o.ToString();
let toI (s: String) = Convert.ToInt32(s)
let toD (s: String) = Convert.ToDouble(s)
let max (a: int) (b: int) = Math.Max(a, b)
let answerQuestion question =
match question with
| "what colour is a banana" -> "Yellow"
| "which city is the Eiffel tower in" -> "Paris"
| "who played James Bond in the film Dr No" -> "Sean Connery"
| "what currency did Spain use before the Euro" -> "Peseta"
| "who is the Prime Minister of Great Britain" -> "David Cameron"
| "what is the twitter id of the organizer of this dojo" -> "olemartin"
| Regex "my name is (.*). what is my name" [name] -> name
| Regex "what is (\d*) plus (\d*)" [a; b] -> (toI a + toI b).ToString()
| Regex "what is (\d*) plus (\d*) plus (\d*)" lst -> lst |> Seq.map toI |> Seq.reduce(+) |> toS
| Regex "what is (\d*) minus (\d*)" [a; b] -> (toI a - toI b).ToString()
| Regex "what is (\d*) multiplied by (\d*)" [a; b] -> (toI a * toI b).ToString()
| Regex "what is (\d*) to the power of (\d*)" [a; b;] -> Math.Pow(toD a, toD b).ToString()
| Regex "which of the following numbers is the largest: (.*)" [nums] ->
let numbers = toInts nums
Seq.fold max 0 numbers |> toS
| Regex "which of the following numbers is both a square and a cube: (.*)" [nums] ->
let nums = toFloats nums |> Seq.filter isSquare |> Seq.filter isCube |> Seq.toList
String.Join(", ", nums)
| Regex "which of the following numbers are primes: (.*)" [nums] ->
let nums = toInts nums |> Seq.filter isPrime |> Seq.toList
String.Join(", ", nums)
| Regex "what is the (.*)th number in the Fibonacci sequence" [num] ->
let n = Convert.ToInt32(num)
let f = fib n
f.ToString()
| _ -> "FsharpFTW"
let answer question =
match question with
| Regex "(.*?): (.*)" [id; question] -> answerQuestion question
| _ -> answerQuestion question
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment