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

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

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

Finding a builder or contractor

LINK: http://www.greenbuildingadvisor.com/blogs/dept/qa-spotlight/fixing-poorly-insulated-roof

“Local builders aren’t getting it

Butson says the local builders he’s contacted haven’t been of much help.

“More broadly, I am slowly coming to terms with the fact that I will have to do most of the engineering work for this renovation,” he says. “I have hired an architect who can create plans and construction details for what I want to execute. What I have not been able to find is a builder who seems to understand these issues or possible solutions.”

Butson has spoken to several builders, but they all insist that he will need heating cables.

“I fundamentally disagree with this, but I doubt I will be able to convince them otherwise,” he says, “and even if I do them, I don’t have confidence in their ability to execute the necessary modifications and details.”

For homeowners in search of a builder who is familiar with building science principles, one option, according to Holladay, is to contact a local energy rater certified by RESNET (resnet.us) tor the Building Performance Institute (bpi.org). To find a local energy rater, Butson can use the search function on one or both of these two websites.

“Call up the energy rater and ask for the names of local contractors who understand energy issues, building performance issues, and building science issues,” Holladay says. “Good luck.”

Leave a comment

Filed under erik-green, green

Radon

“The World Health Organization is recommending that the radon
action level be reduced to 2.7 PC/L from the US current standard of 4 PC/L”

Leave a comment

Filed under erik-green, green

Pros and Cons of Android vs iOS Circa October 2017

(I will update this from time to time… last updated Oct 2017)

All the apps I use are really on each so no issue there. So it really comes down to silly things.
Most people seem to be locked into one or the other. I’ve used both quite a bit and am agnostic/impartial. So here are the things I’ve noticed. Things to keep in mind… an important one to me is Google Voice integration. For some it might be some camera features.

What I would buy:
iPhone SE A1662 32gb Verizon or better… SIM FREE
Moto G 4th generation / “Moto G4” unlocked

Android Pros
========
– I personally think the web interface of archived photos is about 1-million times better than iOS
– I love being able to press ANYWHERE on the screen to take a picture in the camera app. Easier to switch to video than in iOS
– my older 3G phone doesn’t seem to prevent tethering which is helpful when travelling (I believe this is because the OS is out of date… in this case a good thing!)
– “if this phone is lost call XXX-XXX-XXXX” msg on lock screen is trivial! So helpful! Has saved us once!
– photos are widescreen. Apple still does 4×3 type photos?!?!?!
– Google Voice integration. Being able to MAKE calls appearing as your google voice # makes it easier to switch networks/SIMs.

Android Cons
=========
– cannot install Amazon apps without having to go thru all sorts of ridiculous stuff (because they don’t want normal Android users to compete with Fire???)
– if everyone else is on Apple… no apple messaging/facetime
– older androids had much worse security choices in apps and storage was not encrypted by default. Better starting in Android 6.0

iOS Pros
======
– Old phones are MUCH MUCH MUCH more supported by new updates to the OS. iOS 11 on iPhone 5S
– Much better news app
– Google knows enough about me already.
– camera is great
– slowmo mode and timelapse are great
– can still get a small phone! iPhone SE is great!
– locking out apps when driving!
– 5 presses Emergency Call feature!

iOS Cons
======
– Expensive to get a FULLY unlocked (CDMA/GSM) phone which will work on all 4 major networks. “SIM FREE via Apple Store”. If you just need *GSM* unlocked, I think that’s easier — like I think the iPhone SE A1662. But it’s a bit murky. Web pages explanations don’t necessarily match my experience with carriers.
– Setting up data networks is sometimes a mess. If you cannot find the proper profile file to set the APN,
you CANNOT do it manually. Also it (for me in iOS 11) failed once and I had to restore from a backup I had made in iTunes! (Yikes!)
– Getting a “if this phone is lost, call XXX-XXX-XXXX” msg on the lock screen is nearly impossible. This msg WILL appear
once you REALIZE it is lost and lock it from iCloud, but having it right on the lock screen is WAY BETTER!

Leave a comment

Filed under shopping

Screentime and… What is Work?

Great article from Silvia Beier on the nature of work for many of us and what this means when we worry about screen time for our kids.  That’s right… look in the mirror!  And I don’t mean that in a negative way per se.  Just that a lot of the work we do looks EXACTLY LIKE what our kids look like when they are on screens — whether they are watching a TV show or vlog, or playing video games — either alone or with a mob of friends on a minecraft or terraria server.

I sometimes feel like the hard work they are doing understanding Terraria (“OK GOOGLE: terraria vortex wings”…) is EASILY as complicated as the software engineering work that I do for a living.  (They would probably love it!)  But for now… this PLAY is their WORK.  I see no difference between the WebEx meeting I had this afternoon — sharing screens and discussing a design with colleagues — as when my kids are working on crafting and defeating bosses in the latest game while discussing it all on Skype running in the background.  Some in the same room or across the house…. others across town.  Really.  It’s awesome, it’s natural, and it’s work.

http://www.sudval.org/essays/102016.shtml

Silvia Beier, Are They Getting Their Work Done?, October, 2016

Leave a comment

Filed under screen-time, screens, Sudbury Schools and Sudbury Valley School, what is work