Comments Top Combining LiteralsLiterals Contents

Literals

Scam works by figuring out the meaning or value of some code. This is true for the tiniest pieces of code to the largest programs. The process of finding out the meaning of code is known as evaluation.

The things whose values are the things themselves are known as literals. The literals of Scam can be categorized by the following types: integers, real numbers, strings, Booleans, symbols, and lists.

Scam (or more correctly, the Scam interpreter) responds to literals by echoing back the literal itself. Here are examples of each of the types:

    (inspect 3)
    -> 3 is 3
     
    (inspect -4.9)
    -> -4.900000 is -4.900000
     
    (inspect "hello")
    -> hello is hello
     
    (inspect #t)
    -> #t is #t

    (inspect (list 3 -4.9 "hello"))
    -> (list 3 -4.9 "hello") is (3, -4.9, "hello")

Let's examine the five types in more detail.

Integers

Integers are numbers without any fractional parts. Examples of integers are:

    (inspect 3)
    -> 3 is 3
    
    (inspect -5)
    -> -5 is -5
    
    (inspect 0)
    -> 0 is 0

Integers must begin with a digit or a minus sign. The initial minus sign must immediately be followed by a digit.

Real Numbers

Reals are numbers that do have a fractional part (even if that fractional part is zero!). Examples of real numbers are:

    (inspect 3.2)
    -> 3.200000 is 3.200000
    
    (inspect 4.0)
    -> 4.000000 is 4.000000
       
    (inspect 5.)
    -> 5.000000 is 5.000000
       
    (inspect 0.3)
    -> 0.300000 is 0.300000
       
    (inspect .3)
    -> 0.300000 is 0.300000
    
    (inspect 3.0e-4)
    -> 0.000300 is 0.000300
    
    (inspect 3e4)
    -> 30000.000000 is 30000.000000
    
    (inspect .000000987654321)
    -> 0.000001 is 0.000001

Real numbers must start with a digit or a minus sign or a decimal point. An initial minus sign must immediately be followed by a digit or a decimal point. An initial decimal point must immediately be followed by a digit. Scam accepts real numbers in scientific notation. For example, 3.0 * 10-11 would be entered as 3.0e-11. The `e' stands for exponent and the 10 is understood, so e-11 means multiply whatever precedes the e by 10-11.

The Scam interpreter can hold huge numbers, limited by only the amount of memory available to the interpreter, but holds only 15 digits after the decimal point:

    (inspect 1.2345678987654329)
    -> 1.234568 is 1.234568

Note that Scam rounds up or rounds down, as necessary.

Numbers greater than 106 and less than 10-6 are displayed in scientific notation.

Strings

Strings are sequences of characters delineated by double quotation marks:

    (println "hello, world!")
    -> hello, world!
    
    (println "x\nx")
    -> x
       x
    
    (println "\"z\"")
    -> "z" 

Characters in a string can be escaped (or quoted) with the backslash character, which changes the meaning of some characters. For example, the character n, in a string refers to the letter n while the character sequence \n refers to the newline character. A backslash also changes the meaning of the letter t, converting it into a tab character. You can also quote single and double quotes with backslashes. When other characters are escaped, it is assumed the backslash is a character of the string and it is escaped (with a backslash) in the result:

    (println "\z")
    -> z

Note that Scam, when asked the value of strings that contain newline and tab characters, displays them as escaped characters. When newline and tab characters in a string are printed in a program, however, they are displayed as actual newline and tab characters, respectively. As already noted, double and single quotes can be embedded in a string by quoting them with backslashes. A string with no characters between the double quotes is known as an empty string.

Unlike some languages, there is no character type in Scam. A single character a, for example, is entered as the string "a".

Strings are treated like arrays in terms of accessing the individual `characters' of a string. You can read more about arrays and strings in a subsequent chapter.

Symbols

A symbol is a set of characters, much like a string. Like strings, symbols evaluate to themselves. Unlike strings, symbols are not formed using a beginning quotation mark and an ending quotation mark. They are also limited in the characters that compose them. For example, a symbol cannot contain a space character while a string can. A symbol is introduced with a single quotation mark:

    (print 'a)
    -> a

    (print 'hello)
    -> hello

We we learn more about symbols and their relationship to entities called variables in a later chapter.

True, False, and nil

There are two special literals, #t and #f. These literals are known as the Boolean values; #t is true and #f is false. Boolean values are used to guide the flow of a program. The term Boolean is derived from the last name of George Boole, who, in his 1854 paper An Investigation of the Laws of Thought, on which are founded the Mathematical Theories of Logic and Probabilities, laid one of the cornerstones of the modern digital computer. The so-called Boolean logic or Boolean algebra is concerned with the rules of combining truth values (i.e., true or false). As we will see, knowledge of such rules will be important for making Scam programs behave properly. In particular, Boolean expressions will be used to control conditionals and loops.

Another special literal is nil. This literal is used to indicate an empty list or an empty string; it also is used to indicate something that has not yet been created. More on nil when we cover lists and objects.

Lists

Lists are just collections of entities. The simplest list is the empty list:

    (inspect ())
    -> nil is nil

Since the empty list looks kind of strange, Scam uses the symbol nil to represent an empty list.

One creates non-empty list by using the built-in list function. Here, we make a list containing the numbers 10, 100, and 1000:

    (list 10 100 1000)
    -> (10 100 1000)

Lists can contain values besides numbers:

    (list 'a "help me" length)
    -> (a "help me" <builtin length(item)>)

The first value is a symbol, the second a string, and the third item is a function. The built-in length function is used to tell us how many items are in a list:

    (length (list 'a "help me" length))
    -> 3

As expected, the length function tells us that the list ('a "help me" length) has three items in it.

Lists can even contain lists!

    (list 0 (list 3 2 1) 4)
    -> (0 (3 2 1) 4)

A list is something known as a data structure; data structures are extremely important in writing sophisticated programs.

We will see more of lists in a later chapter.

lusth@cs.ua.edu


Comments Top Combining LiteralsLiterals Contents