User Tools

Site Tools


cs330_f2016:racketlists

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
cs330_f2016:racketlists [2016/08/10 16:38]
morse [Notes:]
cs330_f2016:racketlists [2021/06/30 23:42] (current)
Line 5: Line 5:
   * Natural recursion   * Natural recursion
   * Common patterns for recursive list-processing operations   * Common patterns for recursive list-processing operations
-  * Using auxiliary ​variables +  * Using auxiliary ​parameters
- +
  
 ---- ----
Line 14: Line 12:
  
 You will again use DrRacket, which you should have downloaded and installed for the previous lab. You will again use DrRacket, which you should have downloaded and installed for the previous lab.
 +
 ---- ----
  
 ====Deliverables:​==== ====Deliverables:​====
  
-For this lab, you will need to implement the following functions ​and data structures ​in Racket:+For this lab, you will need to implement the following functions ​recursively ​in Racket:
   * ''​check-temps1''​   * ''​check-temps1''​
   * ''​check-temps''​   * ''​check-temps''​
Line 28: Line 27:
   * ''​get-nth''​   * ''​get-nth''​
   * ''​find-item''​   * ''​find-item''​
 +
 +**
 +You must write all of these functions recursively (though you can use helper functions as needed).
 +
 +You may not use higher-order functions like ''​map'',​ ''​filter'',​ or any of the variants of ''​fold''​. You may not use functions that use or return indices, such as ''​index-of''​ or ''​list-ref''​.
 +
 +You may not use anything that modifies a value in place such as ''​set!''​ or list-modification operators.
 +**
  
 === check-temps1 === === check-temps1 ===
Line 33: Line 40:
 (define (check-temps1 temps) ...) (define (check-temps1 temps) ...)
 </​code>​ </​code>​
-where ''​temps''​is a list of numbers representing temperatures and the value returned ​is a boolean indicating whether all of the temperatures are with the range 5 to 95 inclusive.+where ''​temps''​ is a list of numbers representing temperatures and the result ​is a boolean indicating whether all of the temperatures are with the range 5 to 95 inclusive.
  
-=== check-temps1 ​===+For example, ''​(check-temps1 (list 80 92 56))''​ returns true, and ''​(check-temps1 (list 80 99 56))''​ returns false. 
 + 
 +=== check-temps ===
 <code racket> <code racket>
 (define (check-temps temps low high) ...) (define (check-temps temps low high) ...)
 </​code>​ </​code>​
-where ''​temps''​is a list of numbers representing temperatures and the value returned ​is a boolean indicating whether all of the temperatures are with the range ''​low''​ to ''​high''​ inclusive.+where ''​temps''​ is a list of numbers representing temperatures and the result ​is a boolean indicating whether all of the temperatures are with the range ''​low''​ to ''​high''​ inclusive
 + 
 +For example, ''​(check-temps some-list-of-temps 5 95)''​ should work just like ''​(check-temps1 some-list-of-temps)''​. 
 + 
 +Suggestion: Yes, you could code ''​check-temps1''​ to just call your more general ''​check-temps''​. But try to code ''​check-temps1''​ first to see the basic pattern, then think about how to generalize it to ''​check-temps''​.
  
 === convert === === convert ===
Line 45: Line 58:
 (define (convert digits) ...) (define (convert digits) ...)
 </​code>​ </​code>​
-where ''​digits''​ ...+where ''​digits'' ​is a non-empty list of digits (each between 0 and 9) and the result is the integer equivalent of these interpreted as a list of digits of a number in reverse order  
 + 
 +For example, ''​(convert (list 1 2 3))''​ should return the number 321. 
 + 
 +You may not use ''​string->​number''​ or any other string-handling functions for this Avoid using strings at all. 
 + 
 +Hint: Don't try to figure out what position each digit is in as you process the list. (I.e., don't worry about "​hundreds place",​ "tens place",​ etc.). Can you figure out how to write the conversion with a more straightforward recursion? ​ What should recursing on the rest of the list return if the function works correctly? ​ What's the relationship between that and the first thing in the list that will give you the correct result?
  
 === duple === === duple ===
Line 51: Line 70:
 (define (duple lst) ...) (define (duple lst) ...)
 </​code>​ </​code>​
-where ''​lst''​ ...+where ''​lst'' ​is a list of any type of values, and the result is a list of two-element lists that has each value duplicated  
 + 
 +For example, ''​(duple (list 1 2 3))''​ returns the list ''​( (1 1) (2 2) (3 3) )''​. 
 + 
 +Hint: Be careful when to use ''​list''​ and when to use ''​cons''​.
  
 === average === === average ===
Line 57: Line 80:
 (define (average lst) ...) (define (average lst) ...)
 </​code>​ </​code>​
-where ''​lst''​ ...+where ''​lst'' ​is a non-empty list of numbers, and the result is the average of these numbers. 
 + 
 +For example, ''​(average (list 1 2 3 4))''​ returns 5/2. 
 + 
 +Hint: you are **not** required to compute the average in a single passCan you break it into simpler operations?
  
 === convertFC === === convertFC ===
Line 63: Line 90:
 (define (convertFC temps) ...) (define (convertFC temps) ...)
 </​code>​ </​code>​
-where ''​temps''​ ...+where ''​temps'' ​is a list of numbers representing temperatures in Fahrenheit, and the result is a list representing the same values in Celsius  
 + 
 +For example, ''​(convertFC (list 32 50 212))''​ returns ''​(0 10 100)''​.
  
 === eliminate-larger === === eliminate-larger ===
Line 69: Line 98:
 (define (eliminate-larger lst) ...) (define (eliminate-larger lst) ...)
 </​code>​ </​code>​
-where ''​lst''​ ...+where ''​lst'' ​is a list of numbers, and the result is the same list but with **all** values larger than **any** subsequent ones removed  
 + 
 +For example, ''​(eliminate-larger (list 1 2 3 9 4 5))''​ returns ''​(1 2 3 4 5)''​. 
 + 
 +Hint: try using a helper function. 
 + 
 +Hint 2: consider trying to make the decision about whether to drop a particular item from the list **after** recursively calling eliminate-larger on the rest after that one -- it's easier.
  
 === get-nth === === get-nth ===
Line 75: Line 110:
 (define (get-nth lst n) ...) (define (get-nth lst n) ...)
 </​code>​ </​code>​
-where ''​lst''​ and ''​n''​ ...+where ''​lst'' ​is a list of arbitrary types and ''​n'' ​is a non-negative number less than the length of the list, and the result is the nth item of the list using zero-based indexing  
 + 
 +For example ''​(get-nth (list 1 2 3 4) 2)''​ returns the value 3. 
 + 
 +Note: You must code this recursively and not use any other built-in Racket functions for such indexing, nor can you simply convert it to a vector and then do random access.
  
 === find-item === === find-item ===
Line 81: Line 120:
 (define (find-item lst target) ...) (define (find-item lst target) ...)
 </​code>​ </​code>​
-where ''​lst''​ and ''​target''​ ...+where ''​lst'' ​is a list of numbers ​and ''​target'' ​is a number, and the result is the position (using zero-based indexing) at which ''​target''​ first appears in the list, or -1 if it doesn'​t appear 
  
 +For example, ''​(find-item (list 1 2 3 4) 3)''​ returns the value 2, and ''​(find-item (list 1 2 3 4) 42)''​ returns -1.
  
 +Hint: consider using an auxiliary variable.
  
 ---- ----
Line 91: Line 132:
 You again **do not** need to bulletproof the code to enforce proper inputs.  ​ You again **do not** need to bulletproof the code to enforce proper inputs.  ​
 Your code only needs to return correct values given correct inputs. Your code only needs to return correct values given correct inputs.
 +But you should be careful to think about what the correct output should be for the case where the input list is empty.
  
 +----
  
 ====Hints:​==== ====Hints:​====
  
-You may want to again use Step button to walk through your code and watch the equivalent sequence of substitutions that are performed. ​ This is particularly useful in tracing the recursion and watching what's happening to the parameters.+You may want to again use the Step button to walk through your code and watch the equivalent sequence of substitutions that are performed. ​ This is particularly useful in tracing the recursion and watching what's happening to the parameters. 
 + 
 +You may use auxiliary or helper functions as needed. ​ For many of these the simplest solution is to use multiple functions instead of trying to cram it all through one recursive pass through the list.  You might also have to recurse through the list multiple times, in which case it's easiest to use separate functions. 
  
-Again use ''​check-expect''​ to write your own unit tests for your code. 
cs330_f2016/racketlists.1470847134.txt.gz · Last modified: 2021/06/30 23:40 (external edit)