SFTW120-LectureNotes-15

SFTW120-LectureNotes-15 - SFTW120 Programming Science...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 SFTW120 Programming Science Lecture Notes 15: Lisp: Selection Structures © Robert P. Biuk-Aghai SFTW120 - Programming Science 2 Lecture Notes 15 Lisp Selection Structures Lisp has 4 different selection structures: if – for choosing between two cases based on a test condition when – for conditionally evaluating a case based on a test form cond – for choosing between many cases based on a test condition case – for choosing between many cases based on a test condition (shorter than cond , but only works for symbols)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 SFTW120 - Programming Science 3 Lecture Notes 15 if s When a selection between two cases is needed s Format of if statement: (if ( test-form ) ( then-form ) ( else-form )) s If test-form evaluates to non- NIL : evaluate then-form s If test-form evaluates to NIL : evaluate else- form SFTW120 - Programming Science 4 Lecture Notes 15 Example of if (if (leap year) (setq days-in-year 366) (setq days-in-year 365)) Sets the value of variable days-in-year to 366 if the function leap returns non- NIL for the given year , otherwise sets it to 365. The else-form is optional, e.g.: (if (leap year) (setq days-in-year 366)) Note: Assume that function leap has already been defined
Background image of page 2
3 SFTW120 - Programming Science 5 Lecture Notes 15 Grouping Multiple Expressions in if s Then-form and else-form can only have one expression each s To include more than one expression: group expressions using progn , e.g.: (if (leap year) (progn (setq days-in-year 366) (setq feb-days 29))
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/04/2011 for the course SFST 120 taught by Professor Dr.robert during the Spring '11 term at University of Macau.

Page1 / 10

SFTW120-LectureNotes-15 - SFTW120 Programming Science...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online