0704.315 Programming Languages

Homework 1 - Answer Key


Due Date

Section 1: Wednesday Sept 19, 18:30
Section 2: Wednesday Sept 19, 12:30

Form of Submission

Homework can submitted via email or printout by deadline.

Assignment

This assignment will make you familiar with the list-processing style.
  1. What is the difference between the Scheme values '(1 2) and '(1 . 2)?
    [3 pts]

    
    
    

    The former is a list (a linked list of two pairs), the latter is a (single) pair.

    
         
    
  2. From The Schemer's Guide, Question 1 in Problem Set 2 (p.55)
    [1 pt each]

    
    
    

    1. '((Mozart) Sibelius)
    2. #f
    3. '((Offenbach Bach Weber Strauss) Weber)
    4. '(())
    5. #f
    6. #t
    7. #f
    8. '(Offenbach . Weber)
    9. '((Bach))
    10. '((Percy Grainger) Offenbach Bach Weber Strauss)
    11. 'Weber
    12. '((Weber) Strauss)

    
    
    
    
  3. Write a function list-length, that returns the number of elements in a given list. So:
    	(list-length '(eeny meeny miney mo)) --> 4
            (list-length '())                    --> 0   
    	
    
         
    [5 pts]
      (define (list-length li)
        (if (null? li)
    	0
    	(+ 1 (list-length (rest li)))))
         
    
  4. Write a function list-append, that takes two lists as arguments, and returns the result of appending the latter to the end of the former. So:
    	(list-append '(mary had a little lamb) '(her fleece was white as snow))
               -> '(mary had a little lamb her fleece was white as snow)
            (list-append '() '(eeny meeny miney mo))
               -> '(eeny meeny miney mo)
            (list-append '(eeny meeny miney mo) '())
               -> '(eeny meeny miney mo)
            (list-append '() '())
               -> '()
    	
    [5 pts]
      (define (list-append a b)
        (if (null? a)
    	b
    	(cons (first a) (list-append (rest a) b))))
         
    
  5. Write a function nth, that takes two arguments, an integer (n) and a list, and returns the nth element from the list. If n is larger than the length of the list, return null (the empty list). If n is less than 1, return null. So:
    	(nth 2 '(eeny meeny miney mo)) --> 'meeny
    	(nth 4 '(eeny meeny miney mo)) --> 'mo
    	(nth 5 '(eeny meeny miney mo)) --> '()
    	(nth 0 '(eeny meeny miney mo)) --> '()
    	
    [5 pts]
      (define (nth n li)
        (cond ((null? li) '())
    	  ((< n 1) '())
    	  ((= n 1) (first li))
    	  (else (nth (- n 1) (rest li)))))
         
    
  6. Write a function marry, that takes two lists of equal length as arguments, and returns a list of pairs of the first elements, the second elements, etc. Don't worry about the case when the arguments are of unequal length. So:
            (marry '(a b c) '(1 2 3))  --> '((a . 1) (b . 2) (c . 3))
            (marry '() '())  --> '()
    	
    [5 pts]
      (define (marry li1 li2)
        (if (null? li1)
    	'()
    	(cons (cons (first li1) (first li2))
    	      (marry (rest li1) (rest li2)))))
    
    
  7. Write a function reverse-list, that takes a list as argument and returns a list of the same elements in reverse order. So:
            (reverse-list '(a b c d)) --> '(d c b a)
            (reverse-list '() '())  --> '()
    	
    Hint: You may need to write a "helper function" for reverse-list to call.
    [5 pts]
      (define (list-reverse li)
        (reverse-helper li '()))
         
      (define (reverse-helper li result)
        (if (null? li)
    	result
    	(reverse-helper (rest li) (cons (first li) result))))