CS 330 Lecture 24 – Promises, Tail Recursion, and Currying
Agenda
- what ?s
- delaying evaluation
- tail recursion
- partially applying functions
TODO
- Read http://learnyouahaskell.com/higher-order-functions again. No quarter sheet.
Code
macro.c
#include <stdio.h>
#include <stdlib.h>
#define PRINTN(n, expr) \
for (int i = 0; i < n; ++i) { \
printf("%d", expr); \
} \
printf("\n");
int getZero() {
printf("1");
return 0;
}
int main(int argc, char **argv) {
PRINTN(5, getZero());
return 0;
}
promise.html
<!DOCTYPE html>
<html>
<head>
<title>...</title>
</head>
<body>
<script>
function Promise(thunk) {
this.thunk = thunk;
this.isResolved = false;
this.val = null;
}
Promise.prototype.resolve = function() {
if (!this.isResolved) {
this.val = this.thunk();
this.isResolved = true;
}
return this.val;
}
promise = new Promise(function() {alert('hello, world'); return 'A'});
document.write(promise.resolve());
document.write(promise.resolve());
document.write(promise.resolve());
document.write(promise.resolve());
document.write(promise.resolve());
document.write(promise.resolve());
document.write(promise.resolve());
document.write(promise.resolve());
document.write(promise.resolve());
document.write(promise.resolve());
document.write(promise.resolve());
document.write(promise.resolve());
document.write(promise.resolve());
document.write(promise.resolve());
document.write(promise.resolve());
</script>
</body>
</html>
sum.hs
sumAccum [] accum = accum
sumAccum (first:rest) accum = sumAccum rest (accum + first)
sum' list = sumAccum list 0
clamp lo hi n
| n < lo = lo
| n > hi = hi
| otherwise = n
clampPercent = clamp 0 100
Haiku
Got groceries once
Supper was bowls of flour
“You forgot the eggs”
Supper was bowls of flour
“You forgot the eggs”