Skip to content

Instantly share code, notes, and snippets.

@chendrix
Last active August 29, 2015 14:26
Show Gist options
  • Save chendrix/b4598a067d7b2a35414a to your computer and use it in GitHub Desktop.
Save chendrix/b4598a067d7b2a35414a to your computer and use it in GitHub Desktop.
Random Matrix in Elm
import Random exposing (list, int)
import Array exposing (fromList)
import Maybe
type alias Matrix a = Array.Array (Array.Array a)
type alias Location = (Int, Int)
row : Location -> Int
row = fst
col : Location -> Int
col = snd
matrix : Int -> Int -> (Location -> a) -> Matrix a
matrix numRows numCols f =
Array.initialize numRows (
\row -> Array.initialize numCols (
\col -> f (loc row col)))
startingSeed = Random.initialSeed 42
m : Int -> Matrix Int
m width =
let
pullFromArray =
list (width * width) (int 1 10)
|> (flip Random.generate) startingSeed
|> fst
|> Array.fromList
pullFrom i =
get i pullFromArray
|> Maybe.withDefault -1
valueAt location =
let
r = row location
c = col location
in
if | r == c -> 0
| otherwise ->
pullFrom (r * c)
in
matrix width width valueAt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment