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()); }...
This problem should be solved using the DrRacket software in Racket/Scheme language. Write a function (merge-sorter...
This problem should be solved using the DrRacket software in Racket/Scheme language. Write a function (merge-sorter L1) that takes list-of-integers L1 and returns all elements of L1 in sorted order. You must use a merge-sort technique that, in the recursive case, a) splits L1 into two approximately-equal-length lists, b) sorts those lists, and then c) merges the lists to obtain the result. See the following examples for clarificaton. (merge-sorter '(3 1 5 4 2) ---> (1 2 3 4 5)...
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.
create calculator standard using java language with OOP rule and interfaces.
create calculator standard using java language with OOP rule and interfaces.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT