comparison with 3 arguments. Do I understand it right ?

2014-04-23 Thread Roelof Wobben
Hello, I do not understand why this work. I have to check if someone is between 12 and 20 years. So after some trail and error this seems to work (defn teen? [age] (if ( 12 age 20) true; false)) Is it right it stated 12 age 20 ? Roelof -- You received this message because

Re: comparison with 3 arguments. Do I understand it right ?

2014-04-23 Thread James Reeves
( a b c) is equivalent to (and ( a b) ( b c)), so yes, ( 12 age 20) is the same as 12 age 20. You could write your function more concisely as: (defn teen? [age] ( 12 age 20)) The if statement is unnecessary. - James On 23 April 2014 11:11, Roelof Wobben rwob...@hotmail.com wrote:

Re: comparison with 3 arguments. Do I understand it right ?

2014-04-23 Thread Roelof Wobben
Thanks, But something is not going right. When delete the if as you said and have this : (defn boolean [x] (or (false? x)(nil? x)) false; true) (defn abs [x] ( x 0) (* x -1); x) (defn teen? [age] ( 12 age 20) true; false) Then all tests fail and if I

Re: comparison with 3 arguments. Do I understand it right ?

2014-04-23 Thread Dave Della Costa
What James meant was that you should use it like so: = (defn teen? [age] ( 12 age 20)) #'user/teen? = (teen? 50) false = (teen? 10) false = (teen? 14) true = (if (teen? 14) yes is a teen no, not a teen) yes is a teen You don't need to set up these wrappers returning true and false. nil and

Re: comparison with 3 arguments. Do I understand it right ?

2014-04-23 Thread Roelof Wobben
Op woensdag 23 april 2014 13:23:17 UTC+2 schreef David Della Costa: What James meant was that you should use it like so: = (defn teen? [age] ( 12 age 20)) #'user/teen? = (teen? 50) false = (teen? 10) false = (teen? 14) true = (if (teen? 14) yes is a teen no, not a teen)

Re: comparison with 3 arguments. Do I understand it right ?

2014-04-23 Thread Marc Limotte
Roelof, Your abs definition doesn't have an (if), so those statements are just executed in order, and the last one 'x' is the return value for the whole thing. Probably what you want is: (defn abs [x] (if (pos? x) x (- x))) user= (abs -2) 2 user= (abs 2) 2 user= (abs 0) 0 marc On Wed, Apr

Re: comparison with 3 arguments. Do I understand it right ?

2014-04-23 Thread Tassilo Horn
Roelof Wobben rwob...@hotmail.com writes: The only thing which is failing now is this one ; (defn abs [x] ( x 0) (* x -1); x) I keep getting -2 as answer where it must be 2 Well, the function contains three forms where the results of the first two are ignored and only the

Re: comparison with 3 arguments. Do I understand it right ?

2014-04-23 Thread Roelof Wobben
Op woensdag 23 april 2014 14:26:14 UTC+2 schreef Tassilo Horn: Roelof Wobben rwo...@hotmail.com javascript: writes: The only thing which is failing now is this one ; (defn abs [x] ( x 0) (* x -1); x) I keep getting -2 as answer where it must be 2 Well, the

Re: comparison with 3 arguments. Do I understand it right ?

2014-04-23 Thread Roelof Wobben
Chips I thought I understand it But this does not work (defn divides? [divisor n] (mod n divisor)) I still get a cannot be cast error message. Roelof Op woensdag 23 april 2014 14:32:41 UTC+2 schreef Roelof Wobben: Op woensdag 23 april 2014 14:26:14 UTC+2 schreef Tassilo Horn: Roelof

Re: comparison with 3 arguments. Do I understand it right ?

2014-04-23 Thread Gary Verhaegen
It looks like you overgeneralized a bit. In Clojure, you should almost always replace (if expr true false) with just expr. However, it only applies when the last two elements are true and false in that order. (if expr1 expr2 expr3), as you had for abs, may not usually be simplified in that way.

Re: comparison with 3 arguments. Do I understand it right ?

2014-04-23 Thread Tassilo Horn
Roelof Wobben rwob...@hotmail.com writes: So if I understand everything well when I want true or false I do not need a if. No, when the last form of your function already returns true or false, e.g., ( 12 age 20), then you don't need to wrap that in an `if` which checks if the result is true

Re: comparison with 3 arguments. Do I understand it right ?

2014-04-23 Thread Tassilo Horn
Roelof Wobben rwob...@hotmail.com writes: Chips I thought I understand it But this does not work (defn divides? [divisor n] (mod n divisor)) It seems that `divides?` should be a predicate but your definition returns a number and no boolean. A number n is divisible by some divisor if

Re: comparison with 3 arguments. Do I understand it right ?

2014-04-23 Thread Roelof Wobben
Op woensdag 23 april 2014 14:55:39 UTC+2 schreef Tassilo Horn: Roelof Wobben rwo...@hotmail.com javascript: writes: Chips I thought I understand it But this does not work (defn divides? [divisor n] (mod n divisor)) It seems that `divides?` should be a predicate but your

Re: comparison with 3 arguments. Do I understand it right ?

2014-04-23 Thread Roelof Wobben
found the solution: (defn divides? [divisor n] (zero? (mod n divisor))) Roelof Op woensdag 23 april 2014 14:59:31 UTC+2 schreef Roelof Wobben: Op woensdag 23 april 2014 14:55:39 UTC+2 schreef Tassilo Horn: Roelof Wobben rwo...@hotmail.com writes: Chips I thought I understand it

Re: comparison with 3 arguments. Do I understand it right ?

2014-04-23 Thread Xu Hui Hui
You should get familiar with the syntax of if, comment and other basic things in Clojure: (defn boolean [x] (if (or (false? x) (nil? x)) false true)) ;; this is a comment ;; you should use Clojure's built-in boolean instead of define your own (defn abs [x] (if ( x 0) (- x)