{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

SFTW120-LectureNotes-15

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

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

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)

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

View Full Document
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
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)

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}