Structs, Vectors, and Classes in DSSL2
Welcome to DSSL2
• Code organized in statements, functions, and classes Similar to C++
Copyright By PowCoder代写 加微信 powcoder
• Variables and data are mutable (= assignment) Similar to C++
• No explicit pointers (arrows) or memory management Similar to the 111 teaching languages
• No explicit types
Similar to the 111 teaching languages
• (These also apply to Python) 3
DSSL2 statements
let x = 5 # variable definitions use ‘let’ — # this minor difference from Python
# helps avoid ambiguity and thus bugs
println(8 * x) # an expression can also be a statement
if condition: # indentation matters! just like Python do_some_stuff()
do_other_stuff(x, y, z)
DSSL2 functions
# hypotenuse: Number Number -> Number # Finds the length of the hypotenuse. def hypotenuse(a, b):
(a * a + b * b).sqrt()
# fact: Natural -> Natural
# Computes the factorial of `n`. def fact(n):
if n == 0: 1
n * fact(n – 1)
DSSL2 programs
Every DSSL2 program starts with a #lang line, followed by any number of statements:
#lang dssl2
let CM_PER_INCH = 2.54
# Converts centimeters to inches.
def cm_to_inch(cm):
cm / CM_PER_INCH
# Converts inches to centimeters.
def inch_to_cm(inches):
inches * CM_PER_INCH
test ’round trip’:
assert_eq inch_to_cm(cm_to_inch(17)), 17
assert_eq cm_to_inch(inch_to_cm(17)), 17
• One of the key building blocks of data structures: 0123456789
• Literal vector notation:
[ 0, 1, 1, 2, 4, 7, 13, 24, 44, 82 ]
Vector operations
let v = [ 0, 1, 1, 2, 4, 7, 13, 24, 44, 82 ]
Vector operations
let v = [ 0, 1, 1, 2, 4, 7, 13, 24, 44, 82 ]
# you can give names to test cases
# and get nicer error messages than bare assumptions test ‘vector basics’:
assert_eq v[3], 2
assert_eq v[6], 13
test ‘vector set’:
assert_eq v[6], 23
What if I want a really big vector?
• Vector comprehensions allow you to create a vector using a “description” rather than literal elements
[ 0; 1000000 ]
• Creates a vector with 1000000 elements, all 0s Much nicer than typing the whole thing!
• Supports more complicated descriptions too, see the docs
Example: average
# average: Vector
# Averages the elements of a non-empty vector. def average(vec):
sum(vec) / vec.len()
# sum: Vector
# Sums the elements of a non-empty vector. def sum(vec):
let result = 0
# for-each loop, like in C++
# `v` becomes each element of the vector, in turn for v in vec:
result = result + v
return result
• Another key building block xxx
struct posn:
# different ways to construct
posn { x: 12, y: -5 }
posn { x: 0, y: 0 }
posn(3, 4)
Working with structs
struct posn:
let p = posn(3, 4)
assert posn?(p)
assert_eq p.x, 3
assert_eq p.y, 4
assert_eq p.x, 6
assert_eq p.y, 4
# asserts that the result is true
# uses `.` notation, like C++
Structs and vectors
id id id id name name name name position position position position
struct employee:
let id; let name; let position
let employees = [ employee( 928, “Alice”, 4),
employee(1089, “Bob”, 6),
employee( 14, “Carol”, 6),
employee( 546, “Dave”, 6) ] 16
Working with structs and vectors
struct employee:
let id; let name; let position
let employees = [
employee( 928, “Alice”, 4),
employee(1089, “Bob”, 6),
employee( 14, “Carol”, 6),
employee( 546, “Dave”, 6),
QUIZ. Suppose we want to find out Carol’s position:
Working with structs and vectors
struct employee:
let id; let name; let position
let employees = [
employee( 928, “Alice”, 4),
employee(1089, “Bob”, 6),
employee( 14, “Carol”, 6),
employee( 546, “Dave”, 6),
QUIZ. Suppose we want to find out Carol’s position:
employees[2].position
QUIZ: How can we give her a promotion (from 6 to 5)?
employees[2].position = 5
Generalizing
# promote_employee : Vector
# Decrements the position of the `index`th employee. def promote_employee(employees, index):
let emp = employees[index]
# `emp` is not a copy! so we modify the original emp.position = emp.position – 1
• Structs and vectors are enough to represent any data • But data structures = representation + operations
Classes allow us to combine the two • Classes ≈ structs with methods
A code organization machanism to group data and operations together
Our first class example
class Posn:
let x # fields: initialized by let y # the constructor
def __init__(self, x, y): # constructor: method
self.x = x
self.y = y
def get_x(self): self.x
def get_y(self): self.y
# with a special name
# fields are private
# `return` is optional
# `self` = receiver
def distance(self, other): # some other method
# need to use getter for `other`
let dx = self.x – other.get_x()
let dy = self.y – other.get_y()
(dx * dx + dy * dy).sqrt()
Using the Posn class
let p = Posn(3, 4)
assert_eq p.get_x(), 3
assert_eq p.get_y(), 4
assert_error p.x
let q = Posn(0, 0);
assert_eq p.distance(q), 5
# fields are private
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com