/whisper

A simple Lisp interpreter in Haskell

Primary LanguageHaskellOtherNOASSERTION

Whisper

Whisper is a simple toy LISP interpreter written in Haskell.

Below is a simple prime number checker that whisper is capable of running.

(define (is-prime-helper n x)
  (if (< n (* x x))
      true
      (if (= 0 (% n x))
          false
          (is-prime-helper n (+ 1 x)))))

(define (is-prime n)
  (if (< n 2)
      false
      (is-prime-helper n 2)))

(is-prime 479001598)
(is-prime 479001599)

Running the above code sample will print out:

false
true

Features

whisper supports many features familiar to Lisp derivatives, including:

Boolean Literals
true
false

Will print:

true
false

🤯🤯🤯

Integer Literals
42
3

Will print:

42
3

🤯🤯🤯

Native functions, such as `+`, `*`
(+ 1 2 3 4 5)
(* 2 4 6)

Will print:

15
48
Conditionals
(if true 0 42)
(if (< 3 1) 3 1)

Will print:

0
1
Define global variables and functions using `define`
(define global 3)

(define (minimum a b)
        (if (< a b) a b))

global
(minimum 7 100)

Will print:

3
7
Define scope-local variables using `let`
(define x 3)

(let ((x 0)
      (y 20))
     (* x y))

Will print:

0
Passing functions as values
(define x +)

(x 1 2 3)
((if true + *) 2 4 6)

Will print:

6
12
Recursion
(define (is-prime-helper n x)
  (if (< n (* x x))
      true
      (if (= 0 (% n x))
          false
          (is-prime-helper n (+ 1 x)))))

(define (is-prime n)
  (if (< n 2)
      false
      (is-prime-helper n 2)))

(is-prime 479001599)

Will print:

true

Running the Interpreter

REPL

You can easily spin up an interactive REPL with stack.

$ stack run whisper-exe
>>>

Run a File

You can run a file by passing the filename as an argument.

$ stack run whisper-exe examples/prime.lisp

false
true

Launch Interactive REPL After Running File

If you pass the -i flag before the filename, you can launch a REPL after loading the file.

$ stack run whisper-exe -i examples/prime.lisp

false
true
>>> (is-prime 7)
true

Implementation Blog

There are 3 blog posts about this project written during development.