8.13. Exercises

  1. Based on the sum handler from this chapter, write a handler called sum3 that returns the sum of its three numeric arguments.

  2. Modify the listSum handler to sum only the numbers in the list given as its argument. So the call

    listSum ({"Hello", 100, 200, current date, 300})

    should return a result of 600 (= 100 + 200 + 300).

  3. Based on the removeItem handler you developed in this chapter, write a handler called removeItems that takes three arguments: the list as the first item, the starting index as the second, and the number of items to remove as the third. So the call

    removeItems ({1, 2, 3, 4, 5}, 2, 3)

    removes three items from the list {1,2,3,4,5} starting with the second item, returning the list {1,5}.

  4. Given the listSort handler defined in this chapter and a list of numbers stored in the variable L, what would be the effect of the following call?

    reverse of listSort(L)
  5. Based on the result of the last exercise, modify the listSort handler so that this call

    listSort of numList with ascendingSort

    gives an ascending sort of the list numList, whereas the call

    listSort of numList without ascendingSort

    produces a descending sort of the list numList. Bonus: This is not the most efficient way to produce a descending sort because the list is first sorted in ascending order and then reversed. Modify the quickSort handler to take an argument that specifies the direction of the sort and handles this directly within the code.

  6. Can you explain (or guess!) why the following program generates ...

Get Beginning AppleScript® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.