Category Archives: programming

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”

Advertisements

Leave a comment

Filed under programming

Erik’s coding/software engineering links for Feb 2018

1. Avoid Else, Return Early
COMPLETELY AGREE! I learned this basic idea from a grad-student teaching Freshman CS201 Pascal in 1988

2. Ranking in SQL using self-joins is a silly solution O(n^2) LINK

3. Cool SQL solutions to “top N per group” issue

4. Organization skills beat algorithmic wizardry

5. You’re scaling your app too early

6. RAM is cheaper than software engineers
“given sufficient RAM that it never has to go to disk. I’m hoping that it will run fast enough that my friend won’t have to pay his contract programmer to improve the performance.”

7. What goes into a good job ladder

8. “Most of the “methodology” issues in today’s software engineering/development industry were solved decades ago by practitioners of systems engineering while building rockets, etc. Good resources: http://www.incose.org‘s Systems Engineering Body of Knowledge, NASA’s Systems Engineering Handbook, MIT’s Systems Engineering Fundamentals.”

Leave a comment

Filed under programming

What kind of programmer are you?

“If I were in my teens today, Zuck would not be my role model.
You know who would be my role model? Banksy!”
http://scripting.com/2013/12/31/twitterAsANotepad.html

See also:
http://scripting.com/2014/08/27/whatCoderMeansToMe.html

Leave a comment

Filed under art, contrarian, motivation, person: Dave Winer, programming, work

The Wolf

From https://medium.com/p/9bc38b3e8a2e

wolves

If you build software for a living (or otherwise), go read these three posts.

  1. @rands: The Wolf
  2. @kellan: “Wolf” narrative considered harmful (also biologically unlikely)
  3. @codinghorror (from 2004): Commandos, Infantry and Police

Leave a comment

Filed under art, computers, contrarian, programming, software, technology, work

“If you want to get the most out of great developers like Engelbart, who are productive well into their 80s, you have to stop digging up the streets, moving the goalposts, bombing the cities, starting over just for the sake of starting over.”

http://threads2.scripting.com/2013/july/engelbartWasSidelined

Leave a comment

Filed under ageism, art, programming

The personal vs the aggregate in computers (and hence, programming)
http://threads2.scripting.com/2013/june/theHumanWave

This resonates with me as well.

See also:
Battle for the planet of the APIs
http://adactio.com/journal/6291/
“The official line from Twitter is that RSS is “infrequently used today.” That’s the same justification that Google has given for shutting down Google Reader. It reminds of the joke about the shopkeeper responding to a request for something with “Oh, we don’t stock that—there’s no call for it. It’s funny though, you’re the fifth person to ask today.””

Leave a comment

Filed under computers, programming

the personal cloud

Awesome awesome video of Doc Searls on the personal cloud. We have personal computers. And — he nails it — we should (we will soon) have personal clouds. He explains it very well using some physical object analogies. “Life Management Platforms” “We are in charge”

LINK

Leave a comment

Filed under programming