Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write the following Lisp functions using the loops dotimes and dolist: Please write the functions using emacs-Lisp . a. A function called is-const that takes

Write the following Lisp functions using the loops dotimes and dolist: image text in transcribedPlease write the functions using emacs-Lispimage text in transcribed

. a. A function called is-const that takes one parameter that we can assume to be a list, and returns t if all the elements of the list are identical to each other (or what we could call, a constant list) and nil otherwise. Note that both an empty list and one that has only one element can be considered constant. Implementation suggestion: with the dolist loop. Examples of results: (is-const '0) ; t. (is-const '(a)) ; t (is-const '(1 2 3)) ; nil (is-const '(5 5 5 5)) ; t . b. A function called element-i taking two parameters. You can assume that the first one is a list and the second one a non-negative number. The function must return the i-th element of the list. We'll assume that the car of the list is the element number 0, the next one is number 1, and so on. If the index is higher than the length of the list, the function should return nil. Implementation suggestion: you can use either the loop dolist with a counter, or the loop dotimes in combination with the function pop. Recursion also works. Examples of results: (element-i '(1 2) 4) i nil (element-i '(a b c d e) 4) , (element-i '(31 2 6) 0); 3 'e c. A function called is-sorted that takes one parameter that is a list and returns true (t) or false (nil) based on whether the list is sorted in ascending order or not. Note that an empty list or a list with a single element are sorted by default. Implementation suggestion: using dolist where you store the previous element in a variable as you go along, or recursion. Examples of results: (is-sorted 'O ; t. (is-sorted '(10)) ; t. (is-sorted '(5 5 5)) ; t (is-sorted '(4 3 2 1)); nil (is-sorted '(25 9 12 14)); ti d. A function called reverse that reverses a list. The function takes one parameter that you can assume to be a list, and returns another list which is the reverse of the argument (you don't need to do the operation in place). You can use the function list that creates a list out of its arguments, or append that concatenates two lists and returns the result. Implementation suggestion: using dolist or recursion. Examples of results: (reverse '0) ;'( or nil (reverse-list '(2)) ; (20) (reverse-list '(1 2 3)) ; (3 2 1) Show a few examples of testing these functions. . a. A function called is-const that takes one parameter that we can assume to be a list, and returns t if all the elements of the list are identical to each other (or what we could call, a constant list) and nil otherwise. Note that both an empty list and one that has only one element can be considered constant. Implementation suggestion: with the dolist loop. Examples of results: (is-const '0) ; t. (is-const '(a)) ; t (is-const '(1 2 3)) ; nil (is-const '(5 5 5 5)) ; t . b. A function called element-i taking two parameters. You can assume that the first one is a list and the second one a non-negative number. The function must return the i-th element of the list. We'll assume that the car of the list is the element number 0, the next one is number 1, and so on. If the index is higher than the length of the list, the function should return nil. Implementation suggestion: you can use either the loop dolist with a counter, or the loop dotimes in combination with the function pop. Recursion also works. Examples of results: (element-i '(1 2) 4) i nil (element-i '(a b c d e) 4) , (element-i '(31 2 6) 0); 3 'e c. A function called is-sorted that takes one parameter that is a list and returns true (t) or false (nil) based on whether the list is sorted in ascending order or not. Note that an empty list or a list with a single element are sorted by default. Implementation suggestion: using dolist where you store the previous element in a variable as you go along, or recursion. Examples of results: (is-sorted 'O ; t. (is-sorted '(10)) ; t. (is-sorted '(5 5 5)) ; t (is-sorted '(4 3 2 1)); nil (is-sorted '(25 9 12 14)); ti d. A function called reverse that reverses a list. The function takes one parameter that you can assume to be a list, and returns another list which is the reverse of the argument (you don't need to do the operation in place). You can use the function list that creates a list out of its arguments, or append that concatenates two lists and returns the result. Implementation suggestion: using dolist or recursion. Examples of results: (reverse '0) ;'( or nil (reverse-list '(2)) ; (20) (reverse-list '(1 2 3)) ; (3 2 1) Show a few examples of testing these functions. . a. A function called is-const that takes one parameter that we can assume to be a list, and returns t if all the elements of the list are identical to each other (or what we could call, a constant list) and nil otherwise. Note that both an empty list and one that has only one element can be considered constant. Implementation suggestion: with the dolist loop. Examples of results: (is-const '0) ; t. (is-const '(a)) ; t (is-const '(1 2 3)) ; nil (is-const '(5 5 5 5)) ; t . b. A function called element-i taking two parameters. You can assume that the first one is a list and the second one a non-negative number. The function must return the i-th element of the list. We'll assume that the car of the list is the element number 0, the next one is number 1, and so on. If the index is higher than the length of the list, the function should return nil. Implementation suggestion: you can use either the loop dolist with a counter, or the loop dotimes in combination with the function pop. Recursion also works. Examples of results: (element-i '(1 2) 4) i nil (element-i '(a b c d e) 4) , (element-i '(31 2 6) 0); 3 'e c. A function called is-sorted that takes one parameter that is a list and returns true (t) or false (nil) based on whether the list is sorted in ascending order or not. Note that an empty list or a list with a single element are sorted by default. Implementation suggestion: using dolist where you store the previous element in a variable as you go along, or recursion. Examples of results: (is-sorted 'O ; t. (is-sorted '(10)) ; t. (is-sorted '(5 5 5)) ; t (is-sorted '(4 3 2 1)); nil (is-sorted '(25 9 12 14)); ti d. A function called reverse that reverses a list. The function takes one parameter that you can assume to be a list, and returns another list which is the reverse of the argument (you don't need to do the operation in place). You can use the function list that creates a list out of its arguments, or append that concatenates two lists and returns the result. Implementation suggestion: using dolist or recursion. Examples of results: (reverse '0) ;'( or nil (reverse-list '(2)) ; (20) (reverse-list '(1 2 3)) ; (3 2 1) Show a few examples of testing these functions. . a. A function called is-const that takes one parameter that we can assume to be a list, and returns t if all the elements of the list are identical to each other (or what we could call, a constant list) and nil otherwise. Note that both an empty list and one that has only one element can be considered constant. Implementation suggestion: with the dolist loop. Examples of results: (is-const '0) ; t. (is-const '(a)) ; t (is-const '(1 2 3)) ; nil (is-const '(5 5 5 5)) ; t . b. A function called element-i taking two parameters. You can assume that the first one is a list and the second one a non-negative number. The function must return the i-th element of the list. We'll assume that the car of the list is the element number 0, the next one is number 1, and so on. If the index is higher than the length of the list, the function should return nil. Implementation suggestion: you can use either the loop dolist with a counter, or the loop dotimes in combination with the function pop. Recursion also works. Examples of results: (element-i '(1 2) 4) i nil (element-i '(a b c d e) 4) , (element-i '(31 2 6) 0); 3 'e c. A function called is-sorted that takes one parameter that is a list and returns true (t) or false (nil) based on whether the list is sorted in ascending order or not. Note that an empty list or a list with a single element are sorted by default. Implementation suggestion: using dolist where you store the previous element in a variable as you go along, or recursion. Examples of results: (is-sorted 'O ; t. (is-sorted '(10)) ; t. (is-sorted '(5 5 5)) ; t (is-sorted '(4 3 2 1)); nil (is-sorted '(25 9 12 14)); ti d. A function called reverse that reverses a list. The function takes one parameter that you can assume to be a list, and returns another list which is the reverse of the argument (you don't need to do the operation in place). You can use the function list that creates a list out of its arguments, or append that concatenates two lists and returns the result. Implementation suggestion: using dolist or recursion. Examples of results: (reverse '0) ;'( or nil (reverse-list '(2)) ; (20) (reverse-list '(1 2 3)) ; (3 2 1) Show a few examples of testing these functions

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Structure Of The Relational Database Model

Authors: Jan Paredaens ,Paul De Bra ,Marc Gyssens ,Dirk Van Gucht

1st Edition

3642699588, 978-3642699580

More Books

Students also viewed these Databases questions

Question

Describe CaringWorks' hiring process.

Answered: 1 week ago

Question

Define the steps involved in the investment planning process.

Answered: 1 week ago