bella
Uploaded by
17 SLIDES
317 VUES
170LIKES

Exploring Array Manipulations and Functions in Common Lisp

DESCRIPTION

This document from Montana State University, Summer 2003, delves into the intricacies of array declarations, element manipulations, and basic file I/O operations in Common Lisp. It discusses creating multi-dimensional arrays, accessing and modifying their contents, generating random numbers, formatted outputs, and includes exercises such as writing functions to find the smallest integer in an array and generating all permutations of a list. Ideal for those learning programming in Lisp and enhancing their understanding of data structures.

1 / 17

Download Presentation
Télécharger la présentation

Exploring Array Manipulations and Functions in Common Lisp

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Common Lisp! John Paxton Montana State University Summer 2003

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

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

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

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

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

  7. Random Numbers > (random 10) ;; 0 – 9, integer 3 > (random 10.0) ;; [0.0, 10.0], real 4.7472386

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

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

  10. Questions • 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. • Declare a 3 by 5 matrix named numbers that initially contains all 7s.

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

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

  13. File I/O • sample.dat file contents 1 2 3 4

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

  15. 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

  16. 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)) ))))

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

More Related
SlideServe
Audio
Live Player
Audio Wave
Play slide audio to activate visualizer