Monday, 18 February 2013

AGGREGATES!

This week we learned how to group values into a "list" and then used functions to manipulate that list using "map." This is a very useful idea if you wanted the same function to operate on multiple pieces of data. I found it especially difficult to understand at first, but after some experimenting on DrRacket, I sort of figured it out. I'm beginning to notice that when I play around with DrRacket, I seem to understand course concepts more better. I guess it is better to try coding on your own once you are introduced to a new concept.

Anyway, one problem I encountered when trying to figure out aggregate functions is the following. Use map and a function that you create called sub5 to transform the list (list 1 2 3 4 5) into the list (list -4 -3 -2 -1 0). In order to solve this problem, I had to first figure out how to develop a list with numbers 1 - 5 in order. I remembered from class, Professor Heap mentioned that we can use the range function to develop a list provided we tell the function how much increments the numbers will increase/decrease by. This is what I used: (range 1 6 1). This will tell DrRacket to produce a list of numbers starting from 1 and increasing by 1 until 6, but not including 6. Next, I had to define a function sub5. This wasn't hard at all since I already knew had to define functions from several weeks ago. This is what I used: (define (sub5 num) (- num 5). "num" is used as a placeholder. Lastly, we had to combine both our function and our list using the "map" function which tells DrRacket to use the function sub5 and operate it on every number in our list. This is what I used: (map sub5 (range 1 6 1)). This produced my desired answer.

This was a very interesting concept to learn and I am looking forward to class after reading week!

No comments:

Post a Comment