Clojure: complicated iteration through a list?

I want to take the number, 20 and the list. '(1 2 3 4 5 6 7 8 9 10)and return a collection containing two values ​​for each value in the original list: the original value is paired with the remainder when immersing 20 by this value. It would be nice if the original values ​​were somehow tied to the residuals, so that I could easily get every number that created a specific remainder. Basically, I need some function func:

user=> (func 20 '(1 2 3 4 5 6 7 8 9 10))
'(:0 1, :0 2, :2 3,... :20 0)

I have an incredibly difficult time just figuring out how to go through the list. Can someone help me understand how to use list items myself, and then how to return an item that has been divided by 20, and if it returns the remainder?

My thought was to use something like this in a program that calculates square roots. If the numbers were clogged with the rest, I could request a collection to get all the numbers that share the input with a remainder of 0.


Here is my preliminary way around this.

;; My idea on the best way to find a square root is simple.
;; If I want to find the square root of n, divide n in half
;; Then divide our initial number (n) by all numbers in the range 0...n/2 
;; Separate out a list of results that only only return a remainder of 0.
;; Then test the results in a comparison to see if the elements of our returned 
;; list when squared are equal with the number we want to find a square root of.
;; First I'll develop a function that works with evens and then odds

(defn sqroot-range-high-end [input] (/ input 2))
(sqroot-range-high-end 36) ; 18

(defn make-sqrt-range [input] (range (sqroot-range-high-end (+ 1 input))))
(make-sqrt-range 36) ; '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18)

(defn zero-culler [input] (lazy-seq (remove zero? (make-sqrt-range input))))
(zero-culler 100) ; '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18)

(defn odd-culler [input] (lazy-seq (remove odd? (zero-culler input))))
(odd-culler 100) ; '(2 4 6 8 10 12 14 16 18)

;;the following is where I got stuck
;;I'm new to clojure and programming,
;;and am just trying to learn in a way that I understand

(defn remainder-culler [input]
  (if
    (/ input (first odd-culler (input)))
  input)
  (recur (lazy-seq (input)))
)

(remainder-culler 100)
+4
source share
2 answers

Welcome to Clojure!

Quick note: [1 2 3 4 5 6 7 8 9 10]is a vector, not a list.

When you say "keyed to", it makes me think that you are looking for something that returns a card.

Cards

Clojure cheatsheet . . - , , "" , . , Clojure.

group-by. , , , f .

> (group-by #(rem 20 %) [1 2 3 4 5 6 7 8 9 10])
{0 [1 2 4 5 10], 2 [3 6 9], 6 [7], 4 [8]}

, , , :

> (group-by #(keyword (str (rem 20 %))) [1 2 3 4 5 6 7 8 9 10])
{:0 [1 2 4 5 10], :2 [3 6 9], :6 [7], :4 [8]}

, . , (.. ).

/

, , , . : " - , , , 20, ?" for. , list comprehension.

(for [i [1 2 3 4 5 6 7 8 9 10]]
    (list (rem 20 i) i))

, , :

(for [i [1 2 3 4 5 6 7 8 9 10]]
    (list (keyword (str (rem 20 i))) i))

, , :

(map #(list (keyword (str (rem 20 %)))
            %)
     [1 2 3 4 5 6 7 8 9 10])

, , flatten.

, , . " , 0." filter, .

> (filter #(zero? (rem 20 %)) [1 2 3 4 5 6 7 8 9 10])
(1 2 4 5 10)

-. , .

, . , , , , . ! , . -, , , , .

+5
(map #(vector (rem 20 %) %) (range 1 21))
;; => ([0 1] [0 2] [2 3] ... [1 19] [0 20])

, ,

, , clojure.core/map

, 20,

2 . fn, clojure.core/map, , .

, rem

,

, , ({}) - .

clojure.core/range .

clojure.core/juxt, :

(map (juxt (partial rem 20) identity) (range 1 21))

, , :

(defn rem-denominator
  [n]
  (map (juxt (partial rem n) identity)
       (iterate inc 1)))

(take 5 (rem-denominator 20))
;; => ([0 1] [0 2] [2 3] [0 4] [0 5])
(take 20 (rem-denominator 20))
;; => ([0 1] [0 2] [2 3] ... [1 19] [0 20])
+2

All Articles