Introduction to Scheme

Published on September 17, 2015

Introduction to Scheme

  • Tutorial

The most important, but at the same time the most imperceptible property of any tool is its influence on the formation of the habits of people who tend to use it.
Edsger Vyb Dijkstra


Part 1 Introduction to Scheme
Part 2 Deepening in Scheme
Part 3 IronScheme Practice

Introduction


In programming practice, there is often a need to write small scripts to automate various administrative processes, testing and monitoring. It is also not uncommon to need to embed an interpreter in an application or simply create a prototype to test an idea. For these purposes, you can use various popular JavaScript, Python, Lua, Bash, BAT, PHP tools and much more. And there is a need to store structured data in files or transfer over the network, when it comes to text formats, XML, JSON, CSV, even KV are usually used. However, despite the advantages and prevalence of such widely known tools, the obsession with finding a more flexible and elegant tool did not leave me alone.Thus, I once drew attention to the Lisp family of languages. And Lisp allowed to shoot at once all birds with one stone, and it’s beautiful and elegant. Since it has many implementations and standards for any needs and tastes. It can act both as an independent language and as an embedded language. It has a single format for representing data and program code. And most importantly, if necessary, writing your own interpreter is not an impossible task.

When should I use Lisp and when not? This article is devoted to various articles on the Internet. I do not presume to discuss this topic, but only notice where Lisp came in handy. For the most part, I used Lisp as an embedded language. To manage applications through the console, to create flexible configuration files, to store structured data, to transfer data over a network, to implement home -made home - made RPC. In my opinion, it is convenient when all of the above options have the same syntax and can still be expanded (in terms of syntax and functionality) and on the fly.

I do not consider myself a specialist in the Lisp world and I do not guarantee 100% accuracy of the material presented. One of the goals of this series of articles is to collect disparate information on various Scheme implementations in one place and in their native language. In general, this material is not for those who want to know why, but for those who want to know how. If someone will find the topic interesting and useful, write, we will clarify what is and think about the continuation.

Start


Almost every article about Lisp starts with Lisp being one of the oldest high-level and general-purpose programming languages ​​that was invented by John McCarthy around 1958. Despite the antiquity of Lisp, it is a multi-paradigm language that allows you to write in functional, procedural, object-oriented styles. At the same time, all this multiparadigmality is available through a primitive and uniform syntax, the so-called S-expressions. To describe Lisp syntax in the form of Backus-Naura, only 7 lines are enough, with reservations of course. It so happened that over the long history of development over Lisp the best minds of computer sciences have puzzled, polishing it like a gem. However, you can’t say that Lisp is very popular, perhaps due to a frightening pile of brackets, because of which, at first glance, the program looks complicated to perceive. But after a little practice, discomfort passes into delight from the possibilities that this method of programming provides. At least it was with me).

To be more precise, Lisp is not so much a programming language as an idea, on the basis of which the languages ​​of the Lisp family are developed. Today, there are a great many Lisp dialects, and there are even more implementations of them. Which is not surprising, because writing a Lisp interpreter is relatively easy. So the wheel of history turned that Common lisp, Scheme and Clojure became the most popular dialects. Each dialect pursues its own, slightly different goals. Common Lisp is a rather old industry standard; it has in its arsenal not a small number of libraries and developments. Scheme - seeks to minimize the basic constructions through which all other functionality, a variety of styles and programming approaches can be expressed. That is, a minimalist interpreter and a developed standard library. Clojure - a fresh look at Lisp in general, many language constructs have been rethought for convenient development on top of the JAVA platform in the first place. As they write on the forums, developing under Clujure is much more productive and interesting than on JAVA. For the sole purpose of self-study, I tasted different dialects and their implementation. You can argue Scheme vs Common Lisp indefinitely, but for myself I made a choice in favor of Scheme for its conciseness, modernity and accessibility of implementations on various platforms.

You can find reviews about Scheme on forums, as if it is an exclusively academic language, which, if it can be used in practice, is extremely inconvenient due to excessive minimalism. Something told me, it is not so. And now, my dear reader, I’ll tell you that scheme, a rather flexible programming language, is not overloaded with cunning designs and can successfully compete with many popular tools. And using it as an embedded language is a pleasure. The relative simplicity of the Scheme standard plays into the hands of many other implementations. In fact, you can choose an implementation for any needs and develop full-fledged GUI, DB, WEB applications using Scheme as the main language.

There are plenty to choose from


It is curious, but often the most popular products practically do not provide a choice, here is the only and unique version for you, and everything else is either outdated or someone else’s homemade product.

Scheme, perhaps due to the simplicity of the standard, has quite a few implementations, either interpreters, or compilers, or both at once. Moreover, there are compilers in other languages, which in some cases can be extremely useful.

A list of known implementations can be found on the net, for example, a table is published on community.schemewiki.org .
Known Scheme implementations

Name Link Type Platform Active R7rs
BDC Scheme carlstrom.com/bdc-scheme interpreter Java no
Bigloo www-sop.inria.fr/mimosa/fp/Bigloo compiler many yes
BiT github.com/melvinzhang/bit-scheme interpreter Hardware (microcontrollers) no
Biwacheche www.biwascheme.org interpreter Javascript yes
Bus scheme rubygems.org/gems/bus-scheme interpreter Ruby no
Chez scheme www.scheme.com interpreter (free) + compiler (paid) many no
chibi-scheme code.google.com/p/chibi-scheme interpreter C (library) yes yes
Chicken www.call-cc.org interpreter+compiler many yes yes
CPSCM www.omnigia.com/scheme/cpscm/home compiler Javascript, Common Lisp no
Elk sam.zoy.org/projects/elk interpreter C++ (library) no
Foment code.google.com/p/foment interpreter many yes yes
Gambit www.iro.umontreal.ca/~gambit interpreter+compiler many yes
Gauche practical-scheme.net/gauche/index.html interpreter many yes yes
Guile www.gnu.org/software/guile interpreter many yes yes
Heist github.com/jcoglan/heist/tree/master interpreter Ruby no
HScheme hscheme.sourceforge.net interpreter Haskell no
Husk Scheme github.com/justinethier/husk-scheme interpreter Haskell yes yes
Ikarus Scheme launchpad.net/ikarus compiler many no
Inlab-Scheme www.inlab.de/scheme/index.html interpreter Linux no
IronScheme www.codeplex.com/IronScheme interpreter .Net yes
Jaja pagesperso-systeme.lip6.fr/Christian.Queinnec/Java/Jaja.html interpreter Java no
JScheme jscheme.sourceforge.net interpreter Java no
Kawa www.gnu.org/software/kawa interpreter Java yes yes
KSI ksi.sourceforge.net interpreter C (library) no
KSM square.umin.ac.jp/~hchang/ksm interpreter C (library, Linux-only) no
Larceny www.larcenists.org compiler many no yes
librep librep.sourceforge.net interpreter C (library) no
LispMe www.lispme.de/lispme/index.html interpreter Palm no
Llava llava.org interpreter Java no
Luna sourceforge.net/projects/luna-scheme compiler .Net no
Microscheme github.com/ryansuchocki/microscheme compiler Hardware (Atmel) yes
MIT/GNU Scheme www.gnu.org/software/mit-scheme interpreter many yes
Minor Scheme www.red-bean.com/trac/minor compiler C no
MScheme mscheme.sourceforge.net interpreter Java no
mosh-scheme code.google.com/p/mosh-scheme interpreter many no
NexJ Scheme nexj-scheme.org interpreter Java no
Oaklisp oaklisp.alioth.debian.org interpreter POSIX no
Ocs github.com/felix-lang/ocs interpreter Ocaml no
Owl Lisp code.google.com/p/owl-lisp interpreter POSIX no yes
Picrin github.com/picrin-scheme/picrin interpreter C99 yes yes
Pixie Scheme III JayReynoldsFreeman.com/My/Pixie_Scheme_III.html interpreter+compiler iPad yes no
Pocket Scheme www.mazama.net/scheme/pscheme.htm interpreter Windows CE no
PS3I pagesperso-systeme.lip6.fr/Christian.Queinnec/VideoC/ps3i.html interpreter Java no
Psyche www.xs4all.nl/~yduppen/site/psyche.html interpreter Python no
QScheme www.sof.ch/dan/qscheme/index-e.html interpreter POSIX no
Racket www.racket-lang.org interpreter+compiler many yes
Rhizome/pi www.kt.rim.or.jp/~qfwfq/rhiz-pi/index-e.html ? ?
RScheme github.com/bitwize/rscheme ? ?
Sagittarius code.google.com/p/sagittarius-scheme interpreter many yes yes
Scheme 9 from Empty Space t3x.org/s9fes interpreter C89/POSIX, Plan 9 yes
Scheme48 s48.org ? ?
Scheme-to-C scheme2c.alioth.debian.org ? ?
Schemik schemik.sourceforge.net ? ?
Schemix www.abstractnonsense.com/schemix ? ?
SCM swissnet.ai.mit.edu/~jaffer/SCM.html ? ?
Shoe nocrew.org/software-shoe.html ? ?
SISC sisc.sourceforge.net ? ?
SIOD people.delphiforums.com/gjc/siod.html ? ?
SigScheme code.google.com/p/sigscheme ? ?
Sizzle www.grabmueller.de/martin/www/sizzle/sizzle.en.html ? ?
Stalin www.ece.purdue.edu/~qobi/software.html ? ?
STKlos stklos.sourceforge.net ? ?
SXM www.malgil.com/sxm ? ?
s7 ccrma.stanford.edu/software/snd/snd/s7.html interpreter C yes
TinyScheme tinyscheme.sourceforge.net ? ?
UCB Scheme www-inst.eecs.berkeley.edu/~scheme ? ?
ULisp www.zogotounga.net/comp/squeak/lispkit.htm ? ?
UMB Scheme www.cs.umb.edu/~wrc/scheme ? ?
Unlikely Scheme marijnhaverbeke.nl/unlikely ? ?
Vicare marcomaggi.github.com/vicare.html compiler POSIX/x86 yes
VSCM sourceforge.net/projects/vscm ? ?
Vx-Scheme colin-smith.net/vx-scheme ? ?
Wraith Scheme JayReynoldsFreeman.com/My/Software.html interpreter+compiler Macintosh Yes No
XLISP www.mv.com/ipusers/xlisper ? ?
Ypsilon Scheme code.google.com/p/ypsilon interpreter many no



And if you search thoroughly on GitHub , it becomes clear that there are even more options.

Following the standard in various implementations allows you to reuse code on different platforms, including for Web programming. But do not forget that functionality that is not part of the standard is likely to differ, which will lead to portability of the code.

In metal


More often in my practice I come across programming for .NET. Therefore, the solution that allows me to use .NET libraries and embed the Scheme interpreter in my applications is most useful to me. After playing with different options, I settled on IronScheme . So, how it, at first glance, seemed the most advanced implementation of the R6Rs standard for .NET. But there are others that will be written about in future articles.

IronScheme implements the sixth version of the R6Rs standard. Naturally, it has built-in functionality for interacting with the clr runtime. Thus, directly from the Scheme script we can create and manipulate .NET classes. And this is already enough to create a full-fledged application with GUI, DB and other goodies that are available from .NET. But we are not required to write full programs on IronScheme. Moreover, the supplied libraries have wrappers for a small number of standard .NET classes. Although no one is stopping us from helping the community.

Environment setup


  1. To start using IronScheme download the archive from ironscheme.codeplex.com/;
  2. unpack for example in “Program Files (x86)”;
  3. add to the PATH environment variable "C: \ Program Files (x86) \ IronScheme \";
  4. for convenience, in the directory with IronScheme I create a file “is.bat” with the contents of “IronScheme.Console-v4.exe% 1”;
  5. By executing the IronScheme.Console-v4.exe command, run the interpreter in REPL mode.

Now you can enter commands, for example, "(+ 2 2)". The interpreter in REPL mode supports autocompletion by pressing TAB, which is convenient to use as a reference command or to check code fragments. To exit the interpreter, type “(exit)”.

Hello world!


By tradition, we’ll write and launch the Hello world application. In any familiar text editor, preferably supporting Scheme, such as Sublime , create a file and save it hello-world.ss.

The contents of the hello-world.ss file:
(import 
  (rnrs) 
  (ironscheme)
)
(displayln "Hello, world!")

We start the command "IronScheme.Console-v4.exe hello-world.ss" as a result we get the long-awaited inscription.