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
( 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:
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
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
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)
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
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
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
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
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.
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
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
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
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
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)
15 matches
Mail list logo