For Programmers: Free Programming Magazines  


Home > Archive > Scheme > January 2006 > if/cond query









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author if/cond query
H.

2006-01-23, 7:05 pm

> For example, we don't really need both IF and COND;
> either could be replaced by the other.


I thought this too until I read a footnote in SICP this wend which
seemed to imply that there was an edge case involving non-functional
programming in which they are not mutually exchangeable:

chapter 1, footnote 19:
"A minor difference between if and cond is that the <e> part of each
cond clause may be a sequence of expressions. If the corresponding <p>
is found to be true, the expressions <e> are evaluated in sequence and
the value of the final expression in the sequence is returned as the
value of the cond. In an if expression, however, the <consequent> and
<alternative> must be single expressions."

So, strictly speaking, they do not have a biconditional relationship,
in that
if you use nested ifs, you can always use cond instead. TRUE.
if you use cond, you can always use nested ifs instead. FALSE.

Of course, I'm throwing this out there to verify/negate it. imho, more
exact is: "If doing functional programming, then is and cond are
entirely fungible in their usage. If not doing functional programming,
if a subset of cond, i.e. if --> cond"

Jens Axel Søgaard

2006-01-23, 7:05 pm

H. wrote:

> chapter 1, footnote 19:
> "A minor difference between if and cond is that the <e> part of each
> cond clause may be a sequence of expressions. If the corresponding <p>
> is found to be true, the expressions <e> are evaluated in sequence and
> the value of the final expression in the sequence is returned as the
> value of the cond. In an if expression, however, the <consequent> and
> <alternative> must be single expressions."


But that single expression could be a begin.

(if (eq? 1 1)
(begin
(display "Hurray!")
(newline))
(begin
(display "Bummer!")
(newline)))

--
Jens Axel Søgaard




Lauri Alanko

2006-01-23, 7:05 pm

In article <1138041137.378479.211880@g14g2000cwa.googlegroups.com>,
H. <hbe123@gmail.com> wrote:
> So, strictly speaking, they do not have a biconditional relationship,
> in that
> if you use nested ifs, you can always use cond instead. TRUE.
> if you use cond, you can always use nested ifs instead. FALSE.


No. You can define cond using if, provided that you also have begin.
R5RS gives a sample definition that translates

((cond (test result1 result2 ...)
clause1 clause2 ...)

to

(if test
(begin result1 result2 ...)
(cond clause1 clause2 ...)))

And, if you forget about defines (which can be grouped with begin),
begin can also be defined using lambda:

(begin e) => e
(begin e1 e2 ...) => ((lambda (dummy) (begin e2 ...)) e1)

So. If you have lambda and if, you can define cond with them.


Lauri
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com