Question

LISP [3 marks] Write a procedure called triangle that takes three numbers as arguments representing the...

LISP

  1. [3 marks] Write a procedure called triangle that takes three numbers as arguments representing the three side-lengths of a triangle. The procedure should classify the triangle and return one of the following strings: "Equilateral" "Isoceles" "Scalene".
    E.g. (triangle 2 3 4) → "Scalene"
    E.g. (triangle 2 3 2) → "Isoceles"

Homework Answers

Answer #1
LISP code for the above problem-
(defun read-side (side) (format t "Enter a value for side ~d: ~%" side) (read))

(defun triangle-p (a b c) (and (< a (+ b c)) (< b (+ a c)) (< c (+ a b))))

(defun equilateral-p (a b c) (= a b c))

(defun isoceles-p (a b c) (or (= a b) (= b c)))

(defun status-message (message a b c) (format t "The shape ~d,~d,~d ~a ~%" a b c message))

(defun classify-triangle (a b c)
    (cond ((not (triangle-p a b c)) nil)
              ((equilateral-p a b c) 'equilateral)
              ((isoceles-p a b c) 'isoceles)
                  (t 'triangle)))
(let* ((a (read-side 1)) (b (read-side 2)) (c (read-side 3)) (triangle-type (classify-triangle a b c)))
  (format t "classify triangle says ~a ~%" triangle-type)
  (cond ((eq 'equilateral triangle-type) (status-message "is an equilateral triangle." a b c))
        ((eq 'isoceles triangle-type) (status-message "is an isoceles triangle." a b c))
        ((eq 'triangle triangle-type) (status-message "is a "Scalene triangle." a b c))
        (t (status-message "is not a triangle." a b c))))


If the answer helped then please upvote.And for any queries, please comment.

Know the answer?
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
Similar Questions
SCHEME [2 marks] Define a procedure between? that takes 3 numbers representing a value (x) and...
SCHEME [2 marks] Define a procedure between? that takes 3 numbers representing a value (x) and a range (a,b) respectively . The procedure should return true if the first argument falls between the remaining two (inclusive), that is if x ∈ [a,b]. Note: the two arguments representing the range can be in either order ([a,b] or [b,a]). E.g. (between? 1 2 3) → #f E.g. (between? 7.5 5 10) → #t E.g. (between? 7 10 5) → #t
Write a basic c++ program to check whether a triangle is valid or not if the...
Write a basic c++ program to check whether a triangle is valid or not if the three sides are given: A triangle is valid if the sum of its two sides is greater than the third side. Let’s say that a, b, c is the sides of the triangle. A valid triangle must satisfy all these conditions: a + b > c a + c > b b + c > a (2 points) Generate random numbers 1-15 inclusive for...
Q.1. Write a program that accepts the lengths of three sides of a triangle as inputs....
Q.1. Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is an equilateral triangle. Q.2. Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is a right triangle. Recall from the Pythagorean theorem that in a right triangle, the square of one side equals the sum of the...
Write a template function maxn() that takes as its arguments an array of items of type...
Write a template function maxn() that takes as its arguments an array of items of type T and an integer representing the number of elements in the array and that returns the largest item in the array. The number of elements should take the default value of 10. The program should include a specialization that takes an array of strings as an argument and returns the longest string. (If there is a tie, the function should return the first one...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea,...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea, double sideb, double sidec) public static double area(double sidea, double sideb, double sidec) public static String triangletType(double a, double b, double c) The isValid method returns true if the sum of the two shorter sides is greater than the longest side. The lengths of the 3 sides of the triangle are sent to this method but you may NOT assume that they are sent...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea,...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea, double sideb, double sidec) public static double area(double sidea, double sideb, double sidec) public static String triangletType(double a, double b, double c) The isValid method returns true if the sum of the two shorter sides is greater than the longest side. The lengths of the 3 sides of the triangle are sent to this method but you may NOT assume that they are sent...
Part 3. Write a function called totalCost that takes three parameters (shipping cost, cost of one...
Part 3. Write a function called totalCost that takes three parameters (shipping cost, cost of one box, and number of boxes purchased) and returns the total cost which is computed using the following formula: shipping cost + (number of boxes purchased) * (cost of one box). Default values for the parameters are the following: shipping cost is 0, cost of one box is $4, number of boxes is 1. In the main function test your totalCost function with no arguments,...
Needs to be done in DrRacket Write a function dotProduct which takes two lists of numbers...
Needs to be done in DrRacket Write a function dotProduct which takes two lists of numbers representing vectors, and produces their dot product (or reports their incompatibility). E.g., (dotProduct '(1 2) '(3 4) ) -> 11 (dotProduct '(1 2 3) '(4 5 6)) -> 32 (dotProduct '(1 2 3) '(4 5)) -> *incompatible*
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
LANGUAGE: SCHEME R5RS Create a procedure called preceeding that will take a list of numbers as...
LANGUAGE: SCHEME R5RS Create a procedure called preceeding that will take a list of numbers as argument and determine the elements and indices of those elements that preceed a negative number in the given list. The returned information should be in the form of a pair of lists: ((values) . (indices)). E.g. (preceeding '(1 -2 3 4 -5 6 -7 -8)) → ((1 4 6 -7).(0 3 5 6)) ;note: drracket will display this as ((1 4 6 -7) 0...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT