From the clojure docs, the function *':
Returns the product of nums. (*') returns 1. Supports arbitrary
precision. See also: *
I understand the use cases for arbitrary precision as explained in the example provided:
;; great so it gives the same results as *.
;; not quite check this out
(* 1234567890 9876543210)
;; ArithmeticException integer overflow
(*' 1234567890 9876543210)
;;=> 12193263111263526900N
However, the (*') returns 1 does not seem to have any use at all since you can just specify the explicit value. In the same example provided:
;; there is an implicit 1
(*')
;;=> 1
;; the implicit 1 comes into play
(*' 6)
;;=> 6
I had thought that perhaps it would be useful if the second argument is not defined, perhaps nil but:
(*' 6 nil)
Throws a NullPointerException.
Why would you use (*' 6) over 6 and (*') over 1?
* and *' are equivalent in this regard:
first of all - how would you otherwise handle the [] and [x] cases in *?
The source as it is written makes the most sense and AFAIK is mathematicly correct (identity value and such).
user=> (source *)
(defn *
"Returns the product of nums. (*) returns 1. Does not auto-promote
longs, will throw on overflow. See also: *'"
{:inline (nary-inline 'multiply 'unchecked_multiply)
:inline-arities >1?
:added "1.2"}
([] 1)
([x] (cast Number x))
([x y] (. clojure.lang.Numbers (multiply x y)))
([x y & more]
(reduce1 * (* x y) more)))
second, it makes it much more robust in cases other then manually writing (* 2 4). I wouldn't write (*) to mean 1.
Like the following - see the single element and emtpy vector on pos 3 and 4.
user=> (map (partial apply *) [[2 2] [2 3 2] [6] []])
(4 12 6 1)
Usually the 0-arity of a function is useful for a reduction or for applying to lists. For example:
(apply *' (range 5))
The multiplicative identity is one.
In terms of code and clojure, birdspider and Alejandro C. both answered about the (apply * []) case, in which it is indeed useful for (*) to equal 1.
However, I want to point out another case where (*) is useful. When passed to transduce as a reducing function.
transduce docstring:
clojure.core/transduce
[xform f coll]
[xform f init coll]
Added in 1.7
reduce with a transformation of f (xf). If init is not
supplied, (f) will be called to produce it. f should be a reducing
step function that accepts both 1 and 2 arguments, if it accepts
only 2 you can add the arity-1 with 'completing'. Returns the result
of applying (the transformed) xf to init and the first item in coll,
then applying xf to that result and the 2nd item, etc. If coll
contains no items, returns init and f is not called. Note that
certain transforms may inject or skip items.
The relevant part here is "If init is not supplied, (f) will be called to produce it.".
So using * as the rf without an init value will use an init value of 1. Which is probably what you want.
Similarly (+) ;=> 0 (additive identity) and (conj) ;=> [].
I think this is a neat example of things coming together nicely, as (*) was decided long before transducers were a thing.
The default value (*) => 1 is there to avoid a NullPointerException in the event you don't know how many items you want to multiply. That is:
=> (apply * [1 2 3])
6
=> (apply * [3])
3
=> (apply * [])
1
=> (apply * nil)
1
=> (* nil)
nil
=> (*)
1
Wow! I am surprised! I couldn't force a NPE even when I tried!
I still think this behavior provides a false sense of security. If I am expecting to multiply some numbers and one (or more) are missing, I'd rather have the default be to detect the error via an Exception. As it is, the default is for Clojure to cover-up the error by being "helpful" and trying to guess what I may have meant.
Update
I did finally generate an NPE:
> (* 5 nil) => NullPointerException clojure.lang.Numbers.ops (Numbers.java:1013)
I find this even more surprising. If (* nil) => nil, I would have bet money that (* 5 nil) => nil would also be true. Strange!
Related
I am doing is-prime? function that should return true if n is prime and false otherwise, it also should check to see if n is 1 or 2 and respond accordingly; if not, it should call no-divisors function. At the moment I get this output :
Can anyone see what's wrong, would be much appreciated
expected result is false,current false
expected result is true,current true
expected result is true,current false
expected result is false,current false
expected result is true,current false
no-divisors?
(->> (range 2 n)
(filter #(Divides % n))
empty? ))
(println (no-divisors? 4))
is-prime?
(defn is-prime? [n]
(and (< 1 n)
(not-any? (filter #(no-divisors? % n))
(range 2 n))))
(println "expected result is false,current"( is-prime? 1))
(println "expected result is true,current"( is-prime? 2))
(println "expected result is true,current" ( is-prime? 3))
(println "expected result is false,current"( is-prime? 4))
(println "expected result is true,current"( is-prime? 101))
If you format your code per Clojure conventions, it's pretty clear what the problem is:
(defn is-prime? [n]
(and (< 1 n)
(not-any? (filter #(no-divisors? % n))
(range 2 n))))
You're calling filter with a single argument which with return a transducer. Your call to not-any? then tries to treat that transducer as a predicate and since a transducer, given a single argument, returns a function -- and a function is "truthy" (not nil or false) then not-any? will return false per its definition.
The reason it returns true for 2 is that (range 2 2) is an empty sequence and not-any? returns true for an empty sequence without calling the predicate.
Some suggestions:
Several people have asked you for complete, well-formatted code. Provide
it. It will help you think clearly.
Use integer? and < to make sure you are testing a possible prime.
A prime is a number with no proper factors. Define it as such. You
are doing the work twice - Russian doll style.
It is easier to test your function on a range of possibles. I used (filter prime? (range -20 20)), yielding (2 3 5 7 11 13 17 19).
Name the function prime?. That's the Clojure convention. The initial is- is redundant. This may seem nit-picking, but simpler is clearer.
Best of luck!
I am new to Clojure and Lisp, but love it so far. I am currently trying to understand lazy-seq's and Clojure's ability to define infinite sequences. I have the following code:
(defn geometric
([] geometric 1)
([n] (cons n (lazy-seq (geometric (* n 1/2))))))
If I run:
(geometric)
in my REPL, it returns 1, as expected. However, if I run,
(take 10 (geometric))
I get the following error:
IllegalArgumentException Don't know how to create ISeq from:
java.lang.Long clojure.lang.RT.seqFrom
What I expect to get is:
(1 1/2 1/4 1/8 1/16 1/32 1/64 1/128 1/256 1/512)
Why am I getting this error? If I've understood correctly, one should be able to cons n to the lazy-sequence, and take should return the first ten values of the sequence, evaluated recursively.
One of my favorite functions: iterate takes a function f and a value x returning x, (f x), (f (f x), (f (f (f x))) etc.
Here is an elegant implementation with the same functionality:
(defn geometric []
(iterate #(/ % 2) 1))
Not a direct answer to your question but hopefully informative!
You have a small typo in your code:
(defn geometric
([] (geometric 1)) ;; notice the added parens around geometric 1
([n] (cons n (lazy-seq (geometric (* n 1/2))))))
Without this fix (geometric 1) was working because the implementation was to evaluate expression geometric (just a function value) which was discarded, then 1 expression was evaluated and returned as the function result (it was the last expression in this arity function body).
Now it works as expected:
(take 1 (geometric))
;; => (1)
(take 5 (geometric))
;; => (defn geometric
([] geometric 1)
([n] (cons n (lazy-seq (geometric (* n 1/2))))))
Notice that you cannot just call (geometric) safely in REPL as it will try to evaluate an infinite sequence.
Your problem is here:
([] geometric 1)
This expression means that, if geometric is called with no arguments, two things will happen:
The symbol geometric will be evaluated, which will result in the geometric function.
The number 1 will be returned.
What you probably meant was this:
([] (geometric 1))
This means that calling (geometric) is equivalent to calling (geometric 1). Your example will now work as expected:
(take 10 (geometric))
;=> (1 1/2 1/4 1/8 1/16 1/32 1/64 1/128 1/256 1/512)
(geometric) evaluates to the number 1, not to a sequence. (take 10 1) gives the same error that you're seeing now.
You should try running (take 10 (geometric 1)), since (geometric 1) will produce a sequence which can be supplied to the second argument of take.
When you call + with zero arguments
user=> (+)
0
I get 0 because it is invariant element to +. It works similar for *
user=> (*)
1
Why this does not work for - and / ?
user=> (-)
ArityException Wrong number of args (0) passed to: core/- clojure.lang.AFn.throwArity (AFn.java:429)
user=> (/)
ArityException Wrong number of args (0) passed to: core// clojure.lang.AFn.throwArity (AFn.java:429)
Note that - and / work differently when they are given a single argument: (- x 0) is different from (- x). The same for (/ x 1) and (/ x). The practical argument for + and * is that when your arguments may not be known beforehand, you can just apply or reduce over a list (possibly empty). The same is not true for division and negation, because you seldom need:
(apply / list)
You at least have one argument:
#(apply / (cons % list))
This is not authoritative, just a guess.
I guess the reason for this behaviour is the usage of + and * with aggregation functions: this allows to escape lots of boilerplate code in math formulas. Note the following:
(reduce + ()) => 0
(reduce * ()) => 1
the values are chosen not to affect the overall result of homogenous functions. Say you have to find the product of 10, 20, and all the items in some collection. That's what you do:
(defn product [items]
(* 10 20 (reduce * items)))
so when you have some items in a coll, it will work perfectly predictable:
(product [1 2 3]) => (* 10 20 (* 1 2 3))
and when the coll is empty you get the following:
(product []) => (* 10 20 1)
so it is exactly what you would expect.
Similar works for +
So why doesn't it work for - and / ?
i would say that they're not aggregation functions, traditionally they're opposite to aggregation. And in maths there are operators for + ( ∑ ) and * ( ∏ ), and no operators for - and /
Again, it's just a guess. Maybe there are some reasons that are much deeper.
the technical explanation would be:
if you check (source *),(source +) and (source -)
you will see that * and + can take 0 arguments while the - function will not.
(defn -
([x] (. clojure.lang.Numbers (minus x)))
([x y] (. clojure.lang.Numbers (minus x y)))
([x y & more]
(reduce1 - (- x y) more)))
Look at the function below. I want to pass a vector of factors and test if any of the elements in the vector is a factor of x. How do I do that?
(defn multiple?
"Takes a seq of factors, and returns true if x is multiple of any factor."
([x & factors] (for [e m] ))
([x factor] (= 0 (rem x factor))))
You could try using some and map:
(defn multiple? [x & factors]
(some zero? (map #(rem x %) factors)))
Also some returns nil if all tests fail, if you need it to actually return false, you could put a true? in there:
(defn multiple? [x & factors]
(true? (some zero? (map #(rem x %) factors))))
Note that some short-circuits and map is lazy, so multiple? stops as soon as a match is found. e.g. the following code tests against the sequence 1,2,3,4,....
=> (apply multiple? 10 (map inc (range)))
true
Obviously this computation can only terminate if multiple? doesn't test against every number in the sequence.
You can solve it only using some.
=> (defn multiple? [x factors]
(some #(zero? (rem x %)) factors))
#'user/multiple?
=> (= true (multiple? 10 [3 4]))
false
=> (= true (multiple? 10 [3 4 5 6]))
true
some will stop at the first factor.
Try this, using explicit tail recursion:
(defn multiple? [x factors]
"if any of the elements in the vector is a factor of x"
(loop [factors factors]
(cond (empty? factors) false
(zero? (rem x (first factors))) true
:else (recur (rest factors)))))
The advantages of the above solution include: it will stop as soon as it finds if any of the elements in the vector is a factor of x, without iterating over the whole vector; it's efficient and runs in constant space thanks to the use of tail recursion; and it returns directly a boolean result, no need to consider the case of returning nil. Use it like this:
(multiple? 10 [3 4])
=> false
(multiple? 10 [3 4 5 6])
=> true
If you want to obviate the need to explicitly pass a vector (for calling the procedure like this: (multiple? 10 3 4 5 6))) then simply add a & to the parameter list, just like it was in the question.
A more Clojurian way is to write a more general-purpose function: instead of answering true/false question it would return all factors of x. And because sequences are lazy it is almost as efficient if you want to find out if it's empty or not.
(defn factors [x & fs]
(for [f fs :when (zero? (rem x f))] f))
(factors 5 2 3 4)
=> ()
(factors 6 2 3 4)
=> (2 3)
then you can answer your original question by simply using empty?:
(empty? (factors 5 2 3 4))
=> true
(empty? (factors 6 2 3 4))
=> false
I want to be able to do the following pseudocode:
Pass in symbol a.
Pass in symbol b.
Pass in an expression using a and b
As I change the value of a and b, print the output of c at each moment.
Ideally, I would like the signature to look like:
(runner a b (+ a b))
but I'm not sure that I'm approaching this correctly... I've tried changing the function to
(runner 'a 'b (+ 'a 'b))
and this more complicated example:
(runner 'a 'b (+ (* 'a 'b) 'a))
but this does a + on 'a and 'b before stepping into runner.
Here's my first stab at some clojure:
(defn runner [a b c] (
(for [i (range 10)
j (range 10)] (println i j (c i j))
What concept of clojure am I missing?
Function arguments are always evaluated before the function is called. If you want to defer evaluation or represent some computation or code as an object, you have a few options:
Use a function (see my code below)
Use a macro and splice some code into some other code that the macro generates
Pass code as a quoted list, and eval it.
Using a function is what you want to do 99% of the time. 1% of the time, you'll want macros. You should never need eval unless you're generating code at runtime or doing very screwy things.
user> (defn runner [f]
(doseq [a (range 3)
b (range 3)]
(println a b (f a b))))
#'user/runner
user> (runner (fn [x y] (+ x y)))
0 0 0
0 1 1
0 2 2
1 0 1
1 1 2
1 2 3
2 0 2
2 1 3
2 2 4
This could also be written as (runner #(+ %1 %2) or even simply (runner +).
There is no need to pass "a" and "b" into the function as arguments. doseq and for introduce their own local, lexically scoped names for things. There's no reason they should use a and b; any name will do. It's the same for fn. I used x and y here because it doesn't matter.
I could've used a and b in the fn body as well, but they would have been a different a and b than the ones the doseq sees. You may want to read up on scope if this doesn't make sense.
I would make its signature be something like
(runner alen blen op)
example:
(runner 10 10 +)
I'm not really sure i'm answering the correct question. I'm thinking "if i pass a function a symbol instead of a value how can it use the value that symbol represents? is that close?
(def a 4)
(defn my-inc [x] (+ (eval x) 1))
user> (my-inc 'a)
5
I'm sure there is a more elegant way than using eval.
It isn't clear enough to me what you're trying to achieve, but the following is an answer to what I guess is your question:
user=> (declare a b)
#'user/b
user=> (defn runner [] (+ a b))
#'user/runner
user=> (binding [a 1 b 2] (runner))
3
user=> (binding [a 2 b 3] (runner))
5
Note that the above style is likely not what you ought to be doing. Ask a better question and we'll give you better answers.