Thursday, 22 November 2012

Apply

      When I saw the tutorial handout for the quiz #8, I learnt a new function, called apply. When I first saw the expression:
(apply + (list  3 5 4))
I thought it would have the same effect as the map function, so the above expression would like:
(map add1 (list 1 2 3 4 5))
But, then, just after I wrote down what I thought, I found that, I could not assume + was equal to add1. Therefore, a new list could not be produced. So I tried
(apply + (list 1 2 3 4 5))
in DrRacket, it produced 15. Then I knew it would produce a number by adding all the numbers in the list together.
      Next, I quickly knew the answer of the expression:
(apply + (map string-length (list "one" "three" "five")))
I would have
(apply + (list  3 5 4))
first by using string-length function to “one” “three” “five” to get numbers: 3 5 4. Then 3 5 4 were added together to produce 12. So, 12 was the answer
 

No comments:

Post a Comment