Skip to content

Instantly share code, notes, and snippets.

@martintrojer
Last active September 30, 2015 01:08
Show Gist options
  • Save martintrojer/1698008 to your computer and use it in GitHub Desktop.
Save martintrojer/1698008 to your computer and use it in GitHub Desktop.
tail-calls
IL_0000: nop
IL_0001: newobj instance void Program/b@12::.ctor()
IL_0006: ldarg.0
IL_0007: ldc.i4.1
IL_0008: add
IL_0009: tail.
IL_000b: call int32 Program::a(class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<int32, int32="">, int32)
IL_0010: ret
0 getstatic #27 <app stack$b.const__0="">
3 invokevirtual #54 <clojure lang="" var.getrawroot="">
6 checkcast #56 <clojure ifn="" lang="">
9 getstatic #31 <app stack$b.const__1="">
12 invokevirtual #54 <clojure lang="" var.getrawroot="">
15 aload_1
16 aconst_null
17 astore_1
18 lconst_1
19 invokestatic #62 <clojure lang="" numbers.add="">
22 invokeinterface #65 <clojure ifn.invoke="" lang=""> count 3
27 areturn
bool inA = true;
while (true) {
if (inA) {
// do A stuff
inA = false;
}
else {
// do B stuff
inA = true;
}
}
let a (f:int->int) n =
f (n + 1)
let rec b n =
a b (n + 1)
(defn sum3[k n]
(if
(= n 0) (k 0)
(sum3 (fn [m] (k (+ n m))) (dec n))))
(sum3 identity 10)
def sumImperative(n:Int):Int = {
var res = 0
for (i <- n to(0, -1)) {
res = res + i
}
res
}
public static int sum(int acc, int n) {
while (n != 0) {
int arg_12_0 = n + acc;
n--;
acc = arg_12_0;
}
}
public int sum2(int acc, int n) {
while (true) {
if (n == 0)
return acc;
n -= 1; acc = n + acc;
}
}
(defn sum2[acc n]
(if
(= n 0) acc
(recur (+ n acc) (dec n))))
def sum2(acc:Int, n:Int):Int = {
if (n==0) acc
else sum2((n+acc),(n-1))
}
let rec sum n =
if n = 0 then 0
else n + sum(n-1)
(defn a[f n]
#(f (inc n)))
(defn b[n]
#(a b (inc n)))
(trampoline b 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment