Question: Code and run the following examples in LISP. You must turn in your typed solution with the LISP functions you coded and the result of
Code and run the following examples in LISP. You must turn in your typed solution with the LISP functions you coded and the result of executing them.
Be prepared to present some of them in class by explaining the solutions to your fellow-students. Define a function CUBOID-VOLUME-NEW.
This function accepts as its argument a three element list containing sides of the cuboid. It returns the volume of the cuboid.
Construct a function WEIGHT-OF-PURCHASE that computes the sum of the weights from a "shopping list".
The shopping list has two sublists with the weight of the item as the second element. Examples: (WEIGHT-OF-PURCHASE '((MILK 1000 GRAMS) (SUGAR 500 GRAMS))) (Links to an external site.) 1500 (WEIGHT-OF-PURCHASE '((FLOUR 500 GRAMS) (CHEESE 100 GRAMS))) (Links to an external site.)
600 Define a function SHOPPING that extracts only the item to be purchased from a list containing 2 elements and their quantities.
Return the results as a list. Examples: (SHOPPING '((COFFEE 100 GRAMMES) (BREAD 750 GRAMMES))) (Links to an external site.) (COFFEE BREAD) (SHOPPING '((MILK 1000 GRAMMES) (SUGAR 1000 GRAMMES))) (Links to an external site.) (MILK SUGAR)
Define a function INSERT-BEHIND, which, in contrast to CONS, inserts a LISP expression into the back of a list instead of the front. Examples: (INSERT-BEHIND '(FOOTBALL) '(I LIKE)) (Links to an external site.) (I LIKE (FOOTBALL)) (INSERT-BEHIND 'DIFFICULT '(LISP IS NOT)) (Links to an external site.) (LISP IS NOT DIFFICULT)
Define a function REMOVE-LAST that removes the last element of a list. Define a predicate MORE-THAN-TWO-P that checks if a list has more than two elements.
Write a predicate SAFE-LESSP that has two arguments and firstly makes certain that it is dealing with two numbers. If the first number is smaller than the second, T should be returned, otherwise return NIL. Write a function WHAT-IS-IT that accepts an argument and returns what it is. In case of a number return NUMBER, in case of a symbol return S-ATOM, and in case of a list return LIST. If it is none of these, return DO-NOT-KNOW.
Examples: (WHAT-IS-IT "THIS IS A STRING") (Links to an external site.) DO-NOT-KNOW (WHAT-IS-IT 2) (Links to an external site.)
NUMBER Define a recursive function called CONTAINS-NUMBER-P. It goes through the list on the top level, element for element and tests whether at least one element is a number. If this is the case, T is output otherwise NIL.
Define a recursive function ALL-SAME-AS-ELEMENT that has two arguments, an atom and a list. ALL-SAME-AS-ELEMENT should test whether all the elements in the list are the same as the atom. Should this be the case, the value returned should be T, otherwise NIL.
Examples: (ALL-SAME-AS-ELEMENT 'X '(X X X)) (Links to an external site.) T (ALL-SAME-AS-ELEMENT 'Y '(X Y X)) (Links to an external site.) NIL
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
