Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Point-free programming style

Name: Anonymous 2009-01-04 19:30

How do I write the factorial function in point-free?

fact n = foldr1 (*) [1..n]

Which is faster
add1 = 1 +
or
add1 x = 1 + x
???

Name: IHBTC, AIDC 2009-01-05 15:37

Note that there's two separate factorial functions. I figured this would be fairer, especially in a tracing JIT. Constructive criticism welcome.

<!DOCTYPE html>
<html>
  <head><title>( ゚ ヮ゚) MITON GA SUKI!</title><meta http-equiv="Content-Type" value="text/html; encoding=utf-8"></head>
<script type="text/javascript">
function factorialAnon(x) {
  for(var i = x-1; i > 0; --i)
    x *= i;
  return x;
}

function benchAnon(control, fun, args) {
  var startDate = new Date().getTime();
  control(fun, args);
  var endDate = new Date().getTime();
  return endDate - startDate;
}

function loopAnon(n) {
  return function(f) {
    for(var i = 0; i <= n; ++i) {
      var res = f();
    }
  }
}
</script>

<script type="text/javascript">
function factorialFV(x){for(var i=x-1;i>0;i--){x*=i};return x}

function benchFV(x,y,z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10){var d=new Date();var std=d.getTime();var res=x(y,z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10);var d2=new Date();var std2=d2.getTime();
var diff=std2-std;return diff}
function loopFV(x,y,z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10){for(var i=y-1;i>-1;i--){var res=x(z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10)}}
</script>

<script>
function show(xs) {
  return "[" + xs.join(", ") + "]"
}

function doBench() {
  var res = [[], []]
  var n;
  try {
    n = parseInt(document.form.iterations.value);
  } catch(e) { n = 1000000; alert("You failed to type a proper number, so I'll just use 1000000"); }

  for(var i = 0; i < 5; ++i) {
    res[0].push(benchAnon(loopAnon(n), factorialAnon, [45]));
    res[1].push(benchFV(loopFV, factorialFV, n, 45));
  }
  document.documentElement.appendChild(document.createElement("HR"));
  var pre = document.createElement("PRE");
  pre.appendChild(document.createTextNode("With " + n + " iterations:\n  Anon:       " + show(res[0]) + "\n  FrozenVoid: " + show(res[1])));
  document.documentElement.appendChild(pre);
}
</script>
  <body>
    <form name="form">

      <input type="text" name="iterations" value="1000000">
      <input type="button" onclick="doBench()" value="Go!">
    </form>
    <p>Times are in milliseconds.</p>
  </body>
</html>

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List