Skip to content

Adding Functions to the Language

Dmitry Ponyatov edited this page Feb 14, 2021 · 1 revision

5 Adding Functions to the Language

Let’s start turning this into a real programming language. We could add intermediate features such as conditionals, but to do almost anything interesting we’re going to need functions or their moral equivalent, so let’s get to it.

Exercise

Add conditionals to your language. You can either add boolean datatypes or, if you want to do something quicker, add a conditional that treats 0 as false and everything else as true.

What are the important test cases you should write?

Imagine, therefore, that we’re modeling a system like DrRacket. The developer defines functions in the definitions window, and uses them in the interactions window. For now, let’s assume all definitions go in the definitions window only (we’ll relax this soon [REF]), and all expressions in the interactions window only. Thus, running a program simply loads definitions. Because our interpreter corresponds to the interactions window prompt, we’ll therefore assume it is supplied with a set of definitions.

5.1 Defining Data Representations

To keep things simple, let’s just consider functions of one argument. Here are some Racket examples:

(define (double x) (+ x x))
 
(define (quadruple x) (double (double x)))
 
(define (const5 _) 5)
Clone this wiki locally