Wednesday, September 24, 2014

Are we there yet? Writing and Running Scheme Code on Android

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!

8 comments:

  1. Agh, don't know if my previous comment got swallowed by the interwebs. If it didn't, just delete this one.

    Do you know if any of these schemes have bindings for some lightweight graphics in Android? I usually do mobile coding by ssh:ing to my server, but that's all text-only.

    Also, a full touch-enabled version of Vim is available in the F-Droid repository. If you use Vim then that's a great choice for an android editor.

    ReplyDelete
  2. You've mentioned that you've got Emacs running on Android I believe.

    When is your next blog on editing Scheme in Emacs and running it in Gambit? :)

    ReplyDelete
  3. Hey there,

    I'd also like to mention that CHICKEN Scheme has decent support for Android and iOS nowadays, and as you may know, there are plenty of libraries are available for CHICKEN. This is supported/used by two companies, one of which has also been so kind to release integration scripts for Android builds at https://github.com/chicken-mobile/android-chicken

    It's all somewhat "experimental", because there are so few people working with Android. Android itself is a very painful environment to develop for, as I can attest from my professional work using Apache Cordova (which is not even Scheme at all).

    Cheers,
    Peter Bex

    ReplyDelete
  4. Thanks for mentioning my blog. I use Gambit, just the native app with no external editor. It works, but is not convenient for much more than toy programming.

    ReplyDelete
  5. > Do you know if any of these schemes have bindings for some lightweight graphics in Android? I usually do mobile coding by ssh:ing to my server, but that's all text-only.

    Jan - I don't know about ths. I do know that Gambit comes with an example that shows how you can set HTML on the REPL's main text area. Perhaps that could be morphed into lightweight graphics?


    > Also, a full touch-enabled version of Vim is available in the F-Droid repository. If you use Vim then that's a great choice for an android editor.

    I'm a fan of Vim and use it as my preferred lightweight text editor. But, the keyboard I'm using on Android doesn't have an especially well positioned ESC key, so I find that I'm fighting with Vim all the time.

    > You've mentioned that you've got Emacs running on Android I believe.

    Grant, Good catch! Yeah, it occurred to me this weekend that I could / should setup emacs as the external editor that powers Gambit.

    > I'd also like to mention that CHICKEN Scheme has decent support for Android and iOS nowadays, and as you may know, there are plenty of libraries are available for CHICKEN.

    Do they have a REPL that you can install? Or, do you need to create your own embedded app? This is definitely good to know.

    > It's all somewhat "experimental", because there are so few people working with Android. Android itself is a very painful environment to develop for, as I can attest from my professional work using Apache Cordova (which is not even Scheme at all).

    Yeah, I do Cordova development as well, so know what you mean. It's tempting to consider setting up a full app development facility using scheme, but that's way overkill for what I need right now.

    > Thanks for mentioning my blog. I use Gambit, just the native app with no external editor. It works, but is not convenient for much more than toy programming.

    You're welcome. I've got say, as a programmer's scratchpad, Gambit + DroidEdit + an external keyboard works surprisingly well.

    ReplyDelete
  6. > Do they have a REPL that you can install? Or, do you need to create your own embedded app? This is definitely good to know.

    As far as I know there's no ready-to-install app in the app store right now.

    There's an "nrepl" egg which makes it easy to add a remote (networked) repl to any app, and of course making your own interface shouldn't be too hard either using something like the doodle egg.

    ReplyDelete
  7. Thanks for the info Peter!

    ReplyDelete
  8. I know this thread is 2 years old, but I have been using Termux on Android, and I was able to download, build, and install Chicken Scheme within Termux. I also installed Emacs and the Geiser plugin so I can either have the Chicken REPL within Emacs, or standalone - you can also install rlwrap and run Chicken under it. I don't think I would want to spend much time working this way without a bluetooth keyboard, but I thought I'd mention it.

    ReplyDelete

LinkWithin

Related Posts with Thumbnails