Tahmoor Cyan
Tahmoor Cyan

Reputation: 129

Cond If And Or in Racket

Can someone please explain these functions for me in racket. I`m totally lost. Please help me with some examples. Thanks! I just cannot figure these functions out for the life of me.

Upvotes: 0

Views: 19991

Answers (3)

Sylwester
Sylwester

Reputation: 48745

NB: If you are familiar with non-lisp languages this is the answer for you. I'm not trying to explain them in other way than other code. Other answers do however so this is just a supplement.

None of those are functions, but special forms.

(if predicate 
    consequent 
    alternative)

is pretty much like Algol-dialects if

if( predicate )
{
   consequent
}
else
{
   alternative
}

Note that predicate, consequent and alternative can be anything from a complex expression to a simple value.

cond works more like an if, elseif..., else:

(cond (predicate1 consequent1)
      (predicaten consequentn)
      (else alternative))

and works like && in algol dialects. Thus false && print("Do it!") doesn't print anything since it short circuits.

(and #f (display "Do it!")) ; ==> #f (and does not display anything since first term was false)
(and 1 2 3) ; ==> 3 (3 is the last true value.  In Scheme everything except #f is true.)

or works like || in algol dialects. thus true || print("Do it!") doesn't print since first term was true.

(or 'mama (display "Do it")) ; ==> mama (first true value, does not print "do it")

Upvotes: 0

WorBlux
WorBlux

Reputation: 1413

If is the turnary operator. It has three arguments, unless the first argument has a value of #f it will return the value of the second argument, otherwise the value of the third argument.

OR accepts one or more arguments, evaluates them one at a time left to right, and returns the first value if finds that is not #f, and returns #f if no argument satisfies that condition.

AND accepts one or more arguments evaluate them one at a time left to right, if it finds one that is #f it returns #f, otherwise it returns the value of the last argument that it evaluates.

COND accepts one or more arguments, each with one or more sub arguments (2 is the usual number though). It evaluates each argument left to right one at a time by evaluating the first subargument. If it is not #f then it evaluates each of the rest of the sub-arguments (if any) in order and returns the value of the last argument. Otherwise it moves onto the next argument and evaluates it the same way. else is a special sytax within these sub-arguments that is always treated as if it is not #f.

And here argument is understood to mean any valid s-expression.

Upvotes: 0

Leberecht Reinhold
Leberecht Reinhold

Reputation: 263

First, the If:

(if (positive? 1) 1 -1)

Racket first evaluates if 1 is positive (which is the first expresion (positive? 1)). If it is, returns 1, else, return -1. This is equivalent to c-like languages doing:

if ( positive?(1))
  return 1
else
  return -1

The Cond is basically a if that has multiple options. The equivalent in C-like languages would be else-if

(cond [(first-condition) (what-to-do)]
      [(second-condition) (what-to-do)]
      [(third-condition) (you-get-the-idea)])

And and Or are just the logical operators, equivalent to && and || in C-like languages

(and true true) => true
(and true false) => false

(or true true) => true
(or true false) => true
(or false false) => false

Upvotes: 3

Related Questions