SlideShare a Scribd company logo
1 of 20
CSE240 – Introduction to
Programming Languages
Lecture 17:
Programming with LISP| Control Structures
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2
Everything is a List
• A function ID (symbol) is a series of characters other than whitespace, parentheses ( ), pound
( # ), quote ( ' ), double-quote ( " ), period ( . ), or back quote ( ` ).
• A function ID (symbol) may not take the form of numbers.
• It's very common for symbols to have hyphens ( - ) or asterisks ( * ) in them.
• There are NOT operators! only functions
• Symbols (function ID) are case-INSENSITIVE.
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3
Atomic Expressions
• -3
• 2.43
• 123342303923412323411323234012923492341231230234923410239234412
• #C(3.2 2) ; the complex number 3.2 + 2i
• 2/3 ; the fraction 2/3. It is NOT divide 2 by 3
• -3.2e25 ; the number -3.2 x 10^25
• #g ; characters starts with #
• #{
• # ; the character ''
• #tab

• #newline
• 
#space

• #backspace

• #escape
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4
Atomic Expressions
• "Hello, World!"
• "It's a glorious day."
• "He said "No Way!" and then he left."
• "I think I need a backslash here:  Ah, that was better."
• t ; true
• nil ; false
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5
Everything is a List
• (+ 27/32 32/57)
2563/1824
• (* 2.342 3.2e4)
74944.0
• (* 2.342 9.212 -9.23 3/4) ; You can mix number types
-149.34949
• (/ 3 5) ; The return type stays as general as possible
3/5
• (/ 3.0 5) ;Here LISP had no choice: convert to a float
0.6
• (1+ 3)
4
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 6
Everything is a List
• (string-upcase "How about that!")
"HOW ABOUT THAT!"
• (reverse "Four score and seven years ago")
"oga sraey neves dna erocs ruoF"
• (length "Four score and seven years ago")
30
• (sqrt 2)
1.4142135
• (sqrt -1.0)
#C(0 1.0)
• (SqRt -1.0) ;LISP symbols are case-insensitive
#C(0 1.0)
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 7
Function Arguments
• Some functions have NOT a fixed number of arguments:
(+ 100 231 201 921 221 231 -23 12 -34 134)
• Some functions have a fixed number of arguments and optional arguments:
(subseq "Four score and seven years ago" 9)
"e and seven years ago”
(subseq "Four score and seven years ago" 9 23)
"e and seven ye"
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8
Predicates (boolean functions)
• LISP has a special name for functions which return "true" (usually t) or
"false" (nil). These functions are called predicates.
• (= 4 3) ; is 4 == 3 ?
NIL
• (< 3 9) ; is 3 < 9 ?
T
• (numberp "hello") ; is "foo" a number?
NIL
• (oddp 9) ; is 9 an odd number?
T
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9
Errors
• Errors stop the program execution
• (/ 1 0)
*** - division by zero
• (blah-blah-blah 1 0 "foo")
*** - EVAL: the function BLAH-BLAH-BLAH is undefined
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 10
Recursion
• When a list contains another list among its expressions, the evaluation
procedure is recursive.
• (+ 33 (* 2.3 4) 9)
51.2
• (+ (length "Hello World") 44)
55
• (* (+ 3 2.3) (/ 3 (- 9 4))) ;in C++: (3+2.3) * (3 / (9-4))
3.1800003
• (log (log (log 234231232234234123)))
1.3052895
• (+ (* (sin 0.3)
(sin 0.3)) ; expressions may use multiple lines
(* (cos 0.3)
(cos 0.3))) ; sin(0.3)^2 + cos(0.3)^2
1.0000001 ; = 1. Rounding inaccuracy
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 11
Recursion
• (and (< 3 (* 2 5))
(not (>= 2 6))) ; (3 < 2 * 5) && !(2 >= 6)
T
• (print (+ 2 3 4 1))
10
10
• (+ (* 2 3) (/ 3 2) 9)
33/2
Control Structures
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 13
Control Structures
• There are some evaluable lists which are not functions.
• These lists are known as macros or special forms.
• The control structure if is a special form.
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 14
Control Structures
• (if (<= 3 2) (* 3 9) (+ 4 2 3))
; if (3<=2) {return 3*9;} else {return 4+2+3;}
9
• (if (> 2 3) 9)
; if (2>3) {return 9;} else {return nil}
NIL
• (if (= 2 2) (if (> 3 2) 4 6) 9)
4
• (+ 4 (if (= 2 2) (* 9 2) 7) )
22
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 15
Control Structures
• if only allows one test-expression, one then-expression, and one optional-else-expression.
• To do more things in the then-expression we need to make a block. Blocks are made with the
special form progn, which takes the form:
(progn expr1 expr2 expr3 ...)
• (if (> 3 2)
(progn (print "hello")
(print "yo")
(print "whassup?")
(+ 2 2))
(+ 1 2 3))
"hello"
"yo"
"whatsup?"
4
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 16
Loop (dotimes)
dotimes is an iterator with this format
(dotimes (var high-val optional-return-val)
expr1
expr2
…)
• (dotimes (x 4) (print "hello"))
"hello"
"hello"
"hello"
"hello"
NIL
• (setf bag 2)
2
• (dotimes (x 3) (setf bag (* bag bag)))
NIL
• (print bag)
256
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 17
Global Variables
• Variables are set with the macro setf.
• (setf x (* 3 2))
6
• x
6
• (setf y (+ x 3))
9
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 18
Local Variables
• let declares local variables with each declaration. Then it evaluates
the expressions in order (as a block).
• let returns the value of the last expression.
• (setf x 4)
(let ((x 3)) ; x declared local
(print x)
(setf x 9) ; change the value of x
(print x)
(+ x x) ;this is the returned value
)
3
9
18
• x ;outside the let, we're back to global again
4
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 19
Nested Variable Scope
• (let ((x 3) (y (+ 4 9))) (* x y))
39
• (let ((x 3))
(print x)
(let (x)
(print x)
(let ((x "hello"))
(print x))
(print x))
(print x)
(print ”end"))
3
NIL
"hello"
NIL
3
"end"
"end"
CSE240 – Introduction to Programming Languages
Javier Gonzalez-Sanchez
javiergs@asu.edu
Fall 2017
Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.

More Related Content

What's hot

Regular Expressions: QA Challenge Accepted Conf (March 2015)
Regular Expressions: QA Challenge Accepted Conf (March 2015)Regular Expressions: QA Challenge Accepted Conf (March 2015)
Regular Expressions: QA Challenge Accepted Conf (March 2015)Svetlin Nakov
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programmingLukasz Dynowski
 
正規表現のいろは
正規表現のいろは正規表現のいろは
正規表現のいろはAyumu Hanba
 
White-Box Testing on Methods
White-Box Testing on MethodsWhite-Box Testing on Methods
White-Box Testing on MethodsMinhas Kamal
 

What's hot (6)

Big datacourse
Big datacourseBig datacourse
Big datacourse
 
Regular Expressions: QA Challenge Accepted Conf (March 2015)
Regular Expressions: QA Challenge Accepted Conf (March 2015)Regular Expressions: QA Challenge Accepted Conf (March 2015)
Regular Expressions: QA Challenge Accepted Conf (March 2015)
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programming
 
正規表現のいろは
正規表現のいろは正規表現のいろは
正規表現のいろは
 
White-Box Testing on Methods
White-Box Testing on MethodsWhite-Box Testing on Methods
White-Box Testing on Methods
 
Scala @ TomTom
Scala @ TomTomScala @ TomTom
Scala @ TomTom
 

Similar to 201801 CSE240 Lecture 17

Basic arithmetic, instruction execution and program
Basic arithmetic, instruction execution and programBasic arithmetic, instruction execution and program
Basic arithmetic, instruction execution and programJyotiprakashMishra18
 
03_NumberSystems.pdf
03_NumberSystems.pdf03_NumberSystems.pdf
03_NumberSystems.pdfvijayapraba1
 
Introduction to R
Introduction to RIntroduction to R
Introduction to RStacy Irwin
 
Thinking Functionally In Ruby
Thinking Functionally In RubyThinking Functionally In Ruby
Thinking Functionally In RubyRoss Lawley
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7decoupled
 
AutoDesk
AutoDeskAutoDesk
AutoDeskSE3D
 
Extending Spark SQL API with Easier to Use Array Types Operations with Marek ...
Extending Spark SQL API with Easier to Use Array Types Operations with Marek ...Extending Spark SQL API with Easier to Use Array Types Operations with Marek ...
Extending Spark SQL API with Easier to Use Array Types Operations with Marek ...Databricks
 
楽々Scalaプログラミング
楽々Scalaプログラミング楽々Scalaプログラミング
楽々ScalaプログラミングTomoharu ASAMI
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Platonov Sergey
 
論文紹介 Hyperkernel: Push-Button Verification of an OS Kernel (SOSP’17)
論文紹介 Hyperkernel: Push-Button Verification of an OS Kernel (SOSP’17)論文紹介 Hyperkernel: Push-Button Verification of an OS Kernel (SOSP’17)
論文紹介 Hyperkernel: Push-Button Verification of an OS Kernel (SOSP’17)mmisono
 

Similar to 201801 CSE240 Lecture 17 (20)

201801 CSE240 Lecture 16
201801 CSE240 Lecture 16201801 CSE240 Lecture 16
201801 CSE240 Lecture 16
 
201801 CSE240 Lecture 18
201801 CSE240 Lecture 18201801 CSE240 Lecture 18
201801 CSE240 Lecture 18
 
201801 CSE240 Lecture 20
201801 CSE240 Lecture 20201801 CSE240 Lecture 20
201801 CSE240 Lecture 20
 
Scheme language
Scheme languageScheme language
Scheme language
 
Basic arithmetic, instruction execution and program
Basic arithmetic, instruction execution and programBasic arithmetic, instruction execution and program
Basic arithmetic, instruction execution and program
 
ECMA5 and ES6 Promises
ECMA5 and ES6 PromisesECMA5 and ES6 Promises
ECMA5 and ES6 Promises
 
03_NumberSystems.pdf
03_NumberSystems.pdf03_NumberSystems.pdf
03_NumberSystems.pdf
 
Python lecture 05
Python lecture 05Python lecture 05
Python lecture 05
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Thinking Functionally In Ruby
Thinking Functionally In RubyThinking Functionally In Ruby
Thinking Functionally In Ruby
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
AutoDesk
AutoDeskAutoDesk
AutoDesk
 
Extending Spark SQL API with Easier to Use Array Types Operations with Marek ...
Extending Spark SQL API with Easier to Use Array Types Operations with Marek ...Extending Spark SQL API with Easier to Use Array Types Operations with Marek ...
Extending Spark SQL API with Easier to Use Array Types Operations with Marek ...
 
楽々Scalaプログラミング
楽々Scalaプログラミング楽々Scalaプログラミング
楽々Scalaプログラミング
 
Full Stack Clojure
Full Stack ClojureFull Stack Clojure
Full Stack Clojure
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”
 
Angular2 for Beginners
Angular2 for BeginnersAngular2 for Beginners
Angular2 for Beginners
 
論文紹介 Hyperkernel: Push-Button Verification of an OS Kernel (SOSP’17)
論文紹介 Hyperkernel: Push-Button Verification of an OS Kernel (SOSP’17)論文紹介 Hyperkernel: Push-Button Verification of an OS Kernel (SOSP’17)
論文紹介 Hyperkernel: Push-Button Verification of an OS Kernel (SOSP’17)
 
Tutorial matlab
Tutorial matlabTutorial matlab
Tutorial matlab
 

More from Javier Gonzalez-Sanchez (20)

201804 SER332 Lecture 01
201804 SER332 Lecture 01201804 SER332 Lecture 01
201804 SER332 Lecture 01
 
201801 SER332 Lecture 03
201801 SER332 Lecture 03201801 SER332 Lecture 03
201801 SER332 Lecture 03
 
201801 SER332 Lecture 04
201801 SER332 Lecture 04201801 SER332 Lecture 04
201801 SER332 Lecture 04
 
201801 SER332 Lecture 02
201801 SER332 Lecture 02201801 SER332 Lecture 02
201801 SER332 Lecture 02
 
201801 CSE240 Lecture 26
201801 CSE240 Lecture 26201801 CSE240 Lecture 26
201801 CSE240 Lecture 26
 
201801 CSE240 Lecture 25
201801 CSE240 Lecture 25201801 CSE240 Lecture 25
201801 CSE240 Lecture 25
 
201801 CSE240 Lecture 24
201801 CSE240 Lecture 24201801 CSE240 Lecture 24
201801 CSE240 Lecture 24
 
201801 CSE240 Lecture 23
201801 CSE240 Lecture 23201801 CSE240 Lecture 23
201801 CSE240 Lecture 23
 
201801 CSE240 Lecture 22
201801 CSE240 Lecture 22201801 CSE240 Lecture 22
201801 CSE240 Lecture 22
 
201801 CSE240 Lecture 21
201801 CSE240 Lecture 21201801 CSE240 Lecture 21
201801 CSE240 Lecture 21
 
201801 CSE240 Lecture 15
201801 CSE240 Lecture 15201801 CSE240 Lecture 15
201801 CSE240 Lecture 15
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
201801 CSE240 Lecture 13
201801 CSE240 Lecture 13201801 CSE240 Lecture 13
201801 CSE240 Lecture 13
 
201801 CSE240 Lecture 12
201801 CSE240 Lecture 12201801 CSE240 Lecture 12
201801 CSE240 Lecture 12
 
201801 CSE240 Lecture 11
201801 CSE240 Lecture 11201801 CSE240 Lecture 11
201801 CSE240 Lecture 11
 
201801 CSE240 Lecture 10
201801 CSE240 Lecture 10201801 CSE240 Lecture 10
201801 CSE240 Lecture 10
 
201801 CSE240 Lecture 09
201801 CSE240 Lecture 09201801 CSE240 Lecture 09
201801 CSE240 Lecture 09
 
201801 CSE240 Lecture 07
201801 CSE240 Lecture 07201801 CSE240 Lecture 07
201801 CSE240 Lecture 07
 
201801 CSE240 Lecture 06
201801 CSE240 Lecture 06201801 CSE240 Lecture 06
201801 CSE240 Lecture 06
 
201801 CSE240 Lecture 05
201801 CSE240 Lecture 05201801 CSE240 Lecture 05
201801 CSE240 Lecture 05
 

Recently uploaded

What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?Watsoo Telematics
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 

201801 CSE240 Lecture 17

  • 1. CSE240 – Introduction to Programming Languages Lecture 17: Programming with LISP| Control Structures Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2 Everything is a List • A function ID (symbol) is a series of characters other than whitespace, parentheses ( ), pound ( # ), quote ( ' ), double-quote ( " ), period ( . ), or back quote ( ` ). • A function ID (symbol) may not take the form of numbers. • It's very common for symbols to have hyphens ( - ) or asterisks ( * ) in them. • There are NOT operators! only functions • Symbols (function ID) are case-INSENSITIVE.
  • 3. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3 Atomic Expressions • -3 • 2.43 • 123342303923412323411323234012923492341231230234923410239234412 • #C(3.2 2) ; the complex number 3.2 + 2i • 2/3 ; the fraction 2/3. It is NOT divide 2 by 3 • -3.2e25 ; the number -3.2 x 10^25 • #g ; characters starts with # • #{ • # ; the character '' • #tab
 • #newline • 
#space
 • #backspace
 • #escape
  • 4. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4 Atomic Expressions • "Hello, World!" • "It's a glorious day." • "He said "No Way!" and then he left." • "I think I need a backslash here: Ah, that was better." • t ; true • nil ; false
  • 5. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5 Everything is a List • (+ 27/32 32/57) 2563/1824 • (* 2.342 3.2e4) 74944.0 • (* 2.342 9.212 -9.23 3/4) ; You can mix number types -149.34949 • (/ 3 5) ; The return type stays as general as possible 3/5 • (/ 3.0 5) ;Here LISP had no choice: convert to a float 0.6 • (1+ 3) 4
  • 6. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 6 Everything is a List • (string-upcase "How about that!") "HOW ABOUT THAT!" • (reverse "Four score and seven years ago") "oga sraey neves dna erocs ruoF" • (length "Four score and seven years ago") 30 • (sqrt 2) 1.4142135 • (sqrt -1.0) #C(0 1.0) • (SqRt -1.0) ;LISP symbols are case-insensitive #C(0 1.0)
  • 7. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 7 Function Arguments • Some functions have NOT a fixed number of arguments: (+ 100 231 201 921 221 231 -23 12 -34 134) • Some functions have a fixed number of arguments and optional arguments: (subseq "Four score and seven years ago" 9) "e and seven years ago” (subseq "Four score and seven years ago" 9 23) "e and seven ye"
  • 8. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8 Predicates (boolean functions) • LISP has a special name for functions which return "true" (usually t) or "false" (nil). These functions are called predicates. • (= 4 3) ; is 4 == 3 ? NIL • (< 3 9) ; is 3 < 9 ? T • (numberp "hello") ; is "foo" a number? NIL • (oddp 9) ; is 9 an odd number? T
  • 9. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9 Errors • Errors stop the program execution • (/ 1 0) *** - division by zero • (blah-blah-blah 1 0 "foo") *** - EVAL: the function BLAH-BLAH-BLAH is undefined
  • 10. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 10 Recursion • When a list contains another list among its expressions, the evaluation procedure is recursive. • (+ 33 (* 2.3 4) 9) 51.2 • (+ (length "Hello World") 44) 55 • (* (+ 3 2.3) (/ 3 (- 9 4))) ;in C++: (3+2.3) * (3 / (9-4)) 3.1800003 • (log (log (log 234231232234234123))) 1.3052895 • (+ (* (sin 0.3) (sin 0.3)) ; expressions may use multiple lines (* (cos 0.3) (cos 0.3))) ; sin(0.3)^2 + cos(0.3)^2 1.0000001 ; = 1. Rounding inaccuracy
  • 11. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 11 Recursion • (and (< 3 (* 2 5)) (not (>= 2 6))) ; (3 < 2 * 5) && !(2 >= 6) T • (print (+ 2 3 4 1)) 10 10 • (+ (* 2 3) (/ 3 2) 9) 33/2
  • 13. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 13 Control Structures • There are some evaluable lists which are not functions. • These lists are known as macros or special forms. • The control structure if is a special form.
  • 14. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 14 Control Structures • (if (<= 3 2) (* 3 9) (+ 4 2 3)) ; if (3<=2) {return 3*9;} else {return 4+2+3;} 9 • (if (> 2 3) 9) ; if (2>3) {return 9;} else {return nil} NIL • (if (= 2 2) (if (> 3 2) 4 6) 9) 4 • (+ 4 (if (= 2 2) (* 9 2) 7) ) 22
  • 15. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 15 Control Structures • if only allows one test-expression, one then-expression, and one optional-else-expression. • To do more things in the then-expression we need to make a block. Blocks are made with the special form progn, which takes the form: (progn expr1 expr2 expr3 ...) • (if (> 3 2) (progn (print "hello") (print "yo") (print "whassup?") (+ 2 2)) (+ 1 2 3)) "hello" "yo" "whatsup?" 4
  • 16. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 16 Loop (dotimes) dotimes is an iterator with this format (dotimes (var high-val optional-return-val) expr1 expr2 …) • (dotimes (x 4) (print "hello")) "hello" "hello" "hello" "hello" NIL • (setf bag 2) 2 • (dotimes (x 3) (setf bag (* bag bag))) NIL • (print bag) 256
  • 17. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 17 Global Variables • Variables are set with the macro setf. • (setf x (* 3 2)) 6 • x 6 • (setf y (+ x 3)) 9
  • 18. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 18 Local Variables • let declares local variables with each declaration. Then it evaluates the expressions in order (as a block). • let returns the value of the last expression. • (setf x 4) (let ((x 3)) ; x declared local (print x) (setf x 9) ; change the value of x (print x) (+ x x) ;this is the returned value ) 3 9 18 • x ;outside the let, we're back to global again 4
  • 19. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 19 Nested Variable Scope • (let ((x 3) (y (+ 4 9))) (* x y)) 39 • (let ((x 3)) (print x) (let (x) (print x) (let ((x "hello")) (print x)) (print x)) (print x) (print ”end")) 3 NIL "hello" NIL 3 "end" "end"
  • 20. CSE240 – Introduction to Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu Fall 2017 Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.