Skip to content

Instantly share code, notes, and snippets.

@IwoHerka
Last active January 13, 2021 09:45
Show Gist options
  • Save IwoHerka/27a2dc839e21d382d1d9c7de6db5bb23 to your computer and use it in GitHub Desktop.
Save IwoHerka/27a2dc839e21d382d1d9c7de6db5bb23 to your computer and use it in GitHub Desktop.

exercises

syntax

  1. compute 1+1

  2. call the function get with arguments "ciao" and 1

  3. compute (3+4/5)*6

  4. define a vector with the elements 1, "hello" and true

  5. define a vector that contains the keywords :diff and :merge

  6. define a map with the key 1 is associated with the value "hello" and the key :key with the value [13 7]

  7. use def to define a variable my-map that refers to the map {1 2}. Use the assoc function to add a new key and value to my-map. What does the assoc call return? What is the value of my-map after the call?

  8. use conj to add a value to a vector

  9. use the function get to get the second element from a vector

  10. use the function get to get the value of a key from a map

  11. get the value of a key from a map using the map itself as a function

  12. get the value of a key from a map using a keyword as a function

  13. use the function get-in to return the value :treasure from the value:

    {:description "cave"
     :crossroads [{:contents :monster}
                  nil
                  {:contents [:trinket :treasure]}]}
  14. use defn to define a function hello that works like this: (hello) ==> "hello!"

  15. define a function double that works like this: (double 5) ==> 10, (double 1) ==> 2

  16. add a docstring to the function double. Then show it using (doc double).

  17. challenge! implement a factorial function using recursion.

conditionals & structures

  1. use the let structure to define a local variable b with the value "bork". Then use the str function to combine two bs into "borkbork".

  2. define a function small? that returns true for numbers under 100

  3. define a function message! that has three cases:

    (message! :boink) ==> "Boink!"
    (message! :pig)   ==> "oink"
    (message! :ping)  ==> "pong"
  4. reimplement message! using the if structure

  5. reimplement message! using the cond structure

  6. reimplement message! using the case structure

  7. challenge! use the loop structure to add 1 to an empty vector until it has 10 elements. Note: loop can be hard. Don't get stuck on this exercise!

functional programming?

  1. increment all the numbers in the vector [4 7 9 10] by one. Use the map funktiota. Hint: the function inc

  2. do the same as in the previous exercise, but leave only the even results in the vector. Use the functions filter and even?

  3. use the for structure to go through this vector of maps:

    [{:id 1 :value 7.0} {:id 2 :value 3.0} {:id 7 :value 1.1}]

    and return a sequence of the :values: (7.0 3.0 1.1)

  4. Use the function update-in to change 3 into 4 in the value below:

    {:shops [:shop-1]
     :customers [{:id "Pekka"
                  :account {:balance 3}}]}
  5. challenge! use the reduce function to combine a vector of maps like this:

    (combine [{:a 1 :b 2} {:c 3} {:d 4 :e 5}])
       ==> {:a 1 :b 2 :c 3 :d 4 :e 5}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment