PLC: Assignment #4 (Sample Solution)

Question 1

lindenmayer.scm

Question 2

A new-if can be easily defined as:

(define new-if
   (lambda (condition then-part else-part)
      (cond
         (condition then-part)
         (else else-part)
      )
   )
)

This works good for examples like

guile> (new-if (< 2 3) 10 20)
10
guile> (new-if (> 2 3) 10 20)
20

This changes, however, in case of side effects:

guile> (new-if (< 2 3) (display "then") (display "else"))
thenelseguile>

The point is that parameters are in Scheme evaluated before they are passed to a function. This means that then-part and else-part are both evaluated long before condition is considered to return either then-part or else-part. This would be fatal in case of recursive functions:

(define fact (lambda (n) (new-if (> n 0) (* n (fact (- n 1))) 1)))

Question 3

If the special form of Boolean values is not considered, i.e. that they select the first or second argument in dependence of being true or false, following construct that uses If-then-else seems to be natural:

Lx.Ly.(((If-then-else)x)(((If-then-else)y)True)False)False

If the selecting property of booleans is considered, however, this may be shortened to:

Lx.Ly.((x)((y)True)False)False

That was so far my sample solution but following proposal by Patrick North during the lecture on Monday was better:

Lx.Ly.((x)y)False

Sven Wolf contributed an even shorter variant:

Lx.Ly.((x)y)x

which has also a nice counter-part for its or:

Lx.Ly.((x)x)y

Andreas F. Borchert, April 9, 2002