Get Tech'Ed HomePage

Course Homepage


CSM CSCI-400

Intro to Racket

(Last Mod: 17 December 2013 13:16:52 )



Launch DrRacket and set the language to Full Swindle by selecting the "Language" item from the Menu list, then Choose Language, and finally selecting the Full Swindle from the list that pops up.

In this set of exercises, you will get to work with functions such as define, car, cdr, cons, append, list, cond, and others. You may want to explore some external tutorials; some possibilities include:

Exercises

Solutions to these exercises can be downloaded, but you should try to solve them yourself first.

NOTE: You browser probably can't display the contents if you click on the above link because it is a file that ends with a .rkt extension. You should be able to save the file to your computer, perhaps by right-clicking and choosing "Save As".

Exercise #1: Using define and car or first

Define a function named square which takes an integer and returns its square.

Define a function named squareFirst which takes a list as a parameter and returns the square of the first element of the list (assume the list is not empty). NOTE: Use the square function you just wrote.

Exercise #2: Understanding cons, append and list

Define 3 functions (showCons, showAppend, showList) which each take two parameters, an element and a list. The function will simply add the element to the front of the list. Hint: append requires a list as a first parameter, so you need to use the list function inside your showAppend function.

To further understand list, append and cons, call your functions with the parameters in the opposite order. Clearly if you want to extend a list by adding a value to the end, you'll need to do something different in your code (keep working on the exercises...).

Exercise #3: Function composition

Define a function named half that takes a number as a parameter and returns the number divided by 2. Then use function composition to take the square of half some value and then halve the square of some value.

Exercise #4: Using display, newline and composite functions.

Write a function named theParts that takes a complex list as input and displays various parts of the list, including car, cdr, caar, and cadar. Error handling is not necessary (e.g., it will generate an error if you pass it a list which does not have a cadar).

Now modify your function to use the Scheme list-ref function to display the car (you'll need to google list-ref). Why can't you do just a list-ref to display the caar or cadar?

Exercise #5: Using if, even? or modulo, cons and cdr

Define a function named SquareFirstIfEven that takes a list as a parameter and returns a (possibly modified) list as a result. If the first element of the list is even, that element will be squared. If the first element is not even, no changes will be made to the list. NOTES: You can assume the list parameter is not empty. You don't need to do any recursion for this exercise - just add the squared element to the front of the list and return it (or return the list unaltered).

Exercise #6: Using recursion, null?, append and list or cons

There are several ways to approach recursion in Scheme. Before you try this exercise, download the provided recursion examples and make sure you understand them. In the example file, the same task is done in two different ways. The doubleIt method calls itself recursively until it reaches the end of its input list. It then builds the result list as it rewinds (pops method calls off the stack). The dblIt method has an additional parameter, which starts as an empty list. The result list is built as part of the recursive calls. When the intial list is empty, the result list can just be returned.

DOWNLOAD: Demo Recursion.

Define a function named allSquares which takes a list as a parameter and returns a list of all the squares. NOTE: since this method has only one parameter, you'll be using the recursion strategy from doubleIt. Use your square function to calculate the squares.

Exercise #7: Using same elements as above (recursion, null? car, cdr, append, if)

Define a function named evenSq that takes a list as a parameter and returns a list of the squares of just the even elements in the list. If there are no even elements in the list, evenSq will return the empty list '(). NOTE: This function again follows the pattern from doubleIt.

Exercise #8: Using cond and if

Define a function named evenSquares which takes two parameters, a list of numbers and an initial result (starts with empty list) and returns a list of the squares of just the even numbers in the list. If all the numbers in the list are odd, the function returns '().

Do this function twice, once using if-else structure, once using a cond form. NOTE: This function follows the recursive strategy in dblIt.

HINT: Following the strategies above, your list is likely to be returned in reverse order. One way to fix this problem is to call reverse before you return the result: (reverse result). Another is to accumulate the result in the proper order. Try to "append" the new item to the back of the result list. Remember that both parameters to append should be lists.

Exercise #9: More with cond, using and

Define a function named evenSquaresTF which takes a list of numbers and returns a list of the squares of just the even numbers in the list (same as evenSquares). If all the numbers in the list are odd, the function returns #f.

HINT: It may be cleaner to modify your version of the above exercise that uses cond.

Exercise #10: User input and let

Write a function named userInfo that prompts the user for a name, stores the result in a local variable (using a let form), displays a prompt that includes the user name and asks for the users age, then calls a function named printMessage to display a message of your choice based on the user name and age. Hint: use the Scheme function read to collect the user input.

Exercise #11: let* vs let

Write a function named squareSquare that takes a number as a parameter. The function will use let* to a) set a local value named sq to the square of the parameter (use your square function from exercise 1), b) set another local value sq2 to the square of sq (again using the square function). If sq2 is evenly divisible by 4, display "yes" otherwise display "no".

NOTE: You wouldn't do this particular calculation in this fashion, but it will allow you to see the difference between let and let*.

Exercise #12: member

Define a variable named happyMoods with some descriptive words for happy, such as:

(define happyMoods '(happy ecstatic swell))

Then write a function named isHappy that takes a symbol as a parameter and returns #t if the symbol is a happy mood, #f otherwise.

Exercise #13: apply

Write a function named checkOutFirst that takes the name of a predicate and a list. Use apply to test just the first element of the list to see if the predicate applies, e.g.,


(apply pred (list(car list)))

Now write a function named checkOutAll that uses map to apply the predicate to the an entire list.

Now write a function named checkOutFirstString that is similar to checkOutFirst, but the predicate name is passed in as a string. HINT: you need to convert the string to a symbol, using string->symbol. Then you need to evaluate the symbol.

Exercise #14: more recursion

Write a function named countElements that takes a list of lists and returns the total number of elements.

Exercise #15: Update a list

Write a function named replaceElement that takes four parameters, the number of the element to be replaced, a new element, the original list, and the result list.