Skip to content

Instantly share code, notes, and snippets.

@mastoj
Created February 9, 2015 20:23
Show Gist options
  • Save mastoj/8b5771743161ea1aef07 to your computer and use it in GitHub Desktop.
Save mastoj/8b5771743161ea1aef07 to your computer and use it in GitHub Desktop.
F# Diamond
open System
type CharSpec = {Char:char; SndOffset: int}
type DiamondSpec = CharSpec list
let printDiamondRow charSpec =
match charSpec with
| {Char = x; SndOffset = 0} -> sprintf "%c" x
| {Char = x; SndOffset = y} -> sprintf "%c%s%c" x (String(' ', (y-1))) x
let padRow size (row:string) =
let padding = String(' ', (size-(row.Length))/2)
sprintf "%s%s%s" padding row padding
let join separator (strings:string list) = String.Join(separator, strings)
let makeDiamondSpec x =
let letters = ['A' .. x]
let makeOffset rowNumber = 2*(rowNumber)
let rec splitList list first second index =
match list with
| [] -> ((List.rev first) @ second),(index*2-1)
| y::[] ->
let charSpec = {Char = y; SndOffset = (makeOffset index)}
splitList [] (charSpec::first) second (index+1)
| y::rest ->
let charSpec = {Char = y; SndOffset = (makeOffset index)}
splitList rest (charSpec::first) (charSpec::second) (index+1)
splitList letters [] [] 0
let makeDiamond c =
let diamondSpec,size = makeDiamondSpec 'F'
diamondSpec |> List.map (printDiamondRow >> (padRow size)) |> join (Environment.NewLine)
[<EntryPoint>]
let main argv =
let result = makeDiamond 'F'
printfn "%s" result
Console.ReadLine() |> ignore
0 // return an integer exit code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment