Monthly Archives: April 2018

Fun with lambda calculus in Javascript

I would argue that Computer Science introduction courses should be using Javascript, not Java or Scheme, in many of their introductory courses.
1. Runs in a browser! Nothing to install!
2. Lambda calculus in ES6!
3. One could use the classic text and convert examples to javascript — it would be a good proof of understanding of the Scheme/Lisp.

I guess one could argue which has more real-world applicability — Javascript vs Java, but we all know one could learn either in a few weeks on the job.

EXAMPLE IN SCHEME/LISP from the classic:
Structure and Interpretation of Computer Programs, by Abelson, Sussman, and Sussman

ch1-Z-G-27.gif

(define (inc n) (+ n 1))
(define (identity x) x)
(define (sum term a next b)
  (if (> a b)
      0
      (+ (term a)
         (sum term (next a) next b)))) 

(define (sum-integers a b)
  (sum identity a inc b))

Then we can add up the integers from 1 to 10:

(sum-integers 1 10)

55

HTML HERE: LINK

MY EQUIVALENT JAVASCRIPT (utilizing ES6 “arrow functions”) IN THE CHROME/FIREFOX CONSOLE   (run on codepen)

inc = (x) => ++x;
identity = (x) => x; // for simple test with sum_integers below
sum = (term,a,next,b) => {
  if (a > b) 
    return 0; 
  else 
    return term(a) + sum(term,next(a),next,b); 
}
sum_integers = (a,b) => sum(identity,a,inc,b);
sum_integers(1,10);
55

ENGLISH

sum – a function which takes 4 arguments:
1) term – a lambda function describing how to evaluate each item
2) a – start with
3) next – a lambda function describing how to get to the next value between a and b
4) b – end with

To continue with the example from the STRUCTURES book…

SCHEME/LISP:

We can also define pi-sum in the same way:

(define (pi-sum a b)
  (define (pi-term x)
    (/ 1.0 (* x (+ x 2))))
  (define (pi-next x)
    (+ x 4))
  (sum pi-term a pi-next b))

Using these procedures, we can compute an approximation to pi :

(* 8 (pi-sum 1 1000))
3.139592655589783

JAVASCRIPT:   (run on codepen)

pi_sum = (a,b) => { 
    let pi_term = (x) => 1/(x*(x+2));
    let pi_next = (x) => x+4; 
    return sum(pi_term,a,pi_next,b); 
}
8 * pi_sum(1,1000);
3.139592655589783

Cool! Of course to me the original is being a bit more difficult than it needs to in explaining this concept by
1) obfuscating with the naming of arguments
2) one-liner silliness
3) adding recursion into the mix

But OK. We’ll forgive them! Onward with the javascript!


RELATED ELSEWHERE:
TL;DR – Lambda means “function used as data”

Leave a comment

Filed under programming

Social Media and Variable Ratio Reinforcement Schedules

Why social media is addictive:

“Intermittent reinforcement works better. … 9 of out 10 things in my feed are complete garbage—last week’s newspaper lining the birdcage with the droppings already on it—but then once every two weeks I find out my niece is engaged or my best friend got a great new job or my oldest friend is in town and I should make plans to hang out. And now no matter how full the Facebook feed is of bird droppings I still have to keep going back.”
Joel Spolsky

“It’s a social-validation feedback loop … exactly the kind of thing that a hacker like myself would come up with, because you’re exploiting a vulnerability in human psychology.”
Sean Parker, first president of Facebook

“[I]f you are addicted to social media, you are not just weak willed. Console yourself by realising that you are instead a pigeon, in a box, pecking at a lever interminably.”
B.F. Skinner Likes Your F.B. Status

“Variable ratio: rapid, steady rate of responding; most resistant to extinction.”
Operant Conditioning: “Reinforcement”, Wikipedia

Leave a comment

Filed under social media