Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

Scheme Programming/Scheme Datatypes

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Scheme Programming A taste of Scheme Scheme Datatypes Simple Expressions Scheme has several datatypes, there are atomic datatypes and compound datatypes, the significant difference between both is that while atomic datatypes are unique and immutable, compound datatypes are mutable, this means that we can modify them. This also means that there can be for instance two different strings "Hello, World!" even though they have the same characters, while every character #\H is the same object. Atomic types Booleans Booleans are either #t (true) or #f (false). Booleans are self-evaluating. Numbers Numbers are self-evaluating, and are entered as expected. Scheme also supports complex numbers as for instance 0+4i. Just typing 4i is interpreted as a symbol in Scheme, beware. Characters Characters are self-evaluating and entered as such #\a. Symbols Symbols are sequences of characters, different from strings because they are not self-evaluating, and they can't contain white-space either. They are just input by typing them really, for instance (add one two) is a list in Scheme that contains three symbols. Compound Pairs and Lists Pairs and lists are the most important compound types in Scheme. We already said that Scheme source code itself made up of lists, thus lists are obviously not self-evaluating. Typing a list which cannot be sensibly evaluated (such as one that starts with a number) will produce an error. There is one list though which is atomic; it's called the empty list, or nil or null, depending to whom you talk. It's simply the list of no elements at all. It's represented in source code by (). All other lists are in effect pairs in disguise, a pair of a and b is written like (a . b), this is different from a list of a and b. Pairs are also not self-evaluating and often produce an error. What a list is, is just a pair, whose first element (also called its car) is the first element of the list, and whose second element (also called its cdr) is a list containing all the other elements. Thus the list: (print "Hello, World!") Is just a shorthand for: (print . ("Hello, World!" . ())) Vectors The problem with lists is that list-ref and list-set! are slow operations if the list is very large. For a data structure that allows fast reading and writing, Scheme provides vectors. Here is an example of a vector of numbers: #(100 90 80 70) Strings Strings are sequences of characters. They are contained by double-quotes. "This is a string."