Wednesday, October 07, 2015

More Linux-on-Android success: emacs, racket and git

Shira often cringes when I join her at the craps table. Will I drop some forbidden statement like wow, nobody's rolled a 7 in forever!, thereby guaranteeing the next roll to be a 7? In general, I just don't respect the juju that goes with gambling; that underlying assumption that through external means you can control random events. Turns out, this is a studied phenomena, known as the Hot Hand Fallacy. When ProgrammingPraxis took up the topic, I knew I had to jump in and implement the exercise.

I was writing this on fresh Linux system, so the first order of business was to pull in tools. I did something along the lines of:

  apt-get install emacs racket git

The plan was to code the solution in emacs, execute it in racket and publish my work using git. Once the above command finished, I fired up emacs, kicked off a Scheme buffer powered by racket and started coding away. The ProgrammingPraxis exercise called for the implementation of an experiment mentioned in the Wall Street Journal that showed how the Hot Handy fallacy could be experienced. Here's my implementation:

;; http://programmingpraxis.com/2015/10/02/the-hot-hand/

(define (flip)
  (if (= (random 2) 1) 'H 'T))

(define (sample)
  (list (flip) (flip) (flip) (flip)))

(define (hot? s)
  (cond ((null? s) (void))
 ((null? (cdr s)) (void))
 ((eq? 'H (car s)) (eq? 'H (cadr s)))
 (else (void))))

(define (collect fn sample)
  (if (null? sample)
      null
      (let ((v (fn sample)))
 (if (void? v)
     (collect fn (cdr sample))
     (cons v (collect fn (cdr sample)))))))

(define (only-true items)
  (filter (lambda (t) t) items))

(define (percentify items)
  (if (null? items) 0https://mail.google.com/mail/u/0/#inbox/1504362844121cc9
      (exact-gt;inexact (/ (length (only-true items))
    (length items)))))

(define (try count thunk)
  (let loop ((avg (thunk)) (count (- count 1)))
    (if (= 0 count)
 avg
 (loop (/ (+ (thunk) avg) 2) (- count 1)))))

(define (experiment)
  (percentify (collect hot? (sample))))

Each experiment consisted of flipping a coin 4 times and reviewing the outcome. I then kicked off 2,000,000 of these experiments:

 (try 2000000 experiment)

Here's a screenshot of the results:

If you squint, you'll see that I did occasionally get close the 40% behavior mentioned in the article, though I got plenty of other behavior, too. Chances are, the implementation above is buggy.

The little Android icon in the screenshot above reveals the truly remarkable part of this exercise: the above was all executed on my Galaxy Note5 using Gnuroot. I was amazed at how nearly flawless emacs and racket performed. It was just like I was on any old Linux environment. And when I was finished with my solution, I pushed the code to github using the command line version of git.

I was disappointed to see that Gambit Scheme is no longer available on Google Play. But having access to racket and other standard Linux tools makes up for this and then some.

I did note above that the tools were nearly flawless. There were some gotchas, including:

  • emacs: The arrow keys can become undefined. A fix is shown here
  • emacs: my standard desktop configuration loaded up properly, but I did get a warning message about being "past 95% of memory limit." I don't know what that means, but it sounds scary.
  • gnuroot: I don't have a good way to swatch back and forth between a Gnuroot terminal and other Android apps. I can bring up the Gnuroot launcher, but that means having to kick off another terminal session.

But these issues were all minor when compared to how much Gnuroot Just Works.

I see that the next ProgrammingPraxis exercise also covers a gambling related topic. Time to bust out my keyboard and get to work!

4 comments:

  1. Anonymous4:08 PM

    Hi Ben,

    did you need any special configuration to make git work with Gnuroot?

    ReplyDelete
  2. > did you need any special configuration to make git work with Gnuroot?

    Nope. I went through the exact same setup on Gnuroot that I would on any other Linux command line environment.

    ReplyDelete
  3. you can get back to a running gnuroot session by clicking the notification in the drag down from the top of the screen notifications.

    ReplyDelete
  4. ""you can get back to a running gnuroot session by clicking the notification in the drag down from the top of the screen notifications.""

    That's true. However, when using a keyboard, it would be really handy to be able to switch back to it using a keyboard shortcut, rather than using a screen shortcut.

    But I can't really complain, Gnuroot is awesome!

    ReplyDelete