Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

List Procedures - Building Programming Experience - Homework, Exercises of Computer Programming

Some concept of Building Programming Experience are Evaluation, List Procedures, Experience Modifying, Non-Collaborative, Syntactic Sugar, Thesaurus.Main points of this homework are: List Procedures, Procedure, Numbers, Returns, Stutter-List, Procedure, Original, Element, Returns, Procedure

Typology: Exercises

2012/2013

Uploaded on 04/25/2013

lathika
lathika 🇮🇳

4

(12)

178 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Problem 1: List Procedures
1. Write the procedure square-list, which takes in a list of numbers and returns a list of their
squares.
(square-list (list 1 2 3))
;Value: (1 4 9)
(square-list (list 3))
;Value: 9
(square-list nil)
;Value: ()
2. Write the procedure stutter-list, which takes in a list and returns a list that contains each
element of the original list twice:
(stutter-list (list 1 2 3))
;Value: (1 1 2 2 3 3)
(stutter-list (list 1)
;Value: (1 1)
(stutter-list nil)
;Value: ()
3. Write the procedure only-even, which takes in a list of numbers and returns a new list
containing only the even numbers from the original list.
(only-even (list 1 2 3 4 5))
;Value: (2 4)
(only-even (list 1 3 5 7 9))
;Value: ()
(only-even nil)
;Value: ()
Docsity.com
pf3

Partial preview of the text

Download List Procedures - Building Programming Experience - Homework and more Exercises Computer Programming in PDF only on Docsity!

Problem 1: List Procedures

  1. Write the procedure square-list, which takes in a list of numbers and returns a list of their squares.

(square-list (list 1 2 3)) ;Value: (1 4 9) (square-list (list 3)) ;Value: 9 (square-list nil) ;Value: ()

  1. Write the procedure stutter-list, which takes in a list and returns a list that contains each element of the original list twice:

(stutter-list (list 1 2 3)) ;Value: (1 1 2 2 3 3) (stutter-list (list 1) ;Value: (1 1) (stutter-list nil) ;Value: ()

  1. Write the procedure only-even, which takes in a list of numbers and returns a new list containing only the even numbers from the original list.

(only-even (list 1 2 3 4 5)) ;Value: (2 4) (only-even (list 1 3 5 7 9)) ;Value: () (only-even nil) ;Value: ()

(only-even (list 2)) ;Value: (2) (only-even (list 3)) ;Value: ()

  1. Write the procedure add-lists, which takes in two lists of the same length and adds the elements of each of the lists together:

(add-lists (list 1 2 3) (list 4 5 6)) ;Value: (5 7 9) (add-lists nil nil) ;Value: ()

  1. Write the procedure palindrome-list, which takes a list as input and outputs a palindromic list with those elements. A palindrome reads the same forwards as backwards. Examples: 12321, "yay", and "A man, a plan, a canal: panama" (just the letters not the spaces or punctuation). Do this to a list by doubling the length of the list and inverting the order of the second half without duplicating the last element of the list. No recursive solution needed. You may assume the list is non-empty. You may use any of the procedures we have written in class.

(palindrome-list (list 1 2 3)) ;Value: (1 2 3 2 1) (palindrome-list (list 1)) ;Value: (1)

  1. Write the procedure replace-elem, which takes in a list of numbers, a number to replace, and a value to replace it with, and returns a list with the number replaced by the value.

(replace-elem (list 1 2 1 2 1 2) 1 5) ;Value: (5 2 5 2 5 2) (replace-elem (list 1 2 3 4) 3 7) ;Value: (1 2 7 4) (replace-elem (list 1 2 3) 7 77) ;Value: (1 2 3)

  1. Write the procedure list-average, which computes the average value of a list of numbers. You may use any procedures that you have written previously (two in particular will come in handy, making this a 1 liner). You may assume that the list is non-empty.

(list-average (list 1 2 3)) ;Value: 2 (list-average (list 1))