CIS 554 -- Clojure Quiz Name __________________________________
  1. Write an anonymous function (a "function literal") to return the third value in a list. You may assume that the list has at least three elements.
    (fn [x] (first (rest (rest x)))) 
           or
    (fn [x] (nth x 2)) 
  2. Write a function named neg to return the first negative number in a list of numbers. You may assume that the list contains some negative numbers.
    (defn neg [lst]
        (if (< (first lst) 0)
            (first lst)
            (neg (rest lst)) ) ) 
  3. Write an anonymous function that, given a positive integer N, will return a list or sequence (either one) of the integers 1 through N, inclusive.

    (fn [n] (take n (iterate inc 1)))
        or
    (fn [n] (range 1 (inc n)))

  4. Give the function defined in the previous question the name count-to. Then use count-to to write an expression whose value is a list of squares of the numbers 1 through 100. That is, it returns [1 4 9 16 ... 10000].

    (def count-to (fn [n] (range 1 (inc n))))
    
    (map (fn [x] (* x x)) (count-to 100))
         or
    (for [x (count-to 100)] (* x x))

  5. Define a function non-mult-3 with argument N which returns true if N is not a multiple of 3. Reminder: Clojure has mod and not functions that you can use.

    (defn non-mult-3 [x] (not (zero? (mod x 3))))

  6. Use the previously defined functions to compute the product of the numbers 1 through 25 that are not multiples of 3.

    (reduce * (filter non-mult-3 (count-to 25)))

  7. Briefly, what does it mean to say that "a transaction is atomic"?


    Viewed from another thread, either all the actions in the transaction have completed, or none have.



  8. Briefly, what is a reference varable (ref)?


    A mutable pointer to immutable data.


  9. The real problem with the Java approach to concurrency is that


    ___________________________ + ___________________________ = ___________________________.
    (fill in the blanks).

  10. Write a tail-recursive function named sum to add up all the numbers in a list of numbers. Hint: Define sum to have two parameters, and use one of them as an "accumulator."