AI Lisp - CS107 Autumn 2000 Handout 53 November 30, 2000...

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

View Full Document Right Arrow Icon
CS107 Handout 53 Autumn 2000 November 30, 2000 Lisp Section Examples Jerry's section handout from 5 years ago when he TA'ed CS107. Problem 1 LISP has an immense library of functions. Lots of them can be implemented with basic functions; people just wrote standard versions and put them into libraries because these basic routines are commonly used. Let's assume that the version of LISP that you have doesn't include the append function. Write your own append . It should take two lists as its arguments and return a new list that contains all the elements of the first list added onto the second list. Solution ;;;;;;;;; ;;; Function: my-append ;;;;;;;;; ;;; append takes two lists as its arguments. It returns a list ;;; containing the elements of both lists in the following order: ;;; all the members of the first list, followed by all the members of the ;;; second ;;;;;;;;; (defun my-append (list1 list2) (if (null list1) list2 (cons (car list1) (my-append (cdr list1) list2)) )) I'm compelled to mention three things about the solution that have nothing to do with the functionality of the solution itself. First of all, you must comment your code! Commenting is nice for the reader in C, but if anything it's more vital with LISP. Many people see a sea of parentheses and don't think in prefix notation, so reading LISP code that hasn't been well-commented can be frustrating. Second, it is just as important to choose terse yet descriptive names for your function names and variables. Third, good formatting is vital when the only real syntax is composed of nothing but parentheses. Even though the code is the same, there is an extreme disparity between the readability of the previous answer with the following code, even though they do the same thing! (defun app (l1 l2) (if (null l1) l2 (cons (car l1) (app (cdr l1) l2)))) When you're dealing with a function more complex than append , this makes a really big difference.
Background image of page 1

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

View Full DocumentRight Arrow Icon
2
Background image of page 2
3 Problem 2 Here's a problem that's similar to append in some ways: write a procedure that interleaves two lists. It should take two lists as arguments and return a list containing
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

AI Lisp - CS107 Autumn 2000 Handout 53 November 30, 2000...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online