What is the difference between Expressions and Procedures?

18 zobrazení
Preskočiť na prvú neprečítanú správu

janu

neprečítané,
21. 6. 2011, 1:09:4921. 6. 2011
komu: p2pu...@googlegroups.com
Hello All,

I am reviewing section 1.1 , subsection 1.1.6  Conditional Expressions and Predicates and I found these:
The first expression in each pair is a predicate -- that is, an expression whose value is interpreted as either true or false
The word predicate is used for procedures that return true or false, as well as for expressions that evaluate to true or false

Janus

Andres Raba

neprečítané,
21. 6. 2011, 5:12:0421. 6. 2011
komu: p2pu...@googlegroups.com
Hello Janus!

I think what is meant by a procedure is applying the operator to the operands in a combination. In a sense, the description of the procedure sits inside the operator which is the first symbol of a combination. Like + in (+ 5 6 7). Then again, not all combinations have the operator in the first position after the opening parenthesis. Like (1 2 3 4) which gives an error when evaluated, because 1 is not a procedure. The list evaluates to itself when quoted like this: '(1 2 3 4).

We could say that an expression is any primitive or combination that returns a value. Be it a single number, a list, a predicate returning true or false, etc. For example, this is an expression: 2.71. And this is compound expression: (sin 1.6), it applies the procedure sin to argument 1.6. This is also a compound expression: (* 4 (+ 7 19)). It applies the procedures * and + to its arguments.

Procedures are the recipes of evaluation you can define using construct like this:
(define (my-procedure arg1 arg2) <body containing expressions>)
So you get a named procedure which you then use, e.g.: (my-procedure 9.1 2.5)

You can have a procedure without a name! (That is introduced in the first lecture and in section 1.3.2.)

As I understand, a function is a procedure always returning the same value with the same arguments. No side effects and no internal state.

An expression returning true or false is for example (< x y). It applies the procedure "<" to x and y. This can be then used in (cond  ), (if  ) or other forms in the position of the predicate.

The above explanation might be too vague. And maybe I'm wrong. Others please elaborate on this.

Andres

Josmas Flores

neprečítané,
21. 6. 2011, 5:46:3621. 6. 2011
komu: p2pu...@googlegroups.com
Hi Janu,

Not sure I got the question... is it the difference between Expression
and Procedure or the difference between Expression and Predicate that
you are looking for?

If it is the later, you could say that all predicates are expressions,
but only the expressions that evaluate to true or false are
predicates. Predicates would be useful in conditionals, if expressions
(and loops, etc.).

If it is the former, a procedure could be seen as a set of
expressions. Andres' examples are good so won't go into that again :)

cheers,
Jos

janu

neprečítané,
21. 6. 2011, 15:47:0121. 6. 2011
komu: p2pu...@googlegroups.com
The former ... Thank you all. I like this a set of expressions and body containing expressions


Odpovedať všetkým
Odpovedať autorovi
Poslať ďalej
0 nových správ