Systematic programming

Introduction

Beginning Student Language (BSL) is a programming language which is great for learning systematic programming because, unlike popular languages, the knowledge learned in BSL is transferable to any other language. DrRacket is an integrated development environment (IDE) for BSL. In BSL all expressions start with an open parenthesis ( and end with a close parenthesis ).

Numbers

There is no difference between integers and real numbers in BSL. Arithmetic operations can be nested or supplied with multiple arguments. If the output of calculation starts with #i, it means that the number produced is not exact, for example #i-1.0.

(+ 3 4)
(- 9 5)
(* 2 (* 7 10))
(/ 24 2 6)
(sqr 3)
(expt 2 3)
(sin 0)
(cos pi)

Strings

Strings in BSL are enclosed in double quotes ". There are many predefined functions that can do operations on strings.

(string-append "hello " "world" "!")
(number->string 43)
(string->number "17")

Booleans

Boolean values are represented by #true and #false.

(and #true #true)
(or #false #true)
(not #false)

(and (or (= (string-length "hello world")
            (string->number "11"))
         (string=? "hello world" "good morning"))
     (>= (+ (string-length "hello world") 60) 80))

Images

It is possible to use images and produce images in BSL directly in the program file. In order to do this, we have to add library 2htdp/image at the beginning of the program. There are many operations supported for images. For example, overlay puts images on top of one another, empty-scene creates a canvas for placing images, and place-image places the image into the canvas.

(require 2htdp/image)

(* (image-width (circle 5 "solid" "green"))
   (image-height (circle 5 "solid" "green")))

(overlay (circle 5 "solid" "red")
         (rectangle 20 20 "solid" "blue"))

(overlay (rectangle 20 20 "solid" "blue")
         (circle 5 "solid" "red"))

(place-image (circle 5 "solid" "green")
             50 80
             (empty-scene 100 100))

Functions

(define (FunctionName ParameterName) BodyExpression)
(FuncationName ArgumentExpression)

The first line shows how to define a function, and the second shows how to invoke a function.

(define (y x) (* x x))
(y 3)

For example, the function animate from 2htdp/universe library creates an animation from a supplied function.

(require 2htdp/image)
(require 2htdp/universe)

(define (picture-of-ball height)
  (place-image (circle 5 "solid" "green")
               50 height
               (empty-scene 100 60)))

(animate picture-of-ball)

Conditional expressions

(cond
  [ConditionExpression1 ResultExpression1]
  [ConditionExpression2 ResultExpression2]
  ...
  [ConditionExpressionN ResultExpressionN])

Conditional expressions in BSL are evaluated the following way. If ConditionExpression1 is #true, DrRacket replaces it with ResultExpression1, evaluates it and uses the result of the entire cond expression. If ConditionExpression1 is #false, DrRacket drops the first line and starts over. If all condition expressions evaluate to #false, DrRacket returns an error.

(define (sign x)
  (cond
    [(> x 0) 1]
    [(= x 0) 0]
    [(< x 0) -1]))

Constants

Code refactoring is a process of restructuring existing computer code without changing its external behavior. Constants help us change the behavior without modifying much code. Constant definition in BSL is the following:

(define Name Expression)

For example,

(define WIDTH 100)

The order of definition matters for constants and does not matter for functions.

Programmers create programs for other people to read them in the future. Good programming is about solving problems systematically and conveying the systems within the code.