Extra parentheses, while okay in mathematics, affect expressions in Scheme
Operations are written before the associated arguments
Operations need two arguments
Substitutions are done left to right
Functions in Scheme
(define (f x y) (+ (* x x) y))
The word define is used to bind a name to an expression
f is the name of the function
x and y are the parameters
Values that are passed as x and y are the arguments of the function
(+ (* x x) y) is the body expression, and tells us what the function does
x and y have no meaning in any other part of the program
(define b “constant”)
b is a constant
If b is used in any body expressions, “constant” will be used
Constants can make programs easier to understand
, Design Recipe
Contract
Describes the arguments that your function takes as well as the type that your function will
produce
Some possibilities are Int, Nat, Symbol, String, Posn, etc.
For the output of your function, None is also a possibility
Examples:
Symbol -> None
String Int -> Boolean
Posn Symbol Int[>= 7] -> Posn
Purpose
Explains what your function does using the names of your parameters
Purpose comes from the question
Example:
The unit of speed most often used in physics is meters per second (m/s). A common
Imperial unit of speed is miles per hour (mph). Write a function mph->m/s which
consumes a speed in the units of mph and produces the same speed in units of m/s.
Purpose: converts the given speed, s, from mph to m/s
Examples
Used to make sure you understand how the function is supposed to work
Written below the purpose and above the function
Don’t necessarily test all cases; mostly try and get a general idea about what your function
should do
Example:
Write a function final-cs135-grade that consumes four numbers:
(a) the first midterm grade (10%),
(b) the second midterm grade (20%),
(c) the final exam grade (45%), and
(d) the assignments grade (20%).
This function should produce the final grade in the course. You may need to review the mark
allocation in the course. You can assume that all input marks are percentages and are given
as integers between 0 and 100, inclusive. Also, assume a grade of 100 for participation (5%).
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller smartzone. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $12.49. You're not tied to anything after your purchase.