Textbook Notes (290,000)
CA (170,000)
UBC (3,000)
CPSC (20)
Chapter 1

# CPSC 110 Chapter Notes - Chapter 1: Semicolon, Substring, Pineapple

Department
Computer Science
Course Code
CPSC 110
Professor
Gregor Kiczales
Chapter
1

This preview shows pages 1-3. to view the full 9 pages of the document.
WebX notes - Module 1a - Beginning Student Module
Expressions
Expression → ( + 5 4) will result in
Value → 9
Expression → (+2 (* 3 4) )
Value → 14
Calculation explained:
(* 3 4) is carried out first → 12
12 is added to 2 → 14
Comments : When there is a semicolon (;) at the beginning of the line it tells the program that
these lines should be ignored.
Example :
;( * 3 4 ) will not be carried out by the program
Primitive operations :
*, / , +, -
sqr - square
(sqr 4 ) → 16
sqrt- square root
(sqrt 16) → 4
Irrational numbers :
Irrational numbers are represented by ‘#i’ and the approximated value letting us know that the
value displayed is not the exact value but an extremely close approximation.
Example:
(sqrt 5) →#i2.23606797749979
find more resources at oneclass.com
find more resources at oneclass.com

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

WebX notes - Module 1a - Beginning Student Module
Evaluation
( + 2 3 )
NB. Expressions beginning with a primitive function (such as + ) are called primitive calls or calls
to primitives.
‘+’ is considered the operator while 2 and 3 are the operands.
How racket evaluates expressions :
Given the expression
(+ 3 ( * 5 7 ) (- (+ 2 1 ) 3 ) )
NB: The main primitive function and main operands were placed in different colours.
Step 1: Reduce all operands to values (going from left to right).
First operand 3 is already a value so it remains the same
Second operand is an expression (* 5 7), this must be reduced to a value
(* 5 7 ) → 35
Third operand is an expression (- (+ 2 1) 3)
(- (+ 2 1) 3) ;The first operand is also an expression
(+ 2 1 ) → 3
(- 3 3) → 0
Expression with reduced operators : ( + 3 35 0) →. 38
find more resources at oneclass.com
find more resources at oneclass.com

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

WebX notes - Module 1a - Beginning Student Module
Strings and Images
Strings
“Ashley” → string
“465” is NOT EQUAL TO 456
;; “456” → string
;; 456 → number
String functions :
string-append : joins two or more strings
NB: Unless a space is added in the strings to be joined, the strings will be joined without a
space.
Example :
(string-append “pine” “apple” ) → pineapple
(string-append “I” “ “ “like” “ “ “this” “ “ “class”) → I like this class
Here the spaces are joined as separate strings. This will be useful when joining
strings from different program outputs.
(string-append “Hi “ “there!”) → Hi there!
The space is apart of the string. This is not suitable for all problems.
string-length: tells us how long a string is. That is, how many characters are in a string.
NB: characters are also counted.
Example:
(string-length “pine”) → 4
(string-length “Hi there!”) → 9
substring: allows us to take out a subset (some characters) from the given word.
Example:
(substring “Caribbean” 5 9) → bean
Format ( substring String startOfRange endOfRange)
substring utilises zero-based indexing
The count begins at 0 not 1
‘C’ is therefore in the 0 position
‘a’ is 1
‘r’ is 2 …
endOfRange
Last position is substring + 1
Images
find more resources at oneclass.com
find more resources at oneclass.com