Mercurial > repos > icfp2014
view code/mike00.lm @ 48:8b6f6e2cbf38
merge
author | William Morgan <billjunk@mrgn.org> |
---|---|
date | Sun, 27 Jul 2014 02:35:24 -0700 |
parents | abd8ceef0dd8 |
children |
line wrap: on
line source
#{ length <- :lst { len <- [] while: { not: (lst isInteger?)} do: { lst <- lst tail len <- len + 1 } len } reverse <- :lst { new <- [] while: { not: (lst isInteger?)} do: { new <- (lst value) | new lst <- lst tail } new } split:at <- :lst :pos { first <- [] i <- 0 while: { i < pos } do: { first <- (lst value) | first lst <- lst tail i <- i + 1 } #[(reverse: first) lst] } map <- :lst fun { new <- [] while: { not: (lst isInteger?) } do: { new <- (fun: (lst value)) | new lst <- lst tail } reverse: new } fold:with <- :lst acc :fun { while: { not: (lst isInteger?) } do: { acc <- fun: acc (lst value) lst <- lst tail } acc } filter <- :lst pred { new <- [] while: { not: (lst isInteger?) } do: { if: (pred: (lst value)) { new <- (lst value) | new } else: {} lst <- lst tail } reverse: new } flatten <- :lst { fold: lst [] with: :acc el { fold: el acc with: :iacc iel { iel | iacc } } } makeTree:size <- :lst :size { ret <- 0 sub <- 0 half <- size / 2 if: size = 2 { ret <- #[(lst value) ((lst tail) value)] } else: { if: size = 1 { ret <- lst } else: { sub <- split: lst at: half ret <- #[ (makeTree: (sub value) size: half) (makeTree: (sub tail) size: size-half) ] } } ret } makeTree <- :lst { size <- lst length #[size (makeTree: lst size: size)] } get:fromTree:size <- :idx :tree :size { print: #[1 tree size] ret <- 0 half <- size / 2 if: size <= 2 { if: idx = 0 { ret <- tree value } else: { ret <- tree tail } } else: { if: idx < half { ret <- get: idx fromTree: (tree value) size: half } else: { ret <- get: idx-half fromTree: (tree tail) size: size-half } } ret } get:fromTree <- :idx :tree { size <- tree value print: #[0 size] get: idx fromTree: (tree tail) size: size } treeMap:size <- :tree fun :size { ret <- 0 half <- size / 2 if: size = 2 { ret <- #[(fun: (tree value)) (fun: (tree tail))] } else: { if: size = 1 { ret <- #[(fun: (tree value)) 0] } else: { ret <- #[ (treeMap: (tree value) fun size: half) (treeMap: (tree tail) fun size: size-half) ] } } ret } treeMap <- :tree fun { #[(tree value) (treeMap: (tree tail) fun size: (tree value))] } tree:size:update:with <- :tree :size :idx :fun { ret <- 0 half <- size / 2 if: size = 2 { if: idx = 0 { ret <- #[(fun: (tree value)) (tree tail)] } else: { ret <- #[(tree value) (fun: (tree tail))] } } else: { if: size = 1 { ret <- #[(fun: (tree value)) 0] } else: { if: (idx < half) { ret <- #[ (tree: (tree value) size: half update: idx with: fun) (tree tail) ] } else: { ret <- #[ (tree value) (tree: (tree tail) size: size-half update: idx-half with: fun) ] } } } ret } tree:update:with <- :tree :idx :fun { #[(tree value) (tree: (tree tail) size: (tree value) update: idx with: fun)] } tree:set:to <- :tree :idx :val { tree: tree update: idx with: :el { val } } height <- 0 width <- 0 main <- { /* map <- initWorld value height <- map length width <- (map value) length */ lst <- [1 2 3 4 5 6 7 8 9] print: (length: lst) print: (map: lst :el { el + 1 }) print: (split: lst at: (length: lst) / 2) tree <- makeTree: lst print: tree print: (get: 0 fromTree: tree) print: (get: 6 fromTree: tree) print: (get: 8 fromTree: tree) print: (treeMap: tree :el { el + 1 }) print: (tree: tree set: 0 to: 42) print: (tree: tree set: 6 to: 42) print: (tree: tree set: 8 to: 42) print: (fold: lst 0 with: :acc el { acc + el }) print: (filter: lst :el { el > 4 }) print: (tree: tree update: 8 with: :el { el + 29 }) print: (flatten: [lst lst lst]) } }