{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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)
Image of page 1

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

View Full Document Right Arrow Icon
2 SFTW120 - Programming Science 3 Lecture Notes 15 if square6 When a selection between two cases is needed square6 Format of if statement: (if ( test-form ) ( then-form ) ( else-form )) square6 If test-form evaluates to non- NIL : evaluate then-form square6 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
Image of page 2
3 SFTW120 - Programming Science 5 Lecture Notes 15 Grouping Multiple Expressions in if square6 Then-form and else-form can only have one expression each square6 To include more than one expression: group expressions using progn , e.g.: (if (leap year)
Image of page 3

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

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

{[ snackBarMessage ]}