Skip to content

Instantly share code, notes, and snippets.

@nelsonlaquet nelsonlaquet/asdf.fs
Created Jun 28, 2012

Embed
What would you like to do?
open System
type Location =
| City of string * float
| State of string * Location list
| Country of string * Location list
let data = [Country("United States", [State("Wasington", [City("Anacortes", 32.0); City("Seattle", 523.0)])])]
let getTotalPopulation locations =
let rec getPopulation item =
let doListOfItems items = items |> List.map (getPopulation) |> List.fold (+) 0.0
match item with
| City(_, pop) -> pop
| State(_, list) -> doListOfItems list
| Country(_, list) -> doListOfItems list
locations |> List.map (getPopulation) |> List.fold (+) 0.0
Console.WriteLine(getTotalPopulation data)
Console.ReadKey() |> ignore
type Shape =
| Rectangle of float * float
| Square of float
| Circle of float
let getArea = function
| Square(width) -> width * width
| Circle(rad) -> Math.PI * rad * rad
| Rectangle(width, height) -> width * height
let shapes = [Rectangle(3.0, 4.0); Square(3.0); Circle(2.0); Rectangle(412.34, 322.2)]
let totalArea = shapes |> List.map getArea |> List.fold (+) 0.0
let cities = [("Seattle", 23); ("Anacortes", 2); ("Whoa", 3232)]
let output =
cities
|> List.map (fun (name, pop) -> sprintf "%s has a population of %i" name pop)
|> List.fold (fun a s -> a + s + "\n") ""
Console.WriteLine(output)
let totalPopulation = cities |> List.fold (fun a (_, pop) -> a + pop) 0
printfn "Total population: %i" totalPopulation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.