Common Lisp John Paxton Montana State University Summer

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

Common Lisp! John Paxton Montana State University Summer 2003

Montana Facts • The Plains Indians began using buffalo jumps over 2000 years ago.

Montana Facts • The Plains Indians began using buffalo jumps over 2000 years ago.

Array Declarations > (setf numbers (make-array '(4))) #(NIL NIL NIL) > (setf numbers (make-array

Array Declarations > (setf numbers (make-array '(4))) #(NIL NIL NIL) > (setf numbers (make-array '(4) : initial-element 0)) #(0 0 0 0)

Array Declarations > (setf numbers (make-array '(4) : initial-contents '(1 2 3 4))) #(1

Array Declarations > (setf numbers (make-array '(4) : initial-contents '(1 2 3 4))) #(1 2 3 4) > (setf numbers (make-array '(2 3))) #2 A((NIL NIL))

Array Access > (setf (aref numbers 0 0) 3) 3 > (setf (aref numbers

Array Access > (setf (aref numbers 0 0) 3) 3 > (setf (aref numbers 2 0) 3) *** - SYSTEM: : STORE: subscripts (2 0) for #2 A((3 NIL) (NIL NIL)) are out of range

Array Size Determination > (array-dimensions numbers) (2 3) > (array-dimension numbers 0) 2 >

Array Size Determination > (array-dimensions numbers) (2 3) > (array-dimension numbers 0) 2 > (array-dimension numbers 1) 3

Random Numbers > (random 10) 3 ; ; 0 – 9, integer > (random

Random Numbers > (random 10) 3 ; ; 0 – 9, integer > (random 10. 0) 4. 7472386 ; ; [0. 0, 10. 0], real

Formatted Output > (format t “pronto") pronto > (format t "Senor ~% Lopez") Senor

Formatted Output > (format t “pronto") pronto > (format t "Senor ~% Lopez") Senor Lopez

Formatted Output > (format t "~a + ~a" 1 2) 1+2

Formatted Output > (format t "~a + ~a" 1 2) 1+2

Questions 1. Write a function that receives a 1 -D integer array of size

Questions 1. Write a function that receives a 1 -D integer array of size 5 and returns the value of the smallest integer contained in the array. 2. Declare a 3 by 5 matrix named numbers that initially contains all 7 s.

Questions 3. Write a function called print-matrix that prints out the contents of the

Questions 3. Write a function called print-matrix that prints out the contents of the matrix that is passed in. Use the format statement. 4. Write a function called fill-matrix that gives each slot within the passed in matrix a random integer value between 0 and 10 inclusive.

File I/O (defun read-file ( file-name ) (with-open-file (data-file-name : direction : input) (do

File I/O (defun read-file ( file-name ) (with-open-file (data-file-name : direction : input) (do ((item (read data-file nil))) ((not item) 'done) (format t "~a ~%" item) )))

File I/O • sample. dat file contents 12 3 4

File I/O • sample. dat file contents 12 3 4

File I/O > (read-file "sample. dat") 1 2 3 4 DONE

File I/O > (read-file "sample. dat") 1 2 3 4 DONE

Question • Write a function that finds and prints all permutations of a list.

Question • Write a function that finds and prints all permutations of a list. For example, (permute ‘(1 2 3)) could print 1 2 3, 1 3 2, 2 1 3, 2 3 1, 3 1 2, 3 2 1

permute (defun permute (alist &optional (so-far nil)) (cond ((null alist) (format t "~a~%" so-far))

permute (defun permute (alist &optional (so-far nil)) (cond ((null alist) (format t "~a~%" so-far)) (t (dolist (item alist) (permute (remove item alist) (cons item so-far)) ))))

permute > (permute '(1 2 3)) (3 2 1) (2 3 1) (3 1

permute > (permute '(1 2 3)) (3 2 1) (2 3 1) (3 1 2) (1 3 2) (2 1 3) (1 2 3)