Last active
August 29, 2015 14:26
-
-
Save chendrix/b4598a067d7b2a35414a to your computer and use it in GitHub Desktop.
Random Matrix in Elm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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