Question

In: Computer Science

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.

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

TASK: Using stack functions, write a program in C++ language that acts as a simple calculator,...
TASK: Using stack functions, write a program in C++ language that acts as a simple calculator, reading an infix algebraic expression with numbers and simple operations: +, -, *, / , (, and ). The program converts an infix expression into an equivalent postfix expression, and then evaluates the postfix expression, and then prints the result if input expression is correct otherwise prints error messages. Your program must interact with the user until the user quits.    REQUIREMENTS: - Your...
Using C as the programming language, Write a concurrent connection-oriented server that can do something simple...
Using C as the programming language, Write a concurrent connection-oriented server that can do something simple for connected clients. It should be able to carry out such processing for the client as many times as the client wants until the client indicates it wishes to end the session. The server should support multiple clients (use telnet as the client in this task). Compile and run the server program. Try and connect to it from multiple other hosts using telnet as...
GPA calculator in C language To understand the value of records in a programming language, write...
GPA calculator in C language To understand the value of records in a programming language, write a small program in a C-based language that uses an array of structs that store student information, including name, age, GPA as a float, and grade level as a string (e.g., “freshmen,” etc.). Note:Code and Output Screenshots
Design a simple calculator program using C++ which is able to: 1. ADD two decimal numbers...
Design a simple calculator program using C++ which is able to: 1. ADD two decimal numbers 2. MULTIPLY two decimal numbers. The following features must be incorporated in your program. 1. Must have an interface for the user to be able to either select the ADD option or MULTIPLY option or to EXIT the program. NOTE: If the user makes a wrong selection, a display must be shown to inform the user and the user must be given a choice...
Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class...
Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class with public and private members and multiple constructors.  Gain a better understanding of the building and using of classes and objects.  Practice problem solving using OOP. Overview You will implement a date and day of week calculator for the SELECTED calendar year. The calculator repeatedly reads in three numbers from the standard input that are interpreted as month, day of month, days...
Using C# programming language, Write a program that sort three numbers entered by the user using...
Using C# programming language, Write a program that sort three numbers entered by the user using only if and nested if statements. If for instance the user entered 5, 2, and 7; the program should display 2,5,7.
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that sorts the array below in ascending order.  LISP is a recursive language so the program will use recursion to sort. Since there will be no loops, you will not need the variables i, j, and temp, but still use the variable name array for the array to be sorted.             Array to be sorted is 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 The...
Using the C Programming language, write a program that sums an array of 50 elements. Next,...
Using the C Programming language, write a program that sums an array of 50 elements. Next, optimize the code using loop unrolling. Loop unrolling is a program transformation that reduces the number of iterations for a loop by increasing the number of elements computed on each iteration. Generate a graph of performance improvement. Tip: Figure 5.17 in the textbook provides an example of a graph depicting performance improvements associated with loop unrolling. Marking:- Optimize the code for an array of...
Time Calculator – Intro To Programming - JAVA Write a program that asks the user to...
Time Calculator – Intro To Programming - JAVA Write a program that asks the user to enter a number of seconds. • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or equal to...
Using c# programming language Write a program that mimics a lottery game. Have the user enter...
Using c# programming language Write a program that mimics a lottery game. Have the user enter 3 distinct numbers between 1 and 10 and match them with 3 distinct, randomly generated numbers between 1 and 10. If all the numbers match, then the user will earn $10, if 2 matches are recorded then the user will win $3, else the user will lose $5. Keep tab of the user earnings for, let say 5 rounds. The user will start with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT