Question

In: Computer Science

4 function calculator (op1 oper op2) using scheme language also include comments

4 function calculator (op1 oper op2) using scheme language

also include comments

Solutions

Expert Solution

Code:

;;(car List) returns first element of list i.e, operand1
;;(cadr List) returns second element of list i.e, operation
;;(caddr List) returns third element of list i.e, operand2

(define (calculator List) ;;Takes list as input which is of form (operand1 operation operand2)
(cond ((equal? '+ (cadr List)) (add (car List) (caddr List))) ;;If (cadr List) i.e, if second element of list is "+" then add 1st and 3rd operands
((equal? '- (cadr List)) (sub (car List) (caddr List))) ;;If (cadr List) i.e, if second element of list is "-" then subract 1st and 3rd operands
((equal? '* (cadr List)) (mul (car List) (caddr List))) ;;If (cadr List) i.e, if second element of list is "*" then add multiply and 3rd operands
((equal? '/ (cadr List)) (div (car List) (caddr List))) ;;If (cadr List) i.e, if second element of list is "/" then divide 1st and 3rd operands
(else 'not-valid-operation)))


(define (add a b) ;;Returns sum of two operands
(cond ((= a 0) b) ;;if b = 0 returns value of a
((= b 0) a) ;;if a = 0 returns value of b
(else (+ a b)))) ;;else add two operands

(define (sub a b) ;;Returns subraction of two operands
(cond ((= b 0) a) ;;if b = 0 returns value of a
(else (- a b)))) ;;else subract two operands

(define (mul a b) ;;Returns multiplication of two operands
(cond ((= a 0) 0) ;;if a = 0 returns 0
((= b 0) 0) ;;if b = 0 returns 0
(else (* a b)))) ;;else multiply two operands

(define (div a b) ;;Returns division of two operands
(cond ((= a 0) 0) ;;if a = 0 returns 0
((= b 0) 'infinity) ;;if b = 0 returns 'infinity
(else (/ a b)))) ;;else divide two operands

Snapshot of Code and Output:


Related Solutions

Pyramid of oranges, 2^x in each row, total for n rows using scheme language also include...
Pyramid of oranges, 2^x in each row, total for n rows using scheme language also include comments
Write a simple Calculator program using Scheme programming language. It should be able to do following...
Write a simple Calculator program using Scheme programming language. It should be able to do following operations: "+", "-", " * *, "/". the format when using the calculator function should be (calculator(operand1 operation operand2)) -> (calculator(2 + 5)) should give the output of 7.
Use Scheme Language Write a Scheme function that takes a list and returns a list identical...
Use Scheme Language Write a Scheme function that takes a list and returns a list identical to the parameter except the third element has been deleted. For example, (deleteitem '(a b c d e)) returns ‘(a b d e) ; (deleteitem '(a b (c d) e)) returns ‘(a b e).
Please use Java language in an easy way with comments! Thanks! Create a calculator that uses...
Please use Java language in an easy way with comments! Thanks! Create a calculator that uses an array of buttons for numbers from 0-9, the . for decimals, and for operators +, -, * ,/ and = as well as a JTextfield that displays the current value or the result. Use ActionListeners and LayoutManagers appropriately. The example below shows how to determine which button has been clicked. ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println(actionEvent.getActionCommand()); }...
Calculator in Assembly Language (Please add comments to explain your steps) Description: You are responsible to...
Calculator in Assembly Language (Please add comments to explain your steps) Description: You are responsible to implement several assembly functions to perform the simple arithmetic calculations for 2 64-bit integers. These functions will use the C function signature but the main logic within this function should be inline assembly code using the ASM block similar to the assembly example shown in class. Program Specification: 1. long mult ( long op1, long op2 ) - Can’t use the MUL/IMUL instructions, meaning...
Plot the function without using a calculator, as you will not have a calculator on the...
Plot the function without using a calculator, as you will not have a calculator on the exams. a. ? = 34 sin ?, from t = 0 to the end of the first cycle only. b. ? = 2sin3?, from t = 0 to the end of the second cycle only. c. ? = 2cos3?, from t = 0 to the end of the second cycle only. d. ? = 2sin??, from t = 0 to the end of the...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function name: main Purpose:                   main function In parameters: b,r,i Out paramters: trun,error,total,value Version:                   1.0 Author: ********************************************************************************/ void main() {    int i;//declaring this variable to get value for quitting or calaculating series    do {//do while loop to calaculate series until user quits        cout << "Enter 1 to evaluate the series." << endl;       ...
create scientific calculator using java language with OOP rule and interfaces.
create scientific calculator using java language with OOP rule and interfaces.
Please solve using graphing calculator. Include what is typed into calculator in your explaination. For example:...
Please solve using graphing calculator. Include what is typed into calculator in your explaination. For example: First, type the data under L1, then go to STAT --> TESTS -->TInverval. Inpt: Data (highlight the word Data) List: L1 Freq: 1 C-level .98 Calculate--> (3.54, 4.29) Show as much work as you can to support answer. In a Pew Research Center poll 745 randomly selected adults, 589 said that it is morally wrong not to report all income on tax returns. Assume...
Comments using # and "" "should also be included for reader to understand each part. Question:...
Comments using # and "" "should also be included for reader to understand each part. Question: 14. Write in python a script code which tells the user to enter an integer bigger than 5. Any input numbers which are smaller than 5 or 5 are not considered by this script. The code then should output all the prime numbers starting at 5, which are smaller than the number inputted by that user.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT