4Clojure 練習

4clojure 是一個非常棒的 Clojure 練習網站,可以在練習的過程中逐漸熟悉 clojure 的 核心命令以及常用函式。

這邊列出我最近重寫 4clojure 整理的資訊。

目前排名資訊:

Username: coldnew
Rank: 1916 out of 38587
Problems Solved: 85

001: Nothing but the Truth

URL: https://www.4clojure.com/problem/1

This is a clojure form. Enter a value which will make the form evaluate to true.
Don't over think it! If you are confused, see the getting started page. Hint:
true is equal to true.

(= __ true)


點我顯示/隱藏內容














002: Simple Math

URL: https://www.4clojure.com/problem/2

If you are not familiar with polish notation, simple arithmetic might seem
confusing.

Note: Enter only enough to fill in the blank (in this case, a single number) -
do not retype the whole problem.

(= (- 10 (* 2 3)) __)


點我顯示/隱藏內容














003: Intro to Strings

URL: https://www.4clojure.com/problem/3

Clojure strings are Java strings. This means that you can use any of the Java
string methods on Clojure strings.

(= __ (.toUpperCase "hello world"))


點我顯示/隱藏內容














004: Intro to Lists

URL: https://www.4clojure.com/problem/4

Lists can be constructed with either a function or a quoted form.

(= (list __) '(:a :b :c))


點我顯示/隱藏內容














005: Lists: conj

URL: https://www.4clojure.com/problem/5

When operating on a list, the conj function will return a new list with one or
more items "added" to the front.

Note that there are two test cases, but you are expected to supply only one
answer, which will cause all the tests to pass.

(= __ (conj '(2 3 4) 1))
(= __ (conj '(3 4) 2 1))


點我顯示/隱藏內容














006: Intro to Vectors

URL: https://www.4clojure.com/problem/6

Vectors can be constructed several ways. You can compare them with lists.

Note: the brackets [] surrounding the blanks __ are part of the test case.

(= [__] (list :a :b :c) (vec '(:a :b :c)) (vector :a :b :c))


點我顯示/隱藏內容














007: Vectors: conj

URL: https://www.4clojure.com/problem/7

When operating on a Vector, the conj function will return a new vector with one
or more items "added" to the end. test not run

(= __ (conj [1 2 3] 4))
(= __ (conj [1 2] 3 4))


點我顯示/隱藏內容














008: Intro to Sets

URL: https://www.4clojure.com/problem/8

Sets are collections of unique values.

(= __ (set '(:a :a :b :c :c :c :c :d :d)))
(= __ (clojure.set/union #{:a :b :c} #{:b :c :d}))


點我顯示/隱藏內容














009: Sets: conj

URL: https://www.4clojure.com/problem/9

When operating on a set, the conj function returns a new set with one or more
keys "added".

(= #{1 2 3 4} (conj #{1 4 3} __))


點我顯示/隱藏內容














010: Intro to Maps

URL: https://www.4clojure.com/problem/10

Maps store key-value pairs. Both maps and keywords can be used as lookup
functions. Commas can be used to make maps more readable, but they are not
required.

(= __ ((hash-map :a 10, :b 20, :c 30) :b))
(= __ (:b {:a 10, :b 20, :c 30}))


點我顯示/隱藏內容














011: Maps: conj

URL: https://www.4clojure.com/problem/11

When operating on a map, the conj function returns a new map with one or more
key-value pairs "added".

(= {:a 1, :b 2, :c 3} (conj {:a 1} __ [:c 3]))


點我顯示/隱藏內容














012: Intro to Sequences

URL: https://www.4clojure.com/problem/12

All Clojure collections support sequencing. You can operate on sequences with
functions like first, second, and last.

(= __ (first '(3 2 1)))
(= __ (second [2 3 4]))
(= __ (last (list 1 2 3)))


點我顯示/隱藏內容














013: Sequences: rest

URL: https://www.4clojure.com/problem/13

The rest function will return all the items of a sequence except the first.

(= __ (rest [10 20 30 40]))


點我顯示/隱藏內容














014: Intro to Functions

URL: https://www.4clojure.com/problem/14

Clojure has many different ways to create functions.

(= __ ((fn add-five [x] (+ x 5)) 3))
(= __ ((fn [x] (+ x 5)) 3))
(= __ (#(+ % 5) 3))
(= __ ((partial + 5) 3))


點我顯示/隱藏內容














015: Double Down

URL: https://www.4clojure.com/problem/15

Write a function which doubles a number.

(= (__ 2) 4)
(= (__ 3) 6)
(= (__ 11) 22)
(= (__ 7) 14)


點我顯示/隱藏內容














016: Hello World

URL: https://www.4clojure.com/problem/16

Write a function which returns a personalized greeting.

(= (__ "Dave") "Hello, Dave!")
(= (__ "Jenn") "Hello, Jenn!")
(= (__ "Rhea") "Hello, Rhea!")


點我顯示/隱藏內容














017: Sequences: map

URL: https://www.4clojure.com/problem/17

The map function takes two arguments: a function (f) and a sequence (s). Map
returns a new sequence consisting of the result of applying f to each item of s.
Do not confuse the map function with the map data structure.

(= __ (map #(+ % 5) '(1 2 3)))


點我顯示/隱藏內容














018: Sequences: filter

URL: https://www.4clojure.com/problem/18

The filter function takes two arguments: a predicate function (f) and a sequence
(s). Filter returns a new sequence consisting of all the items of s for which (f
item) returns true.

(= __ (filter #(> % 5) '(3 4 5 6 7)))


點我顯示/隱藏內容














019: Last Element

URL: https://www.4clojure.com/problem/19

Write a function which returns the last element in a sequence.

(= (__ [1 2 3 4 5]) 5)
(= (__ '(5 4 3)) 3)
(= (__ ["b" "c" "d"]) "d")

Special Restrictions

  • last


點我顯示/隱藏內容














020: Penultimate Element

URL: https://www.4clojure.com/problem/20

Write a function which returns the second to last element from a sequence.

(= (__ (list 1 2 3 4 5)) 4)
(= (__ ["a" "b" "c"]) "b")
(= (__ [[1 2] [3 4]]) [1 2])


點我顯示/隱藏內容














021: Nth Element

URL: https://www.4clojure.com/problem/21

Write a function which returns the Nth element from a sequence.

(= (__ '(4 5 6 7) 2) 6)
(= (__ [:a :b :c] 0) :a)
(= (__ [1 2 3 4] 1) 2)
(= (__ '([1 2] [3 4] [5 6]) 2) [5 6])

Special Restrictions

  • nth


點我顯示/隱藏內容














022: Count a Sequence

URL: https://www.4clojure.com/problem/22

Write a function which returns the total number of elements in a sequence.

(= (__ '(1 2 3 3 1)) 5)
(= (__ "Hello World") 11)
(= (__ [[1 2] [3 4] [5 6]]) 3)
(= (__ '(13)) 1)
(= (__ '(:a :b :c)) 3)

Special Restrictions

  • count


點我顯示/隱藏內容














023: Reverse a Sequence

URL: https://www.4clojure.com/problem/23

Write a function which reverses a sequence.

(= (__ [1 2 3 4 5]) [5 4 3 2 1])
(= (__ (sorted-set 5 7 2 7)) '(7 5 2))
(= (__ [[1 2][3 4][5 6]]) [[5 6][3 4][1 2]])

Special Restrictions

  • reverse
  • rseq


點我顯示/隱藏內容














024: Sum It All Up

URL: https://www.4clojure.com/problem/24

Write a function which returns the sum of a sequence of numbers.

(= (__ [1 2 3]) 6)
(= (__ (list 0 -2 5 5)) 8)
(= (__ #{4 2 1}) 7)
(= (__ '(0 0 -1)) -1)
(= (__ '(1 10 3)) 14)


點我顯示/隱藏內容














025: Find the odd numbers

URL: https://www.4clojure.com/problem/25

Write a function which returns only the odd numbers from a sequence.

(= (__ #{1 2 3 4 5}) '(1 3 5))
(= (__ [4 2 1 6]) '(1))
(= (__ [2 2 4 6]) '())
(= (__ [1 1 1 3]) '(1 1 1 3))


點我顯示/隱藏內容














026: Fibonacci Sequence

URL: https://www.4clojure.com/problem/26

Write a function which returns the first X fibonacci numbers.



點我顯示/隱藏內容














027: Palindrome Detector

URL: https://www.4clojure.com/problem/27

Write a function which returns true if the given sequence is a palindrome.

Hint: "racecar" does not equal '(\r \a \c \e \c \a \r)

(false? (__ '(1 2 3 4 5)))
(true? (__ "racecar"))
(true? (__ [:foo :bar :foo]))
(true? (__ '(1 1 3 3 1 1)))
(false? (__ '(:a :b :c)))


點我顯示/隱藏內容














028: Flatten a Sequence

URL: https://www.4clojure.com/problem/28

Write a function which flattens a sequence.

(= (__ '((1 2) 3 [4 [5 6]])) '(1 2 3 4 5 6))
(= (__ ["a" ["b"] "c"]) '("a" "b" "c"))
(= (__ '((((:a))))) '(:a))

Special Restrictions

  • flatten


點我顯示/隱藏內容














029: Get the Caps

URL: https://www.4clojure.com/problem/29

Write a function which takes a string and returns a new string containing only
the capital letters.

(= (__ "HeLlO, WoRlD!") "HLOWRD")
(empty? (__ "nothing"))
(= (__ "$#A(*&987Zf") "AZ")


點我顯示/隱藏內容














030: Compress a Sequence

URL: https://www.4clojure.com/problem/30

Write a function which removes consecutive duplicates from a sequence.

(= (apply str (__ "Leeeeeerrroyyy")) "Leroy")
(= (__ [1 1 2 3 3 2 2 3]) '(1 2 3 2 3))
(= (__ [[1 2] [1 2] [3 4] [1 2]]) '([1 2] [3 4] [1 2]))


點我顯示/隱藏內容














031: Pack a Sequence

URL: https://www.4clojure.com/problem/31

Write a function which packs consecutive duplicates into sub-lists.

(= (__ [1 1 2 1 1 1 3 3]) '((1 1) (2) (1 1 1) (3 3)))
(= (__ [:a :a :b :b :c]) '((:a :a) (:b :b) (:c)))
(= (__ [[1 2] [1 2] [3 4]]) '(([1 2] [1 2]) ([3 4])))


點我顯示/隱藏內容














032: Duplicate a Sequence

URL: https://www.4clojure.com/problem/32

Write a function which duplicates each element of a sequence.

(= (__ [1 2 3]) '(1 1 2 2 3 3))
(= (__ [:a :a :b :b]) '(:a :a :a :a :b :b :b :b))
(= (__ [[1 2] [3 4]]) '([1 2] [1 2] [3 4] [3 4]))
(= (__ [[1 2] [3 4]]) '([1 2] [1 2] [3 4] [3 4]))


點我顯示/隱藏內容














033: Replicate a Sequence

URL: https://www.4clojure.com/problem/33

Write a function which replicates each element of a sequence a variable number
of times.

(= (__ [1 2 3] 2) '(1 1 2 2 3 3))
(= (__ [:a :b] 4) '(:a :a :a :a :b :b :b :b))
(= (__ [4 5 6] 1) '(4 5 6))
(= (__ [[1 2] [3 4]] 2) '([1 2] [1 2] [3 4] [3 4]))
(= (__ [44 33] 2) [44 44 33 33])


點我顯示/隱藏內容














034: Implement range

URL: https://www.4clojure.com/problem/34

Write a function which creates a list of all integers in a given range.

(= (__ 1 4) '(1 2 3))
(= (__ -2 2) '(-2 -1 0 1))
(= (__ 5 8) '(5 6 7))

Special Restrictions

  • range


點我顯示/隱藏內容














035: Local bindings

URL: https://www.4clojure.com/problem/35

Clojure lets you give local names to values using the special let-form.

(= __ (let [x 5] (+ 2 x)))
(= __ (let [x 3, y 10] (- y x)))
(= __ (let [x 21] (let [y 3] (/ x y))))


點我顯示/隱藏內容














036: Let it Be

URL: https://www.4clojure.com/problem/36

Can you bind x, y, and z so that these are all true?



點我顯示/隱藏內容














037: Regular Expressions

URL: https://www.4clojure.com/problem/37

Regex patterns are supported with a special reader macro.

(= __ (apply str (re-seq #"[A-Z]+" "bA1B3Ce ")))


點我顯示/隱藏內容














038: Maximum value

URL: https://www.4clojure.com/problem/38

Write a function which takes a variable number of parameters and returns the
maximum value.

(= (__ 1 8 3 4) 8)
(= (__ 30 20) 30)
(= (__ 45 67 11) 67)

Special Restrictions

  • max
  • max-key


點我顯示/隱藏內容














039: Interleave Two Seqs

URL: https://www.4clojure.com/problem/39

Write a function which takes two sequences and returns the first item from each,
then the second item from each, then the third, etc.

(= (__ [1 2 3] [:a :b :c]) '(1 :a 2 :b 3 :c))
(= (__ [1 2] [3 4 5 6]) '(1 3 2 4))
(= (__ [1 2 3 4] [5]) [1 5])
(= (__ [30 20] [25 15]) [30 25 20 15])

Special Restrictions

  • interleave


點我顯示/隱藏內容














040: Interpose a Seq

URL: https://www.4clojure.com/problem/40

Write a function which separates the items of a sequence by an arbitrary value.

(= (__ 0 [1 2 3]) [1 0 2 0 3])
(= (apply str (__ ", " ["one" "two" "three"]))
   "one, two, three")
(= (__ :z [:a :b :c :d]) [:a :z :b :z :c :z :d])

Special Restrictions

  • interpose


點我顯示/隱藏內容














041: Drop Every Nth Item

URL: https://www.4clojure.com/problem/41

Write a function which drops every Nth item from a sequence.

(= (__ [1 2 3 4 5 6 7 8] 3) [1 2 4 5 7 8])
(= (__ [:a :b :c :d :e :f] 2) [:a :c :e])
(= (__ [1 2 3 4 5 6] 4) [1 2 3 5 6])


點我顯示/隱藏內容














042: Factorial Fun

URL: https://www.4clojure.com/problem/42

Write a function which calculates factorials.

(= (__ 1) 1)
(= (__ 3) 6)
(= (__ 5) 120)
(= (__ 8) 40320)


點我顯示/隱藏內容














043: Reverse Interleave

URL: https://www.4clojure.com/problem/43

Write a function which reverses the interleave process into x number of
subsequences.

(= (__ [1 2 3 4 5 6] 2) '((1 3 5) (2 4 6)))
(= (__ (range 9) 3) '((0 3 6) (1 4 7) (2 5 8)))
(= (__ (range 10) 5) '((0 5) (1 6) (2 7) (3 8) (4 9)))


點我顯示/隱藏內容














044: Rotate Sequence

URL: https://www.4clojure.com/problem/44

Write a function which can rotate a sequence in either direction.

(= (__ 2 [1 2 3 4 5]) '(3 4 5 1 2))
(= (__ -2 [1 2 3 4 5]) '(4 5 1 2 3))
(= (__ 6 [1 2 3 4 5]) '(2 3 4 5 1))
(= (__ 1 '(:a :b :c)) '(:b :c :a))
(= (__ -4 '(:a :b :c)) '(:c :a :b))


點我顯示/隱藏內容














045: Intro to Iterate

URL: https://www.4clojure.com/problem/45

The iterate function can be used to produce an infinite lazy sequence.

(= __ (take 5 (iterate #(+ 3 %) 1)))


點我顯示/隱藏內容














046: Flipping out

URL: https://www.4clojure.com/problem/46

Write a higher-order function which flips the order of the arguments of an input
function.

(= 3 ((__ nth) 2 [1 2 3 4 5]))
(= true ((__ >) 7 8))
(= 4 ((__ quot) 2 8))
(= [1 2 3] ((__ take) [1 2 3 4 5] 3))


點我顯示/隱藏內容














047: Contain Yourself

URL: https://www.4clojure.com/problem/47

The contains? function checks if a KEY is present in a given collection. This
often leads beginner clojurians to use it incorrectly with numerically indexed
collections like vectors and lists.

(contains? #{4 5 6} __)
(contains? [1 1 1 1 1] __)
(contains? {4 :a 2 :b} __)
(not (contains? [1 2 4] __))


點我顯示/隱藏內容














048: Intro to some

URL: https://www.4clojure.com/problem/48

The some function takes a predicate function and a collection. It returns the
first logical true value of (predicate x) where x is an item in the collection.

(= __ (some #{2 7 6} [5 6 7 8]))
(= __ (some #(when (even? %) %) [5 6 7 8]))


點我顯示/隱藏內容














049: Split a sequence

URL: https://www.4clojure.com/problem/49

Write a function which will split a sequence into two parts.

(= (__ 3 [1 2 3 4 5 6]) [[1 2 3] [4 5 6]])
(= (__ 1 [:a :b :c :d]) [[:a] [:b :c :d]])
(= (__ 2 [[1 2] [3 4] [5 6]]) [[[1 2] [3 4]] [[5 6]]])

Special Restrictions

  • split-at


點我顯示/隱藏內容














050: Split by Type

URL: https://www.4clojure.com/problem/50

Write a function which takes a sequence consisting of items with different types
and splits them up into a set of homogeneous sub-sequences. The internal order
of each sub-sequence should be maintained, but the sub-sequences themselves can
be returned in any order (this is why 'set' is used in the test cases).

(= (set (__ [1 :a 2 :b 3 :c])) #{[1 2 3] [:a :b :c]})
(= (set (__ [:a "foo"  "bar" :b])) #{[:a :b] ["foo" "bar"]})
(= (set (__ [[1 2] :a [3 4] 5 6 :b])) #{[[1 2] [3 4]] [:a :b] [5 6]})


點我顯示/隱藏內容














051: Advanced Destructuring

URL: https://www.4clojure.com/problem/51

Here is an example of some more sophisticated destructuring.

(= [1 2 [3 4 5] [1 2 3 4 5]] (let [[a b & c :as d] __] [a b c d]))


點我顯示/隱藏內容














052: Intro to Destructuring

URL: https://www.4clojure.com/problem/52

Let bindings and function parameter lists support destructuring.

(= [2 4] (let [[a b c d e] [0 1 2 3 4]] __))


點我顯示/隱藏內容














053: Longest Increasing Sub-Seq

URL: https://www.4clojure.com/problem/53

Given a vector of integers, find the longest consecutive sub-sequence of
increasing numbers. If two sub-sequences have the same length, use the one that
occurs first. An increasing sub-sequence must have a length of 2 or greater to
qualify.

(= (__ [1 0 1 2 3 0 4 5]) [0 1 2 3])
(= (__ [5 6 1 3 2 7]) [5 6])
(= (__ [2 3 3 4 5]) [3 4 5])
(= (__ [7 6 5 4]) [])


點我顯示/隱藏內容














054: Partition a Sequence

URL: https://www.4clojure.com/problem/54

Write a function which returns a sequence of lists of x items each. Lists of
less than x items should not be returned.

(= (__ 3 (range 9)) '((0 1 2) (3 4 5) (6 7 8)))
(= (__ 2 (range 8)) '((0 1) (2 3) (4 5) (6 7)))
(= (__ 3 (range 8)) '((0 1 2) (3 4 5)))

Special Restrictions

  • partition
  • partition-all


點我顯示/隱藏內容














055: Count Occurrences

URL: https://www.4clojure.com/problem/55

Write a function which returns a map containing the number of occurences of each
distinct item in a sequence.

(= (__ [1 1 2 3 2 1 1]) {1 4, 2 2, 3 1})
(= (__ [:b :a :b :a :b]) {:a 2, :b 3})
(= (__ '([1 2] [1 3] [1 3])) {[1 2] 1, [1 3] 2})

Special Restrictions

  • frequencies


點我顯示/隱藏內容














056: Find Distinct Items

URL: https://www.4clojure.com/problem/56

Write a function which removes the duplicates from a sequence. Order of the
items must be maintained.

(= (__ [1 2 1 3 1 2 4]) [1 2 3 4])
(= (__ [:a :a :b :b :c :c]) [:a :b :c])
(= (__ '([2 4] [1 2] [1 3] [1 3])) '([2 4] [1 2] [1 3]))
(= (__ (range 50)) (range 50))

Special Restrictions

  • distinct


點我顯示/隱藏內容














057: Simple Recursion

URL: https://www.4clojure.com/problem/57

A recursive function is a function which calls itself. This is one of the
fundamental techniques used in functional programming.

(= __ ((fn foo [x] (when (> x 0) (conj (foo (dec x)) x))) 5))


點我顯示/隱藏內容














058: Function Composition

URL: https://www.4clojure.com/problem/58

Write a function which allows you to create function compositions. The parameter
list should take a variable number of functions, and create a function that
applies them from right-to-left.

(= [3 2 1] ((__ rest reverse) [1 2 3 4]))
(= 5 ((__ (partial + 3) second) [1 2 3 4]))
(= true ((__ zero? #(mod % 8) +) 3 5 7 9))
(= "HELLO" ((__ #(.toUpperCase %) #(apply str %) take) 5 "hello world"))

Special Restrictions

  • comp


點我顯示/隱藏內容














059: Juxtaposition

URL: https://www.4clojure.com/problem/59


Take a set of functions and return a new function that takes a variable number
of arguments and returns a sequence containing the result of applying each
function left-to-right to the argument list.

(= [21 6 1] ((__ + max min) 2 3 5 1 6 4))
(= ["HELLO" 5] ((__ #(.toUpperCase %) count) "hello"))
(= [2 6 4]
   ((__ :a :c :b) {:a 2, :b 4, :c 6, :d 8 :e 10}))

Special Restrictions

  • juxt


點我顯示/隱藏內容














060: Sequence Reductions

URL: https://www.4clojure.com/problem/60

Write a function which behaves like reduce, but returns each intermediate value
of the reduction. Your function must accept either two or three arguments, and
the return sequence must be lazy.

(= (take 5 (__ + (range))) [0 1 3 6 10])
(= (__ conj [1] [2 3 4]) [[1] [1 2] [1 2 3] [1 2 3 4]])
(= (last (__ * 2 [3 4 5])) (reduce * 2 [3 4 5]) 120)

Special Restrictions

  • reductions


點我顯示/隱藏內容














061: Map Construction

URL: https://www.4clojure.com/problem/61

Write a function which takes a vector of keys and a vector of values and
constructs a map from them.

(= (__ [:a :b :c] [1 2 3]) {:a 1, :b 2, :c 3})
(= (__ [1 2 3 4] ["one" "two" "three"])
   {1 "one", 2 "two", 3 "three"})
(= (__ [:foo :bar] ["foo" "bar" "baz"]) 
   {:foo "foo", :bar "bar"})

Special Restrictions

  • zipmap


點我顯示/隱藏內容














062: Re-implement Iterate

URL: https://www.4clojure.com/problem/62

Given a side-effect free function f and an initial value x write a function
which returns an infinite lazy sequence of x, (f x), (f (f x)), (f (f (f x))),
etc.

(= (take 5 (__ #(* 2 %) 1)) [1 2 4 8 16])
(= (take 100 (__ inc 0)) (take 100 (range)))
(= (take 9 (__ #(inc (mod % 3)) 1)) (take 9 (cycle [1 2 3])))

Special Restrictions

  • iterate


點我顯示/隱藏內容














065: Black Box Testing

URL: https://www.4clojure.com/problem/65

Clojure has many sequence types, which act in subtly different ways. The core
functions typically convert them into a uniform "sequence" type and work with
them that way, but it can be important to understand the behavioral and
performance differences so that you know which kind is appropriate for your
application.

Write a function which takes a collection and returns one of :map, :set, :list,
or :vector - describing the type of collection it was given. You won't be
allowed to inspect their class or use the built-in predicates like list? - the
point is to poke at them and understand their behavior.

(= :map (__ {:a 1, :b 2}))
(= :list (__ (range (rand-int 20))))
(= :vector (__ [1 2 3 4 5 6]))
(= :set (__ #{10 (rand-int 5)}))
(= [:map :set :vector :list] (map __ [{} #{} [] ()]))

Special Restrictions

  • class
  • type
  • Class
  • vector?
  • sequential?
  • list?
  • seq?
  • map?
  • set?
  • instance?
  • getClass


點我顯示/隱藏內容














068: Recurring Theme

URL: https://www.4clojure.com/problem/68

Clojure only has one non-stack-consuming looping construct: recur. Either a
function or a loop can be used as the recursion point. Either way, recur rebinds
the bindings of the recursion point to the values it is passed. Recur must be
called from the tail-position, and calling it elsewhere will result in an error.

(= __
   (loop [x 5
          result []]
     (if (> x 0)
       (recur (dec x) (conj result (+ 2 x)))
       result)))


點我顯示/隱藏內容














070: Word Sorting

URL: https://www.4clojure.com/problem/70

Write a function that splits a sentence up into a sorted list of words.
Capitalization should not affect sort order and punctuation should be ignored.

(= (__  "Have a nice day.")
   ["a" "day" "Have" "nice"])
(= (__  "Clojure is a fun language!")
   ["a" "Clojure" "fun" "is" "language"])
(= (__  "Fools fall for foolish follies.")
   ["fall" "follies" "foolish" "Fools" "for"])


點我顯示/隱藏內容














071: Rearranging Code: ->

URL: https://www.4clojure.com/problem/71

The -> macro threads an expression x through a variable number of forms. First,
x is inserted as the second item in the first form, making a list of it if it is
not a list already. Then the first form is inserted as the second item in the
second form, making a list of that form if necessary. This process continues for
all the forms. Using -> can sometimes make your code more readable.

(= (__ (sort (rest (reverse [2 5 4 1 3 6]))))
 (-> [2 5 4 1 3 6] (reverse) (rest) (sort) (__))
 5)


點我顯示/隱藏內容














072: Rearranging Code: ->>

URL: https://www.4clojure.com/problem/72

The ->> macro threads an expression x through a variable number of forms. First,
x is inserted as the last item in the first form, making a list of it if it is
not a list already. Then the first form is inserted as the last item in the
second form, making a list of that form if necessary. This process continues for
all the forms. Using ->> can sometimes make your code more readable.

(= (__ (map inc (take 3 (drop 2 [2 5 4 1 3 6]))))
   (->> [2 5 4 1 3 6] (drop 2) (take 3) (map inc) (__))
   11)


點我顯示/隱藏內容














083: A Half-Truth

URL: https://www.4clojure.com/problem/83

Write a function which takes a variable number of booleans. Your function should
return true if some of the parameters are true, but not all of the parameters
are true. Otherwise your function should return false.

(= false (__ false false))
(= true (__ true false))
(= false (__ true))
(= true (__ false true false))
(= false (__ true true true))
(= true (__ true true true false))


點我顯示/隱藏內容














088: Symmetric Difference

URL: https://www.4clojure.com/problem/88

Write a function which returns the symmetric difference of two sets. The
symmetric difference is the set of items belonging to one but not both of the
two sets.

(= (__ #{1 2 3 4 5 6} #{1 3 5 7}) #{2 4 6 7})
(= (__ #{:a :b :c} #{}) #{:a :b :c})
(= (__ #{} #{4 5 6}) #{4 5 6})
(= (__ #{[1 2] [2 3]} #{[2 3] [3 4]}) #{[1 2] [3 4]})


點我顯示/隱藏內容














099: Product Digits

URL: https://www.4clojure.com/problem/99

Write a function which multiplies two numbers and returns the result as a
sequence of its digits.

(= (__ 1 1) [1])
(= (__ 99 9) [8 9 1])
(= (__ 999 99) [9 8 9 0 1])


點我顯示/隱藏內容














095: To Tree, or not to Tree

URL: https://www.4clojure.com/problem/95

Write a predicate which checks whether or not a given sequence represents a
binary tree. Each node in the tree must have a value, a left child, and a right
child.

(= (__ '(:a (:b nil nil) nil))
 true)
(= (__ '(:a (:b nil nil)))
 false)
(= (__ [1 nil [2 [3 nil nil] [4 nil nil]]])
 true)
(= (__ [1 [2 nil nil] [3 nil nil] [4 nil nil]])
 false)
(= (__ [1 [2 [3 [4 nil nil] nil] nil] nil])
 true)
(= (__ [1 [2 [3 [4 false nil] nil] nil] nil])
 false)
(= (__ '(:a nil ()))
 false)


點我顯示/隱藏內容














102: intoCamelCase

URL: https://www.4clojure.com/problem/102

When working with java, you often need to create an object with fieldsLikeThis,
but you'd rather work with a hashmap that has :keys-like-this until it's time to
convert. Write a function which takes lower-case hyphen-separated strings and
converts them to camel-case strings.

(= (__ "something") "something")
(= (__ "multi-word-key") "multiWordKey")
(= (__ "leaveMeAlone") "leaveMeAlone")


點我顯示/隱藏內容














105: Identify keys and values

URL: https://www.4clojure.com/problem/105

Given an input sequence of keywords and numbers, create a map such that each key
in the map is a keyword, and the value is a sequence of all the numbers (if any)
between it and the next keyword in the sequence.

(= {} (__ []))
(= {:a [1]} (__ [:a 1]))
(= {:a [1], :b [2]} (__ [:a 1, :b 2]))
(= {:a [1 2 3], :b [], :c [4]} (__ [:a 1 2 3 :b :c 4]))


點我顯示/隱藏內容














107: Simple closures

URL: https://www.4clojure.com/problem/107

Lexical scope and first-class functions are two of the most basic building
blocks of a functional language like Clojure. When you combine the two together,
you get something very powerful called lexical closures. With these, you can
exercise a great deal of control over the lifetime of your local bindings,
saving their values for use later, long after the code you're running now has
finished.

It can be hard to follow in the abstract, so let's build a simple closure. Given
a positive integer n, return a function (f x) which computes xn. Observe that
the effect of this is to preserve the value of n for use outside the scope in
which it is defined.

(= 256 ((__ 2) 16),
   ((__ 8) 2))
(= [1 8 27 64] (map (__ 3) [1 2 3 4]))
(= [1 2 4 8 16] (map #((__ %) 2) [0 1 2 3 4]))


點我顯示/隱藏內容














115: The Balance of N

URL: https://www.4clojure.com/problem/115

A balanced number is one whose component digits have the same sum on the left
and right halves of the number. Write a function which accepts an integer n, and
returns true iff n is balanced.

(= true (__ 11))
(= true (__ 121))
(= false (__ 123))
(= true (__ 0))
(= false (__ 88099))
(= true (__ 89098))
(= true (__ 89089))
(= (take 20 (filter __ (range)))
 [0 1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101])  


點我顯示/隱藏內容














126: Through the Looking Class

URL: https://www.4clojure.com/problem/126

Enter a value which satisfies the following:

(let [x __]
  (and (= (class x) x) x))


點我顯示/隱藏內容














134: A nil key

URL: https://www.4clojure.com/problem/134

Write a function which, given a key and map, returns true iff the map contains
an entry with that key and its value is nil.

(true?  (__ :a {:a nil :b 2}))
(false? (__ :b {:a nil :b 2}))
(false? (__ :c {:a nil :b 2}))


點我顯示/隱藏內容














143: dot product

URL: https://www.4clojure.com/problem/143

Create a function that computes the dot product of two sequences. You may assume
that the vectors will have the same length.

(= 0 (__ [0 1 0] [1 0 0]))
(= 3 (__ [1 1 1] [1 1 1]))
(= 32 (__ [1 2 3] [4 5 6]))
(= 256 (__ [2 5 6] [100 10 1]))


點我顯示/隱藏內容














145: For the win

URL: https://www.4clojure.com/problem/145

Clojure's for macro is a tremendously versatile mechanism for producing a
sequence based on some other sequence(s). It can take some time to understand
how to use it properly, but that investment will be paid back with clear,
concise sequence-wrangling later. With that in mind, read over these for
expressions and try to see how each of them produces the same result.

(= __ (for [x (range 40)
            :when (= 1 (rem x 4))]
        x))
(= __ (for [x (iterate #(+ 4 %) 0)
            :let [z (inc x)]
            :while (< z 40)]
        z))
(= __ (for [[x y] (partition 2 (range 20))]
      (+ x y)))


點我顯示/隱藏內容














156: Map Defaults

URL: https://www.4clojure.com/problem/156

When retrieving values from a map, you can specify default values in case the
key is not found:

(= 2 (:foo {:bar 0, :baz 1} 2))

However, what if you want the map itself to contain the default values? Write a
function which takes a default value and a sequence of keys and constructs a
map.

(= (__ 0 [:a :b :c]) {:a 0 :b 0 :c 0})
(= (__ "x" [1 2 3]) {1 "x" 2 "x" 3 "x"})
(= (__ [:a :b] [:foo :bar]) {:foo [:a :b] :bar [:a :b]})


點我顯示/隱藏內容














157: Indexing Sequences

URL: https://www.4clojure.com/problem/157

Transform a sequence into a sequence of pairs containing the original elements
along with their index.

(= (__ [:a :b :c]) [[:a 0] [:b 1] [:c 2]])
(= (__ [0 1 3]) '((0 0) (1 1) (3 2)))
(= (__ [[:foo] {:bar :baz}]) [[[:foo] 0] [{:bar :baz} 1]])


點我顯示/隱藏內容














161: Subset and Superset

URL: https://www.4clojure.com/problem/161

Set A is a subset of set B, or equivalently B is a superset of A, if A is
"contained" inside B. A and B may coincide.

(clojure.set/superset? __ #{2})
(clojure.set/subset? #{1} __)
(clojure.set/superset? __ #{1 2})
(clojure.set/subset? #{1 2} __)


點我顯示/隱藏內容














162: Logical falsity and truth

URL: https://www.4clojure.com/problem/162

In Clojure, only nil and false represent the values of logical falsity in
conditional tests - anything else is logical truth.

(= __ (if-not false 1 0))
(= __ (if-not nil 1 0))
(= __ (if true 1 0))
(= __ (if true 1 0))
(= __ (if [] 1 0))
(= __ (if [0] 1 0))
(= __ (if 0 1 0))
(= __ (if 1 1 0))


點我顯示/隱藏內容














166: Comparisons

URL: https://www.4clojure.com/problem/166

For any orderable data type it's possible to derive all of the basic comparison
operations (<, ≤, =, ≠, ≥, and >) from a single operation (any operator but = or
≠ will work). Write a function that takes three arguments, a less than operator
for the data and two items to compare. The function should return a keyword
describing the relationship between the two items. The keywords for the
relationship between x and y are as follows:

- x = y → :eq
- x > y → :gt
- x < y → :lt

(= :gt (__ < 5 1))
(= :eq (__ (fn [x y] (< (count x) (count y))) "pear" "plum"))
(= :lt (__ (fn [x y] (< (mod x 5) (mod y 5))) 21 3))
(= :gt (__ > 0 2))


點我顯示/隱藏內容