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 =...

  • Hi can any one help me with this programme to translating in F# language using visual...

    Hi can any one help me with this programme to translating in F# language using visual studio. //Include the required header files. #include //Define the main() function. int main() { //Declare the required variables. int x,y; //Prompt the user to enter the input. printf(“Enter the value of x : “); scanf(“%d”,&x); //Check the value of x and set the value of y. if(x>10) { y=x; } if(x<5) { y=x*2; } if(x==7) { y=x+10; } //Display the value of y. printf(“\nThe...

  • (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...

  • Using a programming language of your choice, write a complete and fully functional program that uses...

    Using a programming language of your choice, write a complete and fully functional program that uses reference and pointer types to swap two integer numbers. The two numbers are read in separately by the program’s user. Use a proper prompt for each number read in. a. Use one function that uses formal parameter reference type to swap the two numbers b. Use another function that uses formal parameter pointer type to swap the two numbers. c. In the main or...

  • 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...

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