Here's the solution to the most recent Programming Praxis problem. It's not the most pithy or elegant code, but it gets the job done:
; Solution to: http://programmingpraxis.com/2014/09/23/triangle-roll-up/
(define first car)
(define second cadr)
(define (sum row)
(let loop ((row row) (result '()))
(if (= (length row) 1)
(reverse result)
(loop (cdr row)
(cons (+ (first row) (second row))
result)))))
(define (roll numbers)
(let loop ((numbers numbers) (result (list numbers)))
(cond ((< (length numbers) 2)
(reverse result))
(else
(loop (sum numbers) (cons (sum numbers) result))))))
(define (fmt results)
(for-each (lambda (r)
(display r)
(newline))
(reverse results)))
> (fmt (roll '(4 7 6 3 7)))
(87)
(46 41)
(24 22 19)
(11 13 9 10)
(4 7 6 3 7)
One reason it's not streamlined: I worked out the solution on my Galaxy S5. As a Schemer at heart, I've always wanted to have a Scheme interpreter running on my phone . Unfortunately, I've never found an environment robust enough to be anything more than a basic demo.
But times change, so I figured it was worth checking the Play Store for new Scheme options.
To my delight I found quite a few. There's Scheme REPL, Scheme Droid, Gambit and Scheme Pad. They're all very promising. I figured trying to solve the Programmin Praxis problem would be an ideal test. Note, I'm using my Perixx 805L Keyboard for text entry. None of these environments would be of any use without an external keyboard. As usual, the Perixx worked really well. (Allowing me to develop a Scheme solution and compose this blog entry.)
Casually browsing through the apps, Scheme Pad looked most promising. It had a way to load and save files and included parenthesis matching. Paren matching is absolutely key. Unfortunately, Scheme Pad is too smart for its own good. It tries to automatically send lines of code to the REPL as you enter them, and I quickly found that I could confuse the app. When it works, it's awesome, but I found myself having to load and reload my example file to keep it working.
Back to the drawing board.
When I went back to the apps, I was pretty bummed out. None of them had the paren matching Scheme Pad had, so they really weren't going to be realistic solutions.
Then I thought I'd take another approach. What if I used an external text editor to do the authoring of the scheme file and just used one of the REPLs to interpret it.
After a couple of false starts I found Droid Edit, which is quite the impressive editor. It does paren matching, text highlighting, and responds to keyboard short cuts. I'll almost certainly be buying the pro version to get rid of the ad in the bottom right hand corner.
I found that I was able to scratch out some Scheme code without any difficulty. I could then flip over to the Gambit REPL and load in my file:
(load "/sdcard/Documents/ex1.scm")
The Gambit App allows you to scroll back in the REPL and hit enter on a previously run expression. This queues up the expression for you to edit and run. The result is that I had to type my load command once and then could re-run it.
The edit, Alt-Tab, load, run, loop isn't quite the most efficient way to program. But, both the editor and REPL are excelling what they do, so it seems reasonable to put up with the two app solution.
Not to mention, I'm fairly certain I could automate the process further by using the External Keyboard app's shortcuts or perhaps using Tasker.
Here's some screenshots of the development in action:
I think it'll take a bit more tweaking, but it does seem that a Scheme environment that Really Works is achievable on Android.
Whoo!