Reputation: 137
Doing some practice on recursion and map implementation to knock some rust off Clojure. I'm using lists only here so how am I trying to mix seq and non-seqable stuff?
(defn mapset
([operator operand] (mapset operator operand '()))
([operator operand finished-set]
(if (empty? operand)
'(finished-set)
(mapset operator (rest operand) (into finished-set (operator (first operand)))))))
REPL:
namespace.name> (mapset + '(1 3 4 6 5))
Execution error (IllegalArgumentException) at tester.core/mapset (core.clj:38).
Don't know how to create ISeq from: java.lang.Long
Upvotes: 0
Views: 1010
Reputation: 7576
Some errors:
'(finished-set)
with finished-set
into
adds elements from one collection to another, I think you're looking for conj
(that's the source of IllegalArgumentException
)conj
, you have to use []
as initial finished-set
, because conj
adds element to beginning of the list, but at the end of the vectorYour function, with minimal changes:
(defn mapset
([operator operand] (mapset operator operand []))
([operator operand finished-set]
(if (empty? operand)
finished-set
(mapset operator (rest operand) (conj finished-set (operator (first operand)))))))
Tests:
(mapset inc '(1 3 4 6 5))
; => [2 4 5 7 6]
(mapset dec '(1 3 4 6 5))
; => [0 2 3 5 4]
You can also write it with only two arguments, using cons
:
(defn mapset [operator operand]
(if (empty? operand)
'()
(cons (operator (first operand))
(mapset operator (rest operand)))))
Note that neither version is lazy, that would require adding lazy-seq
.
Upvotes: 1
Reputation: 29984
This error means you are are accidentally giving it an integer when it is expecting a sequence (list, vector, etc):
demo.core=> (first 5)
Execution error (IllegalArgumentException) at demo.core/eval2055 (form-init5218243389029929734.clj:1).
Don't know how to create ISeq from: java.lang.Long
Upvotes: 0