CH 3 | Review Questions

¡Supera tus tareas y exámenes ahora con Quizwiz!

Write a Scheme list representation for the binary search tree shown in figure 3.11

((() a b) c (() f()) left sub tree: () a b root: c right sub tree: () f ()

3.8 (c) Write a function that collects integers from the user until a 0 is encountered and returns them in a list in the order they were input (Scheme or ML only).

(define (no_zeros) (let ((x (read))) ; Input in the list (if (= x 0) '() ; If any zeros it stops reading (cons x(no_zeros))))) ; Shows all numbers before the zero

3.8 (g) A Pythagorean triple is a tuple of integers (x, y, z) such that x * x + y + y = z * z. Write a function with a parameter n to print all Pythagorean triples such that 1 ≤ z ≤ y ≤ z ≤ n.

(define ptriple (lambda( x y) (list ( - (* y y) (* x x))(* ( x y) 2)(+ (* y y)(* x x))))) (define ftriple(lambda(num x y list-so-far) (let ((triple(ptrilple x y)))) (cond ((>=(car(cdr triple)) num )(reverse list-so-far)) (else(triple

3.8 (b) Write a function that computes the maximum and minimum of a list of integers.

;Find Maximum of a list (defin (findmax 1) (if (null? (cdr 1)) ;car will return the first element in the list ;cdr is for the second and other elements in the list but not the first (car 1) ;if the first element is smaller then, recursive call of findmax ;going to next element in the list and so on till alrgest is found (if (> (car 1) (findmax (cdr 1))) (car 1) (findmax (cdr 1))))) ;Find Minimum of a list ;same as for findmax just with finding min now (define (findmin 1) (if (null? (cdr 1)) (car 1) (if (> (car 1) (findmin (cdr 1))) (car 1) (findmin (cdr 1)))))

Which of the following functions are referentially transparent? Explain your reasoning. (a) The factorial function (b) A function that returns a number from the keyboard (c) A function p that counts the number of times it is called

For a function to be referentially transparent, its value must be dependent only on the values of its arguments and non-local variables. (a) This is referentially transparent because the factorial of a specific number doesn't change. (b) A function that returns a number from the keyboard is not referentially transparent for it is dependent on what key is pressed and what type of keyword is being used. (c) This is also not referentially transparent because the value returned will not always be the same since each time it is called it returns a number greater then the previous.

A function that returns the maximum value of a 0-ended list of integers input from a user is given by the following C function: int readMax() { int max, x; scanf("%d" ,&x); if (x != 0) { max = x; while (x != 0) { scanf("%d" ,&x); if (x > max) max = x; } return max; } } Rewrite this procedure as much as possible in functional style using tail recursion (the scanf procedure prevents a complete removal of variables).

{ int max, x; scanf("%d", &x); if (x == 0) //REturn the function with 0 value if user enters 0 i.e. end of list indicator { return x; } else } max = x; //Set the value of max to x x = readMaxRecursion(); //call function til 0 entered if(x > max)//when above call returns then returned value will be compared { max = x; //update value of max if returned value is greater } return max; } } void main() { printf("%d", readMaxRecursion()); }

the following C function computes the power ab, where a is a floating-point number and b is a (non-negative) integer: int readMax() { int max, x; scanf("%d" ,&x); if (x != 0) { max = x; while (x != 0) { scanf("%d" , &x); if (x > max) max = x; } return max; } } a) Rewrite the procedure in functional form b) Rewrite your answer to (a) using an accumulating parameter to make it tail recursive.

a) Rewrite procedure in functional form (define (power a b) (if(= b 0) 1 (* a(power a(- b 1))) b) Rewrite answer to (a) using an accumulating parameter to make it tail recursive (define (power a b result) (if(= b 0) (+ result 1) (* a (power a (- b 1) result))))

3.8 (a) Write a tail-recursive function compute the length of an arbitrary list.

define (my-length lst) (cond ((empty? lst) 0) (else (+1 (my-length (rest lst))))))

In Section 3.2.6 we defined the make-double higher-order function in Scheme. What functions are returned by (make-double -) and (make-double / )?

make-double take input a function and returns another function (make-double -) returns a function that will return 0, => ((make-double -) 6) = (- 6 6) = 0 (make-double / ) returns a function that will return 1, => ((make-double / ) 5) = ( / 5 5) = 1

Give applicative and normal order reductions for the following lambda expressions. State which steps use which conversions and which variables are bound and which are free. (a) (λx. ((λy.(* 2 y)) (+ x y))) y (b) (λx. λy. (x y)) (λz. (z y))

Normal order: This follows pass by name or substitute then evaluate. Applicative Order: this follows pass by value or evaluate then substitue Given Expression: (a) (λx. ((λy.(* 2 y)) (+ x y))) y Normal order implication: (λx.( (λy.( * 2 y) ) (+ x y))) y Step 1: Substitute the outermost function (x). (λy.(* 2 y))(+ y y) Step 2: Substitute the new outermost function (y). (* 2 (+ y y)) Applicative Order simplification: (λx.( (λy.( * 2 y) ) (+ x y))) y Step 1: Before Substituting the function of x, we need to evaluate its definition. This means dealing with the function of (y) (λx.( * 2 (+ x y) )) y Step 2: There is nothing more to be done with the definition or inputs to the x funtion, so we can simplify it. (* 2 (+ y y )) Given Expression: (b) (λx. λy. (x y)) (λz. (z y)) Simplification: An underscore, _, will be used to show where a function input would be if one is not provided. ( λx. λy.( x y) ___ ) (λz.(z y)___) Step 1: For both normal and applicative order, there is only one function with inputs that can be evaluated. Start with the x function .( λy.( λz.(z y) y) ___ ) In the above step, the 'y' which occurred first is a free variable. Step 2: The function of z now has an input, so it is still the only option to simplify. λy.(y y)___ In the above step, the 'y' which occurred last is a free variable. This is really confusing because we have one free and one bound variable. A free variable can never become bound, so we need to keep these separate. We can rename bound variables, so it is easy to clarify this. λw.(w y)


Conjuntos de estudio relacionados

Магдебурзьке право

View Set

PSYCHOLOGY CHAPTER 14: Stress, Lifestyle, and Health

View Set

Board Review Anatomic Sciences and Physiology

View Set

CPR Checking an unconscious person

View Set

C168 Critical Thinking and Logic

View Set

issues and wellness tri 3 nutrition

View Set

Mental Health Unit I: questions from Quizlet and NCLEX questions from Online Resources

View Set