|
| 1 | + |
| 2 | + |
| 3 | +-- |
| 4 | +-- do-end notation |
| 5 | +-- |
| 6 | + |
| 7 | +function assoc[T] v::PersistentVector[T] i::Int el -> PersistentVector: |
| 8 | + boundscheck! v i |
| 9 | + if (i > |v| - |v.tail|): |
| 10 | + newtail = v.tail # (1..end) |
| 11 | + newtail # (mask i) := el |
| 12 | + PersistentVector v.trie newtail v.length |
| 13 | + else: |
| 14 | + newnode = v.trie[i][1..end] |
| 15 | + newnode # (mask i) := el |
| 16 | + PersistentVector (assoc v.trie i newnode) v.tail v.length |
| 17 | + end |
| 18 | +end |
| 19 | + |
| 20 | +function max list: |
| 21 | + reduce (a b -> a > b ? a : b) list |
| 22 | +end |
| 23 | + |
| 24 | + |
| 25 | +type Tree[a] = |
| 26 | + | Empty |
| 27 | + | Node (a, Tree[a], Tree[a]) |
| 28 | + |
| 29 | + |
| 30 | +function map :: (a -> b) -> Tree[a] -> Tree[b] |
| 31 | +function map f = |
| 32 | + | Empty => Empty |
| 33 | + | Node (x, l, r) => Node (f x, map f l, map f r) |
| 34 | +end |
| 35 | + |
| 36 | + |
| 37 | +-- |
| 38 | +-- { } notation |
| 39 | +-- |
| 40 | + |
| 41 | +function assoc[T] v::PersistentVector[T] i::Int el = { |
| 42 | + boundscheck! v i |
| 43 | + if (i > |v| - |v.tail|) { |
| 44 | + newtail = v.tail # (1..end) |
| 45 | + newtail # (mask i) := el |
| 46 | + PersistentVector v.trie newtail v.length |
| 47 | + } |
| 48 | + else { |
| 49 | + newnode = v.trie[i][1:end] |
| 50 | + newnode # (mask i) := el |
| 51 | + PersistentVector (assoc v.trie i newnode) v.tail v.length |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +function max list = { |
| 56 | + reduce (a b -> a > b ? a : b) list |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +type Tree[a] = { |
| 61 | + | Empty |
| 62 | + | node (a, Tree[a], Tree[a]) |
| 63 | +} |
| 64 | + |
| 65 | +function map :: (a -> b) -> Tree[a] -> Tree[b] |
| 66 | +function map f = { |
| 67 | + | Empty => Empty |
| 68 | + | Node (x, l, r) => Node (f x, map f l, map f r) |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +-- |
| 74 | +-- indentation notation (= for block definitions) |
| 75 | +-- (Maybe*) |
| 76 | +-- |
| 77 | + |
| 78 | +function assoc[T] v::PersistentVector[T] i::Int el = |
| 79 | + boundscheck! v i |
| 80 | + if (i > |v| - |v.tail|): |
| 81 | + newtail = v.tail # (1..end) |
| 82 | + newtail # (mask i) := el |
| 83 | + PersistentVector v.trie newtail v.length |
| 84 | + else: |
| 85 | + newnode = v.trie[i][1:end] |
| 86 | + newnode # (mask i) := el |
| 87 | + PersistentVector (assoc v.trie i newnode) v.tail v.length |
| 88 | + |
| 89 | +function max list = |
| 90 | + reduce (a b -> a > b ? a : b) list |
| 91 | + |
| 92 | +type Tree[a] = Empty |
| 93 | + | node (a, Tree[a], Tree[a]) |
| 94 | + |
| 95 | +function map :: (a -> b) -> Tree[a] -> Tree[b] |
| 96 | +function map f = |
| 97 | + | Empty => Empty |
| 98 | + | Node (x, l, r) => Node (f x, map f l, map f r) |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +-- |
| 103 | +-- haskell-like notation |
| 104 | +-- (Maybe) |
| 105 | +-- |
| 106 | + |
| 107 | +assoc [T](v::PersistentVector[T] i::Int el) = |
| 108 | + boundscheck! v i |
| 109 | + if (i > |v| - |v.tail|): |
| 110 | + newtail = v.tail # (1..end) |
| 111 | + newtail # (mask i) := el |
| 112 | + PersistentVector v.trie newtail v.length |
| 113 | + else: |
| 114 | + newnode = v.trie[i][1:end] |
| 115 | + newnode # (mask i) := el |
| 116 | + PersistentVector (assoc v.trie i newnode) v.tail v.length |
| 117 | + |
| 118 | +max list = |
| 119 | + reduce (a b -> a > b ? a : b) list |
| 120 | + |
| 121 | +type Tree[a] = Empty |
| 122 | + | node (a, Tree[a], Tree[a]) |
| 123 | + |
| 124 | +map :: (a -> b) -> Tree[a] -> Tree[b] |
| 125 | +map f = | Empty => Empty |
| 126 | + | Node (x, l, r) => Node (f x, map f l, map f r) |
| 127 | + |
| 128 | + |
| 129 | +-- |
| 130 | +-- indentation notation (expression definitions) |
| 131 | +-- (No.) |
| 132 | +-- |
| 133 | + |
| 134 | +assoc = [T](v::PersistentVector[T] i::Int el) -> |
| 135 | + boundscheck! v i |
| 136 | + if (i > |v| - |v.tail|): |
| 137 | + newtail = v.tail # (1..end) |
| 138 | + newtail # (mask i) := el |
| 139 | + PersistentVector v.trie newtail v.length |
| 140 | + else: |
| 141 | + newnode = v.trie[i][1:end] |
| 142 | + newnode # (mask i) := el |
| 143 | + PersistentVector (assoc v.trie i newnode) v.tail v.length |
| 144 | + |
| 145 | +max = list -> |
| 146 | + reduce (a b -> a > b ? a : b) list |
| 147 | + |
| 148 | +Tree = type[a]: Empty |
| 149 | + | node (a, Tree[a], Tree[a]) |
| 150 | + |
| 151 | +map :: (a -> b) -> Tree[a] -> Tree[b] |
| 152 | +map = f -> | Empty => Empty |
| 153 | + | Node (x, l, r) => Node (f x, map f l, map f r) |
| 154 | + |
| 155 | + |
| 156 | +-- |
| 157 | +-- do notation |
| 158 | +-- |
| 159 | + |
| 160 | +function assoc[T] v::PersistentVector[T] i::Int el -> PersistentVector do |
| 161 | + boundscheck! v i |
| 162 | + if (i > |v| - |v.tail|) do |
| 163 | + newtail = v.tail # (1..end) |
| 164 | + newtail # (mask i) := el |
| 165 | + PersistentVector v.trie newtail v.length |
| 166 | + else |
| 167 | + newnode = v.trie[i][1:end] |
| 168 | + newnode # (mask i) := el |
| 169 | + PersistentVector (assoc v.trie i newnode) v.tail v.length |
| 170 | + end |
| 171 | +end |
| 172 | + |
| 173 | +function max list do |
| 174 | + reduce (a b -> a > b ? a : b) list |
| 175 | +end |
| 176 | + |
| 177 | +type Tree[a] = |
| 178 | + | Empty |
| 179 | + | Node (a, Tree[a], Tree[a]) |
| 180 | + |
| 181 | +function map :: (a -> b) -> Tree[a] -> Tree[b] |
| 182 | +function map f do |
| 183 | + | Empty => Empty |
| 184 | + | Node (x, l, r) => Node (f x, map f l, map f r) |
| 185 | +end |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | +-- |
| 190 | +-- pyret notation |
| 191 | +-- |
| 192 | + |
| 193 | +function assoc (v::PersistentVector a) (i::Int) (el::a) -> PersistentVector: |
| 194 | + boundscheck! v i |
| 195 | + if (i > |v| - |v.tail|): |
| 196 | + newtail = v.tail # (1..end) |
| 197 | + newtail # (mask i) := el |
| 198 | + PersistentVector v.trie newtail v.length |
| 199 | + else: |
| 200 | + newnode = v.trie[i][1:end] |
| 201 | + newnode # (mask i) := el |
| 202 | + PersistentVector (assoc v.trie i newnode) v.tail v.length |
| 203 | + end |
| 204 | +end |
| 205 | + |
| 206 | +height = Leaf => 0 |
| 207 | + | Node (l, _, r) => 1 + max (height l) (height r) |
| 208 | + |
| 209 | +type Tree a = Leaf |
| 210 | + | Node (Tree a, a, Tree a) |
| 211 | + |
| 212 | +map :: (a -> b) -> Tree a -> Tree b |
| 213 | +map f = Leaf => Leaf |
| 214 | + | Node (l, x, r) => Node (map f l, f x, map f r) |
| 215 | + |
| 216 | +protocol Show a = |
| 217 | + show :: T -> String |
| 218 | +end |
| 219 | + |
| 220 | +instance Show (Tree a) = |
| 221 | + show = Leaf => "()" |
| 222 | + | Node (x, l, r) => "[{x}]: ({show l} | {show r})" |
| 223 | +end |
| 224 | + |
| 225 | + |
| 226 | +-- // -- |
| 227 | + |
| 228 | + |
| 229 | +(function factorial [n] |
| 230 | + (log/info "Will calculate factorial of {n}") |
| 231 | + (if (= n 0) |
| 232 | + 1 |
| 233 | + (* n (factorial (- n 1))))) |
| 234 | + |
| 235 | +function factorial n do |
| 236 | + Log.info "Will calculate factorial of {n}" |
| 237 | + (n == 0) ? 1 : n * factorial (n - 1) |
| 238 | +end |
| 239 | + |
| 240 | +let factorial n = |
| 241 | + Log.info "Will calculate factorial of {n}" |
| 242 | + (n == 0) ? 1 : n * factorial (n - 1) |
| 243 | +end |
| 244 | + |
| 245 | +function factorial n: |
| 246 | + Log.info "Will calculate factorial of {n}" |
| 247 | + (n == 0) ? 1 : n * factorial (n - 1) |
| 248 | +end |
| 249 | + |
| 250 | +factorial n = do |
| 251 | + Log.info "Will calculate factorial of {n}" |
| 252 | + (n == 0) ? 1 : n * factorial (n - 1) |
| 253 | +end |
| 254 | + |
| 255 | +function factorial n { |
| 256 | + Log.info "Will calculate factorial of {n}" |
| 257 | + (n == 0) ? 1 : n * factorial (n - 1) |
| 258 | +} |
| 259 | + |
| 260 | +factorial n = { |
| 261 | + Log.info "Will calculate factorial of {n}" |
| 262 | + (n == 0) ? 1 : n * factorial (n - 1) |
| 263 | +} |
| 264 | + |
0 commit comments