my-little-schemer

1.0.0 • Public • Published

My Little Schemer

An interface for interoperable Scheme and JavaScript. Inspired by The Little Schemer, 4th Ed.

Design and purpose

This module provides an interface for integrating Scheme into JavaScript applications. The primary goal is interoperability, rather than writing in Scheme alone (although that is an option). Hence, this dialect of Scheme, while inspired The Little Schemer, 4th Ed, has some additions and changes that make it more useful for integration in JavaScript apps.

The power and flexibility of Scheme syntax can be harnessed to do what functional languages do best — pure functions, recursion, parsing other languages, etc. The jsExpression() function converts strings representing valid S-Expressions into a useful JavaScript datatype that can be composed and manipulated using Scheme, JavaScript, or Scheme-like JavaScript. The sExpression() function converts them back into strings. The purpose is not to write in Scheme alone, but to combine Scheme and JavaScript to open up new possibilities. Hence, Output can be passed between Scheme and JavaScript. Functions defined in Scheme can be used in JavaScript, and vice versa!

Four ways to write

There are four ways to write using this module:

const s = require('my-little-schemer');

// Scheme
s.evaluate(`(
  (define isLat
    (lambda (l)
      (cond
        ((isNull l) #t)
        ((isAtom (car l)) (isLat (cdr l)))
        (else #f))))
  (isLat (cons cat (dog)))
)`, false, true, true); // #t

// jScheme
s.value(['cons', 'bird', ['cdr', ['mouse', 'house']]]) // [ 'bird', 'house' ]

// SchemeJS
s.isLat = (l) => {
  if (s.isNull(l)) {
    return true;
  }

  if (s.isAtom(s.car(l))) {
    return s.isLat(s.cdr(l));
  }

  return false;
};

s.cons(s.isLat(['<3']), ['love']); // [ true, 'love' ]

// Creatively
s.isFinWord = word => word.slice(-1) === '.';
ss['>'] = (n, m) => n > m;

s.evaluate(`
  (define hasRunOnSentence
    (lambda (p n)
      (cond
        ((> n 20) #t)
        ((isNull p) #f)
        ((isFinWord (car p)) (hasRunOnSentence (cdr p) 0))
        (else (hasRunOnSentence (cdr p) (add1 n))))))
`);

function checkForRunOnSentences(p) {
  console.log(s.hasRunOnSentence(s.jSExpression(`(${p})`), 0) ? 
`Whoa, you've got a long one there!` : `This is OK.`);
}

const para = `She went to the movies.
  Then she met a friend for ice cream at that one place 
  she loved so much that she went to that one time.
  Then she went home.`

checkForRunOnSentences(para); // Whoa, you've got a long one there!

Installation

From the command line:

npm install my-little-schemer

Usage

Including the module

CommonJS:

const s = require('my-little-schemer');

ES6:

import s from 'my-little-schemer';

jS-Expressions

const sExp = '(car (cdr (cat dog)))';

s.jSExpression(sExp); // [ 'car', [ 'cdr', [ 'cat', 'dog' ] ] ]

jSExpression(string: string) converts a string representing a valid Scheme S-Expression into a "jS-Expression", which is a manipulatable JavaScript data structure. The conversion is done according to the following mapping (S --> JS):

  • Lists --> arrays
  • Numbers --> numbers
  • #t --> true
  • #f --> false
  • #n --> '\n'
  • #NaN --> NaN
  • #Infinity --> Infinity
  • #null --> null
  • #undefined --> undefined
  • All other atoms --> strings

Whitespace, line breaks and '\n' are ignored. You can use the special symbol #n instead of \n. Other escaped characters (e.g., '\'' ) work as you would expect. The argument must be a string.

sExpression(exp: jSExpression) can be used to convert a jS-Expression back into a S-Expression:

const jSExp = ['car', ['cdr', ['cat', 'dog']]];

s.sExpression(jSExp); // ( car ( cdr ( cat dog ) ) )

Evaluation

const sExp = `
  ((define insertR
    (lambda (new old lat)
      (cond 
        ((isNull lat) (quote ()))
        (else
          (cond 
            ((isEqan (car lat) old) (cons old (cons new (cdr lat))))
            (else (cons (car lat) (insertR new old (cdr lat)))))))))
  (define sentence
    (quote (My favorite pudding)))
  (quote sentence)
  (quote (insertR chocolate favorite sentence)))`;

const jSExp = s.jSExpression(sExp);

s.evaluate(sExp); // [ undefined, undefined, [ 'My', 'favorite', 'pudding' ], [ 'My', 'favorite', 'chocolate', 'pudding' ] ]

s.evaluate(jSExp, true); // [ undefined, undefined, [ 'My', 'favorite', 'pudding' ], [ 'My', 'favorite', 'chocolate', 'pudding' ] ]

s.evaluate(sExp, false, true, false); // [ 'My', 'favorite', 'chocolate', 'pudding' ]

s.evaluate(sExp, false, false, true); // ( #undefined #undefined ( My favorite pudding ) ( My favorite chocolate pudding ) )

evaluate(scheme: any, js: bool, final: bool, convert: bool) evaluates a string representing a valid S-Expression or, if the js option is true, a jS-Expression, and returns a jS-Expression containing the results. If the final option is true, then only the final result is returned. If convert is true, then the result is returned as an S-Expression. The options all default to false.

value(exp: jSExpression) can also be used to evaluate a jS-Expression. The output is another jS-Expression. It is equivalent to evaluate(jSExpression, true, false, false).

Functions

Primitive functions

The following primitives are defined:

  • isList
  • isAtom
  • isNumber
  • isNull
  • car
  • cdr
  • cons
  • isDefined
  • isFunction
  • quote
  • define
  • undefine
  • cond
  • lambda
  • isEqan
  • isEqual
  • isEqlist
  • ||
  • &&
  • sub1
  • add1
  • isZero

All work as you would expect. Note that anything that is not a list is an atom (including functions, booleans, etc.). Since ? is an operator in JavaScript, Scheme question forms like null? are converted to "is" forms like isNull.

In addition, a few helpful arithmetic functions have been defined as primitives:

  • +
  • -
  • *
  • /
  • %

All are equivalent to the JavaScript symbol, although usage follows Scheme syntax with the operator in the front: (+ n m) or ['+', 'n', 'm'] or ss['+'](n, m) , depending on the context you are writing in. The number set is not restricted to integers. Rational numbers are allowed.

Finally, there are the parsing and evaluation functions:

  • jsExpression
  • sExpression
  • value
  • evaluate

Pure functions and define

All of the predefined functions are pure functions, with the exception of define. The language primitives are all defined within the module's namespace, i.e. the ss object. Variables defined with define / define() will be defined inside of ss . Thus, it is an impure function which changes the state of the module and essentially creates new primitives. While this is not how actual Scheme works, it is useful for integrating with JavaScript. Note that isDefined and the evaluators will only recognize definitions and perform substitutions if the variables are defined inside of ss. However, you can also pass functions and variables from other namespaces into the evaluator in your jS-Expressions:

function pickle(x) {
  return s.cons('orange', x);
}

const juice = ['ice', 'cream'];

const js1 = ['car', ['pickle', 'juice']]; // pickle
const js2 = ['car', [pickle, juice]]; // orange
const js3 = ['cdr', [pickle, juice]]; // [ 'ice', 'cream' ]

Package Sidebar

Install

npm i my-little-schemer

Weekly Downloads

1

Version

1.0.0

License

MIT

Last publish

Collaborators

  • ssmolkin1