Table of Contents

3. Strings and Variables

Remember that our print commands were enclosed in parentheses (...)? Without parentheses, the function will not be called.

If you try running the print code without parentheses, it will still work, and misleadingly, the message will still appear, but this happens because "Hello, World!" was the final value of our code block.

Running any code block will return its final value.

If you look closely, running (print "Hello, World") will result in => #<no value>, meaning that print does not return a value. When the parentheses are left out, it will return the value "Hello, World!" instead. The value shown after the ;; => characters is the return value of the code block.

Try clicking on both code blocks below and observe the difference:

(print "Hello, World!")
print "Hello, World!"

It's worth noting that the semicolon ; character marks the beginning of a comment. The text following the semicolon (and including it) up to the end of the line is ignored by the interpreter. Comments are useful to describe what the code is doing, useful remarks, or even temporary observations.

;; Code that displays "Howdy!" in the console
(print "Howdy!")  ; this is also a comment

Strings

A string is a textual value, which may contain characters from alphabets of worldwide languages, numbers, and punctuation marks.

A string must be enclosed in double quotes: "this is a string". If a double quote character needs to be included, it must be written as \" (a backslash followed by the double quote character).

(print "Alice said, \"Good day!\"")

Writing your own function

Creating a function in Scheme looks like this:

(define (function-name parameter-1 parameter-2 more-parameters)
  function-body)

Let's improve our Hello, World! program so that it can be more specific. We'll call our function greet. In its body, it will call print:

(define (greet first-name)
  (print "Hello, " first-name))

In our function, first-name is a parameter. Try the following:

(greet "Alan")

When we call a function, the actual values passed are called arguments. The parameter called first-name in the function definition will be bound to the value of the argument.

In Scheme, you can pass anything as an argument to a function. For greet, only textual values (strings) will make sense, but you can also pass numbers and even the greet function itself:

(greet 99)
(greet greet)

The meaning of #<Closure>, which shows up as the result of (greet greet) will be explained later.

For arithmetic functions, strings cannot be used. You will see an error message if you try to add strings:

(+ "1" "2")

Lists

We've been using lists the whole time, but without specifically calling them lists.

A list in Scheme is enclosed in parentheses (...) and may contain any number of elements. An empty list, (), is also a valid list.

Variables

What happens if we forget the double quotes around the argument? You will get an error.

(greet bob)

When Scheme encounters a sequence of characters such as cats, it considers it a symbol. (Remember, strings need to be enclosed in double quotes, so cats is not the same as "cats").

If the symbol occurs as the first element of a list, Scheme will treat the symbol as a function name and try calling it with the remaining elements of the list as arguments.

If the symbol occurs after the first element, it will try to evaluate that symbol. To evaluate something is to try to obtain its value.

Remember (greet greet)? It turns out that the value of greet, when you try to display it in the console, is #<Closure>.

A variable is defined by binding a symbol to a value. It looks very similar to a function definition:

(define bob "Bob Schemer")

Click on the box above to define bob, then click again on (greet bob) further up.

Table of Contents