Question

Using the Scheme functional language AND do not use set! (or any procedure with !)

Question 5 [4 marks] The following program can be used to determine if a given interpreter is using applicative-order or norm

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answers:

a.

In an applicative-order evaluation, the function arguments are evaluated first before they passed to the function for the operation.

for example:
we have a function (define (add a b) (+ a b))
this statement needs to be evaluated, (add (add 1 2) 3)

It will be evaluated as follows

(add (add 1 2) 3)
(add (+ 1 2) 3)
(add 3 3)
(+ 3 3)
6

First the parameter (add 1 2) was evaluated and then the outer expression was evaluated.

In this program, the function is defined as below,
(define (test x y) (if (= x 0) x y))
The function calling,
(test 0 (/ 3 0))
The function test is called with two parameters,
parameter 1: 0
parameter 2: (/ 3 0)

On evaluation of this program (/ 3 0) will give error because division by zero is not possible. The correct evaluation of the statement (test 0 (/ 3 0)) is as below,

Expression Description
(test 0 (/ 3 0)) the function is called. Since it is an applicative order evaluation, all the parametes should be evaluated
(/ 0 3) this will be produce an error.

Hence an interpreter using applicative-order evlauation will throw an error for the given code. Since Scheme is an application-order evaluation, it will throw an error.

b.

In a normal-order evaluation, the evaluation of an expression happens from left to right and the function arguments will be not evaluated until they are necessary.

for example:
we have a function (define (add a b) a+b)
this statement needs to be evaluated, (add (add 1 2) 3)

It will be evaluated as follows

(add (add 1 2) 3)
(+ (add 1 2) 3)
(+ (+ 1 2) 3)
(+ 3 3)
6

Here the execution happened from left to right. First the outer add was evaluated and then the first parameter (add 1 2) was evaluated and at last the whole expression was evaluated.

For the given program,
(define (test x y) (if (= x 0) x y))

(test 0 (/ 3 0))

Evaluation:

Expression Description
(test 0 (/ 3 0))
(if (= 0 0) 0 (/ 3 0)) function test is evaluated
(if true 0 (/ 3 0)) (= 0 0) is evaluated to be true and now the if condition should be evaluated
0 since the condition is true, 0 is returned. The expression (/ 3 0) is never evaluated

In an interpreter using normal order evaluation, the code will given an output 0. There will be no errors.

Add a comment
Know the answer?
Add Answer to:
Using the Scheme functional language AND do not use set! (or any procedure with !) Question...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using scheme language, write the code for the following question 3 Define a procedure "Product" that...

    Using scheme language, write the code for the following question 3 Define a procedure "Product" that takes two parameters and returns the product of them. Then, call you procedure using [5 points] > (Product 1040) 03d

  • Consider the following pseudocode Xinteger procedure set x(n global integer) Xin procedure print x write integer(x)...

    Consider the following pseudocode Xinteger procedure set x(n global integer) Xin procedure print x write integer(x) procedure first set x(1) print x procedure second xinteger set x(2) print x set x(e) first() print x second print x What does this program print if the language uses static scoping? What does it print with dynamic scoping? Why?

  • Question 2 ARM Assembly Language (25 marks) An ARM instruction set summary is provided at the...

    Question 2 ARM Assembly Language (25 marks) An ARM instruction set summary is provided at the end of this paper. (5 marks) Explain the difference between eor and eors instruction. Use an example to show why both forms are useful. а. b. (5 marks) Explain using an example what the "Idr r3, [r7,#4]" instruction does. c. (10 marks) The following is the assembly language generated by a C compile type mystery, %function mystery: args 0, pretend = 0, frame =...

  • (8) (3 marks) Write BNF grammar rules for a language that is comprised only of sentences described as follows: symbol,...

    (8) (3 marks) Write BNF grammar rules for a language that is comprised only of sentences described as follows: symbol, fol symbols orjust onéx symbol, A sequence of one or more occurrences of an a lowed by either zero or morez followed by a sequence of one or more b symbols. (9) (1 mark) The following fragment of grammar describes the syntax of the exponentiation operator. What is the associativity ot the operator as detined here? factor | expr factor...

  • this is assembly language for x-86 processors using microsoft visual studio Create a procedure named FindThrees...

    this is assembly language for x-86 processors using microsoft visual studio Create a procedure named FindThrees that returns 1 if an array has three consecutive values of 3 somewhere in the array. Otherwise, return 0. The procedure’s input parameter list contains a pointer to the array and the array’s size. Use the PROC directive with a parameter list when declaring the procedure. Preserve all registers (except EAX) that are modified by the procedure. Write a test program that calls FindThrees...

  • QUESTION 1 How can you implement iteration (such as checking if there are any even elements...

    QUESTION 1 How can you implement iteration (such as checking if there are any even elements in a linked list) in a purely functional language, when there's no loop variable concept? O Recursion O Declaring type signatures O Writing out clear, concise comments Find a dragon to help you QUESTION 2 Explain what is happening below. Why are these results looking like this? Describe why it is happening in your own words. >(-10.2 10) 0.1999999999999993 >(-1.2.1) 0.19999999999999996 >(-1.41) 0.3999999999999999 >(-2.2...

  • In c++ #include <iostream > 2 #include <set> #înclude <functional» 5 using namespace std; 7 //your...

    In c++ #include <iostream > 2 #include <set> #înclude <functional» 5 using namespace std; 7 //your code 9 int main) 4 6 8 1e set <double, greater<double>>valuesA --1.1, 2.9, -2.3, 2.71 J: set <double, greater<double>> valuesB -3.14, 2.71, -3.88, 2.19; double value; cin > value; 12 13 14 15 16 17 18 19 /your code return ; Scenario Write a program that creates two sets (the sets are given in the code below) and asks the user for a number....

  • Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that...

    Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that it is tail-recursive. You may use the letrec form but do not use any additional forms and functions besides those we've talked about in class. (define filter (lambda (input-list func)     (cond ((null? input-list) '())           ((func (car input-list))            (cons (car input-list) (filter (cdr input-list) func)))           (else            (filter (cdr input-list) func))))) 2. Test your filter function on '(25 -22 44 56...

  • the coding language is just the basic c language and here is my first attempt at...

    the coding language is just the basic c language and here is my first attempt at this problem my problem is that if the user inputs a number that is equal to a number that has been entered previously the code will never end until the user enters a number that is bigger than any other number previously entered any help will be helpful Write a program to read-in a sequence of integers from the keyboard using scanf(). Your program...

  • Define black box and white box testing. What are the advantages of each approach? Why are...

    Define black box and white box testing. What are the advantages of each approach? Why are both necessary? Why Is it Important to be able to partition the test space into equivalence classes? For the following code fragment, describe 3 different test cases, and for each, describe the class of test cases It represents. The below code might or might not have any issue(s) public String print Triangle Type (Int x, y, z) {I* requires: The parameters are in ascending...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT