Bringing Scheme to the iPhone
Engineer Bainomugisha, Pr. Theo D'Hondt(VUB)
This page summarises our results from our experiments on porting Scheme to the iPhone platform. Our driving motivation is to bring a host of well-known Scheme benefits (e.g., closures and first class functions, structural macros, and automatic garbage collection) to the iPhone development.
Scheme on the iPhone, Video
The video below shows the sample Scheme expressions running on the iPhone. The video clip is available at Scheme on iPhone Video.
An Interactive Scheme Environment for the iPhone
To showcase the interactive Scheme environment on the iPhone device, we have developed a simple editor named iScheme. You can type Scheme expressions or load files of code and evaluate them by pressing the return key twice and see the result.
The interactive interface is developed using Objective-C's UIKit. At the back-end we are using MacScheme – a Scheme interpreter mostly R5RS complaint developed at our lab. So far we have performed tests on NON Jail broken iPhone OS 2.2+.
Accessing iPhone APIs from Scheme
In our ongoing work, we are implementing Scheme/Objective-C bridge to allow Scheme programs access to iPhone APIs (GPS, accelerometer and distribution). On top of the Scheme/Objective-C bridge, we provide high-level abstractions for common cases.
- Location information from GPS.
We provide CURRENT-LOCATION abstraction to obtain the longitude and latitude information from the iPhone's GPS.
(begin (CURRENT-LOCATION (lambda (latitude longitude) (newline) (display (list latitude longitude)) (newline)))) (37.33168900 -122.0307310)
The argument to CURRENT-LOCATION is a Scheme procedure that is called with the values of latitude and longitude as soon they become available.
- Interaction with the SMS and Phone
We provide abstractions to interact with some of native applications. For example starting a phone call or launching an SMS application as follows:
(make-call "026291241")
Dials the specified telephone number.
(sms "026291241")
Launches the SMS application with the to field populated with the specified telephone number.
make-call and sms abstractions are only available on the real iPhone device and not on the simulator.
Access to other Objective-C classes is possible using the bridge abstractions OBJC-INSTANCE and OBJC-SEND to create and send messages to Objective-C instances, respectively. In some cases this might involve writing code in both languages (Scheme and Objective-C). Our target is to move much of implementation to Scheme and depend on Objective-C only for heavy GUI intensive implementation.
The example below shows the bridge abstractions in action by implementing a prototype implementation controlling the iPhone's inbuilt iPod from a Scheme application.
;;example of ipod controller (define (ipod-controller) (let* ((Controller (OBJC-CLASS MPMusicPlayerController)) (Query (OBJC-CLASS MPMediaQuery)) (query (OBJC-SEND Query songsQuery)) (musicPlayer (OBJC-SEND Controller iPodMusicPlayer))) (OBJC-SEND musicPlayer setQueueWithQuery: query) (lambda (action) (case action ((play) (OBJC-SEND musicPlayer play)) ((stop) (OBJC-SEND musicPlayer stop)) ((pause) (OBJC-SEND musicPlayer pause)) ((play-next) (OBJC-SEND musicPlayer skipToNextItem)))))) ;; example usage (define my-ipod-controller (ipod-controller)) ; to start playing (my-ipod-controller 'play) ; to play next song (my-ipod-controller 'play-next)
iScheme: Ambient-oriented programming for the iPhone
We have built a distribution layer using the Scheme/Objective-C bridge abstractions that eases development of distributed iPhone applications. Detailed explanation of iScheme is available here. Our representative AmbiScrabble game application developed in iScheme is also available here.
Installation Process (Now available)
Download the application files iPhone-Scheme3.0.zip. Included in the archived file is
- 3.x folder with application sandbox folder that you can copy paste to your iPhone Simulator. You need the iPhone SDK 3.0+
- ReadMe.txt file with installation instructions and requirements
- Sample Scheme code for test purposes (fibonacci, retrieving configuration settings, maps example)
Previous Version
If you still run iPhone SDK 2.x, the very first version with tiny input is available for download at iPhone-Scheme.zip. Please read the ReadMe.txt files for the installation procedure.

