Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

LISP Functions and Control Structures, Slides of Programming Languages

An introduction to lisp functions, predicates, set operations, and control structures. It covers topics such as defining functions, using conditional statements, and performing set operations on lists. The document also includes examples and explanations of various lisp functions and operators.

Typology: Slides

2011/2012

Uploaded on 11/10/2012

omni
omni 🇮🇳

4.6

(9)

46 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Functions
> (reverse L) ; reverses a list
(C B A)
> (length L) ; returns the length of list L
3
List has elements
(A B C)
Docsity.com
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download LISP Functions and Control Structures and more Slides Programming Languages in PDF only on Docsity!

Functions

> ( reverse L) ; reverses a list (C B A)

> ( length L) ; returns the length of list L 3

List has elements (A B C)

Predicates

• A special function which returns NIL if

the predicate is false, T or anything

other than NIL, otherwise

• We can use the following operators to

build the predicates

  • = , > , < , >= , <= for numeric values
  • eq or equal for others
  • atom : test if x is an atom
  • listp : test if x is a list
  • Also number , symbolp , null

> ( cond (<test-1> <action-1>)

... (<test-k> <action-k>))

  • Each (<test-i> <action-i>) pair is called a clause
  • If test-i (where i=1) returns T (or anything other than NIL), this function returns the value of action-i; otherwise it will go to the next clause
  • Usually the last test is T, which always holds (default case)
  • cond can be nested and may contain other conditional statements (action-i may contain (cond ...))

Control Flow

> (setf operation ‘area L 100 W 50)

> 50

> (cond ((eq operation ‘perimeter) (* 2 (+ L W)))

(eq operation ‘area) (* L W)) (t ‘i-do-not-understand-this) ) )

> 5000

Defining LISP functions

( defun func-name (arg-1 ... Arg-n) func-body)

Example:

> (defun y-plus (x) (+ x y)) ;definition of y-plus > (setq y 2) > (y-plus 23) 25

  • Local and global variables: local variables are

defined in function body

  • Function returns the value of last expression in

its body

Example:

(defun power (x y)

(if (= y 0) 1

(* x (power x (1- y)))

• There is a limit to the depth of recursion,

and it depends on the version of LISP

Recursion