Clojure

Learn Clojure - Answers

Table of Contents

This page contains solutions to the "Test your knowledge" sections of the Learn Clojure guide.

Syntax

  1. Using the REPL, compute the sum of 7654 and 1234.

    user=> (+ 7654 1234)
    8888
  2. Rewrite the following algebraic expression as a Clojure expression: ( 7 + 3 * 4 + 5 ) / 10.

    (/ (+ 7 (* 3 4) 5) 10)
  3. Using REPL documentation functions, find the documentation for the rem and mod functions. Compare the results of the provided expressions based on the documentation.

    user=> (doc rem)
    clojure.core/rem
    ([num div])
      remainder of dividing numerator by denominator.
    nil
    
    user=> (doc mod)
    clojure.core/mod
    ([num div])
      Modulus of num and div. Truncates toward negative infinity.
    nil
  4. Using find-doc, find the function that prints the stack trace of the most recent REPL exception.

    pst

Functions

  1. Define a function greet that takes no arguments and prints "Hello".

    (defn greet []
      (println "Hello"))
  2. Redefine greet using def with fn and #().

    ;; using fn
    (def greet
      (fn [] (println "Hello")))
    
    ;; using #()
    (def greet
      #(println "Hello"))
  3. Define a function greeting …​

    (defn greeting
      ([] (greeting "Hello" "World"))
      ([x] (greeting "Hello" x))
      ([x y] (str x ", " y "!")))
  4. Define a function do-nothing …​

    (defn do-nothing [x] x)
  5. Define a function always-thing …​

    (defn always-thing [& xs] 100)
  6. Define a function make-thingy …​

    (defn make-thingy [x]
      (fn [& args] x))
  7. Define a function triplicate …​

    (defn triplicate [f]
      (f) (f) (f))
  8. Define a function opposite …​

    (defn opposite [f]
      (fn [& args] (not (apply f args))))
  9. Define a function triplicate2 …​

    (defn triplicate2 [f & args]
      (triplicate (fn [] (apply f args))))
  10. Using the java.lang.Math class …​

    user=> (Math/cos Math/PI)
    -1.0
    user=> (+ (Math/pow (Math/sin 0.2) 2)
              (Math/pow (Math/cos 0.2) 2))
    1.0
  11. Define a function that takes an HTTP URL as a string…​

    (defn http-get [url]
      (slurp
        (.openStream
          (java.net.URL. url))))
    (defn http-get [url]
      (slurp url))
  12. Define a function one-less-arg:

    (defn one-less-arg [f x]
      (fn [& args] (apply f x args)))
  13. Define a function two-fns:

    (defn two-fns [f g]
      (fn [x] (f (g x))))