Programming Languages CS 4700
Utah State University
Popular in Course
Popular in ComputerScienence
This 4 page Class Notes was uploaded by Alek Haley on Wednesday October 28, 2015. The Class Notes belongs to CS 4700 at Utah State University taught by Curtis Dyreson in Fall. Since its upload, it has received 12 views. For similar materials see /class/230443/cs-4700-utah-state-university in ComputerScienence at Utah State University.
Reviews for Programming Languages
Report this Material
What is Karma?
Karma is the currency of StudySoup.
Date Created: 10/28/15
CS 4700 Handouts Scheme Quick Reference Sheet 1 Types Scheme has literals of several primitive types The literals are selfevaluating expressions Which means that they evaluate to the value that they represent 3 integer literal 3 integer literal 43 floating point literal 465e2 floating point literal scientific notation t boolean literal true f boolean literal false quothelloquot string literal quothellonquot string literal note the newline quotI said quothelloquotquot other backslash characters similar to C Scheme has one composite type a list Lists and strings are allocated in the heap and automatically garbage collected just like in Perl So don7t worry too much about the cost of creating new lists use them freely they are the primary data structure in Scheme Note the quote symbol in front of each list it is actually a function call see below i 3 4 a two element list 4 3 3 lists can be nested empty list a one element list the element is the empty list quothiquot 43 quotjoequot lists can be heterogeneous Note that the list elements are not separated by commas Type checking in Scheme is dynamic 2 Builtin Functions A function is a list that is evaluated In general a list Will be evaluated only in some contexts are lists not evaluated such as When it is preceded by a quoteh Each function is in prefix notationl functionname operand1 operand2 operandN Each function returns a value and takes zero or more operandsl 21 Arithmetic Functions max 34 5 7 38 6 gt 38 maximum of the operands min 3 5 5 330 4 24 gt 24 minimum of the operands 3 4 gt 7 addition 4 4 gt 16 multiplication 3 4 gt 1 subtraction 3 gt magnitude magnitude 3 7 7 quotient 35 7 quotient 35 7 quotient 3 quotient modulo 13 modulo 13 remainder remainder gcd 32 4 5 7 35 7 4 4 13 4 13 4 gt 4 gt 7 gt 7 gt 5 gt gt gt 1 gt 3 5 5 gt 1 gt 1 22 Comparison Functions 2 3 gt f eqv quotabsquot quotabsquot gt t eqv a a gt t lt 2 3 gt gt 2 3 gt t f gt 2 3 gt f or f t gt t not f gt 1 and f t gt f integer 2 gt t real 3 gt f string 3 gt f zero 3 0 gt f positive 3 4 5 gt t negative 3 4 5 gt f String quot2quot quot3quot gt f 2 3 List Functions unary subtraction absolute value absolute value div remainder remainder greatest common divisor are all the integer operands equal are all the operands equal are all the operands equal less than greater than greater than or equal to or negation all the operands integers reals strings zero positive negative two strings equal These are by far the most important functions that we Will be using quote a b a b gt a b car a b car car cdr cdr car gt a gt a b gt ERROR car Wrong type in arg1 a b gt a a b gt b gt ERROR car Wrong type in arg1 a a b gt a b quote suspends evaluation of the list quote suspends evaluation of the list get head of list contents of address reg expects a list get head of list could be a list get rest of list contents of address reg expects a list get rest of list put element on head of list build a list build a list from the operands from the operands cons 3 gt 3 cons a b c d gt a b c d cons quotaquot b c gt quotaquot b c list a 7 c gt a 7 c list gt length a b c gt 3 length a b c d e length gt o gt 3 what is the what is the what is the length of a list length of a list length of a list append x y gt X y append a b c d gt a b c d append a b C gt a b c append one list to another reverse a b c gt c b a reverse a list reverse a b c d e f gt e f d b C a null gt t is the list empty null 2 gt f is the list empty append one list to another append one list to another reverse a list 2 4 Miscellaneous Functions While Scheme has several loop functions only the following two functions are often useful if 2 3 t f gt f an if function returns result of evaluating one of the branches if 2 3 2 3 2 3 gt 1 if gt 3 2 if functions can be nested if 2 3 equal gt lt display quothelloquot prints hello display quothellonquot prints hello followed by a newline display 1 3 prints 1 3 exit leave the scheme interpreter 3 Binding Names to Functions or Values ln Scheme each name is bound to a meaning The name is a sequence of letters but could include numbers or special characters such as a question mark or hyphen The meaning is usually either a function or literal The binding between a name and a function is dynamic so the meaning of a name can change during the lifetime of a program The de ne function binds a name to a meaning There are two avours of de ne The rst binds a name to a value define x 23 bind name x to literal 23 x dereference x x 4 gt 27 X gt ERROR define x append x x gt define y quothelloquot bind name y to a string literal define y 4 5 bind name y to the result of evaluating the function 4 5 ie to 9 define y 4 5 bind name y to the list 4 5 define y 4 x bind name y to the result of 4 x will result in an ERROR since x means names are dereferenced whereever they appear 23 is not a functionname bind name x to the empty list The second avour of de ne binds a name to a function and is how we will de ne new functions ln this form of de ne enclose the name being de ned in brackets Add parameter names as necessary within the brackets The scope of each parameter is local to the function de nition define x 23 x gt 23 X gt CLUSURE 23gt define x 4 5 bind functionname x to evaluate to 23 call the function named x dereference x bind functionname x to the function 4 5 x gt 9 call the function named x define add X y x y define a binary add function add 4 5 9 call the add function add 4 to 5 results in 9 define add x y z x y z define a ternary add function add 4 5 7 gt 16 call the add function Note that both operands could be lists but neither is evaluated So this is another context like Within a quote function in Which lists are not evaluated as functions The second avour of bind is actually just syntactic sugar for binding the name to an anonymous function An anonymous function is a function that does not have a name The lambda function can be used to create an anonymous functioni lambda x x 1 ltCLUSURE x x 1gt create a function that adds 1 to its one operand 1ambda x x 1 4 5 use the anonymous function define succ lambda x x 1 bind the name succ succ 4 gt 5 dereference succ to get function define succ x x 1 bind the name succ succ 4 5 dereference succ to get function So in general the second avour of bind is just syntactic sugar for the first avour of bind Always remember that names are usually dereferenced Whenever they are used An exception is When the name is bound in a de ne function 31 Important Function Details Here are some important things to remember about functions 0 In general functions have eager evaluation Which means that the parameters are evaluated prior to invoking the function call however there are exceptions For instance the and cond functions have lazy evaluation Which means that the function is called and the operands are evaluated as needed 0 Functions are first class values They can be passed as parameters and returned from functions 0 Parameter passing is by positional correspondence If the Wrong number of arguments are passed an error Will be generated