Clojure Feature Issue

everyone, I started working yesterday at the Euler project in Clojure, and I have a problem with one of my solutions that I cannot understand.

I have this function:

(defn find-max-palindrom-in-range [beg end]
  (reduce max
          (loop [n beg result []]
            (if (>= n end)
              result
              (recur (inc n)
                     (concat result
                             (filter #(is-palindrom? %)
                                     (map #(* n %) (range beg end)))))))))

I am trying to run it as follows:

(find-max-palindrom-in-range 100 1000)

and I get this exception:

java.lang.Integer cannot be cast to clojure.lang.IFn
  [Thrown class java.lang.ClassCastException]

which, I suppose, means that in some place I'm trying to evaluate Integer as a function. However, I cannot find this place, and what puzzles me even more is that everything works if I simply evaluate it as follows:

(reduce max
          (loop [n 100 result []]
            (if (>= n 1000)
              result
              (recur (inc n)
                     (concat result
                             (filter #(is-palindrom? %)
                                     (map #(* n %) (range 100 1000))))))))

(I just removed the function definition and replaced the parameters with constants)

Thank you in advance for your help and it is a pity that I probably embarrass you with an idiotic mistake on my part. Btw I am using Clojure 1.1 and the newest SLIME from ELPA.

: is-palindrom?. , .

(defn is-palindrom? [n]
  (loop [num (String/valueOf n)]
    (cond (not (= (first num) (last num))) false
          (<= (.length num) 1) true
          :else (recur (.substring num 1 (dec (.length num)))))))
+3
1

REPL (1.1). - , - ?

, , , . ( , , Project Euler, , , ):

  • is-palindrome? , filter. (filter is-palindrome? ...) .

  • , loop is-palindrome? . , (first last seq , last ). (require '[clojure.contrib.str-utils2 :as str]) (= num (str/reverse num)).

  • , concat - seq, , ( 4, ). , into.

  • , , , , . , ,

    (for [f (range 100 1000)
          s (range 100 1000)
          :when (<= f s)] ; avoid duplication of effort
      (* f s))
    
+6

Source: https://habr.com/ru/post/1746727/


All Articles