Skip to content

Instantly share code, notes, and snippets.

@bjartwolf
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bjartwolf/83ab3b20d3a2e644e07c to your computer and use it in GitHub Desktop.
Save bjartwolf/83ab3b20d3a2e644e07c to your computer and use it in GitHub Desktop.
open System
// We can not work with radius of the diamond outside 0 to 25
let CreateRadius (i:int) =
match i with
| i when i = 0 && i < 26 -> Some i
| _ -> None
let radius = CreateRadius 20 |> Option.get
let axis = [|-radius .. radius|]
type Point = { x : int; y: int;}
type PointWithLetter = { p: Point; c: Char}
let TaxicabNorm p =
abs p.x + abs p.y
let indexedLetters =
['A' .. 'Z'] |> Seq.mapi (fun i l -> i,l)
let findLetter (p: Point) =
let _,l = Seq.find(fun (i, _) -> i = abs p.y) indexedLetters
l
let createAllPoints vector =
seq { for x in vector do for y in vector do yield {x=x;y=y} }
let allPoints = createAllPoints axis
let diamond = allPoints
|> Seq.filter (fun p -> TaxicabNorm p = radius)
|> Seq.map( fun p -> { p=p; c=findLetter p })
// Everything below is just for drawing the diamond
// Returns a 2d point array to draw in
let axisTo2D axis =
[| for _ in axis -> (Array.map (fun _ -> '.') axis) |]
let drawing = axisTo2D axis
let findIndexInAxis elem axis =
Array.findIndex (fun x -> x = elem) axis
for pointWithChar in diamond do
let yindex = findIndexInAxis pointWithChar.p.y axis
let xindex = findIndexInAxis pointWithChar.p.x axis
drawing.[xindex].[yindex] <- pointWithChar.c
let printArray (arr: char array) = new string (arr) |> sprintf "%A" |> Console.WriteLine
for line in drawing do
printArray line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment