Common Lisp John Paxton Montana State University Summer

  • Slides: 13
Download presentation
Common Lisp! John Paxton Montana State University Summer 2003

Common Lisp! John Paxton Montana State University Summer 2003

Montana Geography • The continental divide runs through the state of Montana in the

Montana Geography • The continental divide runs through the state of Montana in the Rocky Mountains. On the west, water flows to the Pacific Ocean. On the east, water flows to the Gulf of Mexico. • The longest undammed river in the U. S. , the Yellowstone River, is in Montana.

Montana Statehood • Montana was the 41 st state to join the United States.

Montana Statehood • Montana was the 41 st state to join the United States. It did so in 1889. • Bannock was the territorial capitol.

Iteration • • dotimes dolist do do*

Iteration • • dotimes dolist do do*

dotimes > (setf sum 0) 0 > (dotimes (i 10 sum) (setf sum (+

dotimes > (setf sum 0) 0 > (dotimes (i 10 sum) (setf sum (+ sum i)) ) 45

dotimes • Question. Write a function that computes the Fibonacci numbers using dotimes. •

dotimes • Question. Write a function that computes the Fibonacci numbers using dotimes. • Question. Compile the above function and run it.

dolist > (setf sum 0) 0 > (dolist (item '(1 2 3) sum) (setf

dolist > (setf sum 0) 0 > (dolist (item '(1 2 3) sum) (setf sum (+ sum item)) ) 6

dolist • Question. Write a function called mymapcar that is equivalent to mapcar. The

dolist • Question. Write a function called mymapcar that is equivalent to mapcar. The function should use the dolist construct.

do ( (local-variable-1 initial-value update) … (local-variable-n initial-value update) ) (termination-test intermediate-forms result) loop-body

do ( (local-variable-1 initial-value update) … (local-variable-n initial-value update) ) (termination-test intermediate-forms result) loop-body )

do > (do ( (sum 0) (counter 0 (+ 1 counter)) ) ( (>=

do > (do ( (sum 0) (counter 0 (+ 1 counter)) ) ( (>= counter 10) sum ) (setf sum (+ sum counter)) ) 45

do • Question: Write a function that computes the Fibonacci numbers using do.

do • Question: Write a function that computes the Fibonacci numbers using do.

do* • do* works just like do. The only difference is that the local

do* • do* works just like do. The only difference is that the local variables are assigned initial values and updated in sequence instead of in parallel.

do* • Question: Write a function that computes the Fibonacci numbers using do*.

do* • Question: Write a function that computes the Fibonacci numbers using do*.