Lisp Exercises: Complex Matrix Operations and Function Definitions - Prof. Amauri Antonio , Assignments of Electrical and Electronics Engineering

Lisp exercises for homework set #5 in fall 2008, focusing on complex matrix operations and function definitions. Students are required to give a short description of the method or approach used, list each program, and provide at least three test cases for each function. Functions include cadd for adding complex matrices, ctransp for transposing complex matrices, mapc for mapping functions with premature stopping, allnums as a predicate for lists of numbers, and eqset as a predicate for set equality using mapc.

Typology: Assignments

Pre 2010

Uploaded on 03/11/2009

koofers-user-pd4-1
koofers-user-pd4-1 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
EEL-5840 Hmwk 5
Fall 2008 Dr. Arroyo
Homework SET #5
Homework 5:Lisp Exercises 3 Due Tuesday September 30, 2008 in class
Tougher LISP Problems. Be sure to follow the guidelines for Programming Assignments. Make sure you give
me a short description of the method or approach used, a listing of each program and at least three test cases for
each. You may wish to use the "dribble" option in the file menu of XLISP to capture the screen as you test your
functions. Turn in your hard copy write-up in class but also send me an e-mail copy of your LISP code.
1. An N๎˜N complex matrix R is represented by N lists of N pairs, where the first element of each pair is the real
part of the matrix component and the second element of each pair is the imaginary part of the corresponding
matrix component. Give a definition for CADD, a function to add complex matrices using a mapping function.
> (cadd '((1 2) (3 4)) '((5 6) (7 8))) > (cadd โ€˜(((1 2) (3 4)) ((5 6) (7 8))) โ€˜(((10 10) (10 10)) ((1 1) (1 1))) )
( (6 8) (10 12) ) ( ((11 12) (13 14)) ((6 7) (8 9)) )
> (cadd '((1 2) (3 4)) () ) > (cadd () '((1 2) (3 4)) )
() nil
> (cadd () () ) > (cadd '((1 2)) '((3 4)))
nil ((4 6))
2. Give a definition for CTRANSP, a function to transpose a complex matrix using a mapping function.
> (ctransp '(((1 2) (3 -4)) ((5 6) (7 -8)))) > (ctransp () )
(((1 2) (5 6)) ((3 -4)) ((7 -8))) Nil
> (ctransp '(((1 2) (3 4)) ((5 6) (7 8))) ) > (ctransp โ€˜(((10 10) (10 10)) ((1 1) (1 1))) )
(((1 2) (5 6)) ((3 4)) ((7 8))) (((10 10) (1 1)) ((10 10)) ((1 1)))
3. MAPC is like MAPCAR in that it hands to its functional argument (funarg) successive cars of the input list.
MAPC differs from MAPCAR in that MAPC prematurely stops applying the funarg to the successive cars of
the input list if the value returned by the argument function is NIL. Further, MAPC returns NIL if it was
stopped or T if it was allowed to exhaust the input list. This is useful because many of the functions we have
defined in LISP should be stopped when the result is obvious and there is no need to traverse the input list
until it is exhausted. Give a definition for the MAPC mapping function using a PROG.
4. Define ALLNUMS using MAPC. ALLNUMS is a predicate that returns T if its argument is a list of numbers.
Lisp> (ALLNUMS '(1 3 7)) ==> T
5. Define EQSET, a predicate that returns true if two sets are the same set, using the MAPC mapping function.
Lisp> (eqset '(a b c) '(b a) ) ==> Nil
Lisp> (eqset '(a b) '(a b c) ) ==> Nil
Lisp> (eqset '(a b c) '(b a c) ) ==> T
Lisp> (eqset '(coke is it) '(is it coke) ) ==> T
Lisp> (eqset '(inhuman acts are human mistakes) '(human acts are inhuman mistakes) ) ==> T
6. Write a lisp program which takes a simple numeric list as input and returns a sorted list in descending order.
Do not remove duplications.
Lisp> (sort '(3 4 1 0)) ==> (4 3 1 0)
Lisp> (sort '(1 5 3 2)) ==> (5 3 2 1)
Lisp> (sort '(3 4 3 2 1 2)) ==> (4 3 3 2 2 1)
This problem may require at least one function using a PROG statement and one or two small, recursive
helper functions. You are under no restrictions concerning the sort method (bubble, insertion, radix,
quicksort, etc.) you may want to implement.

Partial preview of the text

Download Lisp Exercises: Complex Matrix Operations and Function Definitions - Prof. Amauri Antonio and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

EEL-5840 Hmwk 5

Fall 2008 Dr. Arroyo

Homework SET

Homework 5:Lisp Exercises 3 Due Tuesday September 30, 2008 in class

Tougher LISP Problems. Be sure to follow the guidelines for Programming Assignments. Make sure you give

me a short description of the method or approach used, a listing of each program and at least three test cases for

each. You may wish to use the "dribble" option in the file menu of XLISP to capture the screen as you test your

functions. Turn in your hard copy write-up in class but also send me an e-mail copy of your LISP code.

1. An NN complex matrix R is represented by N lists of N pairs, where the first element of each pair is the real

part of the matrix component and the second element of each pair is the imaginary part of the corresponding

matrix component. Give a definition for CADD, a function to add complex matrices using a mapping function.

(cadd '((1 2) (3 4)) '((5 6) (7 8))) > (cadd โ€˜(((1 2) (3 4)) ((5 6) (7 8))) โ€˜(((10 10) (10 10)) ((1 1) (1 1))) ) ( (6 8) (10 12) ) ( ((11 12) (13 14)) ((6 7) (8 9)) ) (cadd '((1 2) (3 4)) () ) > (cadd () '((1 2) (3 4)) ) () nil (cadd () () ) > (cadd '((1 2)) '((3 4))) nil ((4 6))

2. Give a definition for CTRANSP , a function to transpose a complex matrix using a mapping function.

(ctransp '(((1 2) (3 -4)) ((5 6) (7 -8)))) > (ctransp () )

(((1 2) (5 6)) ((3 -4)) ((7 -8))) Nil

(ctransp '(((1 2) (3 4)) ((5 6) (7 8))) ) > (ctransp โ€˜(((10 10) (10 10)) ((1 1) (1 1))) ) (((1 2) (5 6)) ((3 4)) ((7 8))) (((10 10) (1 1)) ((10 10)) ((1 1)))

3. MAPC is like MAPCAR in that it hands to its functional argument (funarg) successive cars of the input list.

MAPC differs from MAPCAR in that MAPC prematurely stops applying the funarg to the successive cars of

the input list if the value returned by the argument function is NIL. Further, MAPC returns NIL if it was

stopped or T if it was allowed to exhaust the input list. This is useful because many of the functions we have

defined in LISP should be stopped when the result is obvious and there is no need to traverse the input list

until it is exhausted. Give a definition for the MAPC mapping function using a PROG.

4. Define ALLNUMS using MAPC. ALLNUMS is a predicate that returns T if its argument is a list of numbers.

Lisp> (ALLNUMS '(1 3 7)) ==> T

5. Define EQSET, a predicate that returns true if two sets are the same set, using the MAPC mapping function.

Lisp> (eqset '(a b c) '(b a) ) ==> Nil Lisp> (eqset '(a b) '(a b c) ) ==> Nil Lisp> (eqset '(a b c) '(b a c) ) ==> T Lisp> (eqset '(coke is it) '(is it coke) ) ==> T Lisp> (eqset '(inhuman acts are human mistakes) '(human acts are inhuman mistakes) ) ==> T

6. Write a lisp program which takes a simple numeric list as input and returns a sorted list in descending order.

Do not remove duplications.

Lisp> (sort '(3 4 1 0)) ==> (4 3 1 0) Lisp> (sort '(1 5 3 2)) ==> (5 3 2 1) Lisp> (sort '(3 4 3 2 1 2)) ==> (4 3 3 2 2 1)

This problem may require at least one function using a PROG statement and one or two small, recursive

helper functions. You are under no restrictions concerning the sort method (bubble, insertion, radix,

quicksort, etc.) you may want to implement.