## Scheme Documentation

#### We use comments to leave notes about the code to the reader. Comments are not actually run by Scheme, they are just there to help us read the code.

#### We can make single line comments with ; .

; Use single line comments to clarify parts of code.

Example:

; This program adds 1 and 2 and stores the result in x
(define x (+ 1 2)); done

### Basic Data Types

##### Data can be a string, number, a boolean, or something else.

######
A string is a text element.
A number can be number, integer, rational, real, and/or complex.
A boolean is an element that returns either #t (True) or #f (False).
There are additional variable types.

The example code below shows how we can check the type of data

(string? "hi") ; Returns #t (True) indicating "hi" is a string
(integer? 2) ; Returns #t (True) indicating 2 is an integer
(boolean? #f) ; Returns #t (True) indicating #f is a boolean

### Variables

#### Variables have an associated 'type' based on their characteristics.

#### We use variables to store values that can be used to control commands in our code. We can also alter these values throughout the code.

; Make a variable to store text
(define name "Zach")
; Create variables that are numbers
(define num_one 3) ; num_one now have the value 3
(define num_two 4) ; num_two now has the value 4
(define sum (+ num_one num_2)) ; sum now has the value 7
(set! num_one 20) ; num_one now has the value 20

### Operators

#### We use mathematical, comparison, and logical operators in our codes to compare and alter values and make decisions.

### Comparison Operators

#### Use comparison operators to compare elements in order to make decisions in your code. Comparison operators return booleans (#t/#f).

; For numbers use below
(> x y) ; is x greater than y
(>= x y) ; is x greater than or equal to y
(< x y) ; is x less than y
(<= x y) ; is x less than or equal to y
(= x y) ; is x equal to y
; For strings use below
(string>? x y) ; is x greater than y
(string>=? x y) ; is x greater than or equal to y
(string

### Logical Operators

#### Use logical operators to check multiple conditions at once or one condition out of multiple.

; And Operator
(and x y) ; Returns x and y
; Or Operator
(or x y) ; Returns x or y
; Not operator
(not x) ; Returns not x
; You can combine many booleans!
(and x (or y z))

### Mathematical Operators

#### Use mathematical operators to alter values.

+ Addition
- Subtraction
* Multiplication
/ Division
remainder Remainder of division
quotient Quotient of division

Examples

(+ a b)
(* a b)
; Division
(/ 5 2) ; Returns 5/2
(/ 5.0 2) ; Returns 2.5
(/ 6 2.0)) ; Returns 3.
; Quotient
(quotient 13 5) ; Returns 2
; Remainder
(remainder 13 5) ; Returns 3
; Absolute value
(abs -2) ; Returns 2
; Square root
(sqrt 9) ; Returns 3
; Raising to a power
(expt x y) ; Returns x^y
; Rounding, rounds to even when the number is halfway between two integers
(round 1.7) ; Returns 2
(round 3.1) ; Returns 3
(round 1.5) ; Returns 2
(round 2.5) ; Returns 2

### Random Numbers

; Random integer in [0, n)
(random n)

; Returns random number in [0, 10)
(random 10)

### Strings

#### Strings are pieces of text. We can gain much information about strings and alter them in many ways using various methods.

; We can take a substring of a string.
(substring string a b) ; returns the substring of string beginning at a and ending right before b

Example

(substring "example 1 3") ; returns ex

### Loops

#### Loops help us repeat commands which makes our code much shorter. Scheme doesnn't really have loops but we can do something similar

#### Scheme doesn't have traditional for or while loops, but when you want to repeat something, you can do something like below.

; This will print the numbers 1 to 10 each on it's own line
(let loop ((i 1))
(if (= i 10)
(begin (display i) (display "\n"))
(begin (display i) (display "\n")
(loop (+ i 1)))))

### If Statements

#### Use an if statement to instruct the computer to do something only when a condition is true. If the condition is false, the following command will be skipped.

(if (BOOLEAN_EXPRESSION) (do something) )

Example:

; x will only be doubled if x is positive
(if (> x 0) (set! x (* x 2)))

### If/Else Statements

#### We can tell the computer how to make decisions using if/else statements.

(if (BOOLEAN_EXPRESSION) (do something) (do something else) )
; if x is positive x is doubled, else x is set to 1
(if (> x 0) (set! x (* x 2)) (set! x 1))

### Cond

#### We can use cond similarly to linking if and else if statements. cond executes
the body of the first condition that is met. Only the last condition may represent else.

####
; this will show equal
(define x 7)
(cond ((> x 7) 'greater)
((< x 7) 'less)
('equal))