Question

Explain this lisp recursive function that gets the length of a list. ; a recursive function...

Explain this lisp recursive function that gets the length of a list.

; a recursive function to find the lenght of the list
(defun len (list)
(if list
(1+ (len (cdr list)))
0))

What does the 1+ and the 0 at the end do?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

(defun len (list)
(if list
(1+ (len (cdr list))) ; Here if list is not empty we add 1 and call for next element of list cdr go for next element
0)) ; if list is empty we return 0

Example :

; a recursive function to find the lenght of the list
(defun len (list)
(if list
(1+ (len (cdr list)))
0))
(write (len (list 1 2 3)))

/* OUTPUT */

3

Now, len(list 3) become 1

so, 1 + len(list 3) = 2

which returns to len(list 2,3) becomes 2

so,

1 + len(list 2,3) becomes 3

now , len(list 1 2 3) becomes 3 and it returns from where it called

/* PLEASE UPVOTE */

Add a comment
Know the answer?
Add Answer to:
Explain this lisp recursive function that gets the length of a list. ; a recursive function...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lisp Scheme Write a procedure in Lisp Scheme called charflip that takes a string as a...

    Lisp Scheme Write a procedure in Lisp Scheme called charflip that takes a string as a parameter and returns a copy of this string, flipping the case of every character in an odd position (index 1, 3, 5, etc..). I suggest that you do this recursively. This means cons, car and cdr will come in handy, as will these functions: • char-upper-case? • char-downcase • char-upcase • list->string • string->list Here’s this function in action. scheme@(guile-user)> (charflip "hatburg") $16 =...

  • In Lisp programming language a 16. (20 pts.) Write a function that takes a list of...

    In Lisp programming language a 16. (20 pts.) Write a function that takes a list of 0's and l's and "toggles" each element. Your function should include lambda expression that knows how to flip an individual element, plus an applicative operator to do this to every element of the list. Example: 1 0) List (0 0 1 1 0 1) should be transformed into (1 1 0 0

  • solve with python Write a recursive function recStringWithLenCount() that takes a one-dimensional list of strings as...

    solve with python Write a recursive function recStringWithLenCount() that takes a one-dimensional list of strings as a parameter and returns the count of strings that have at least the length of second parameter passed into the function that are found in the list. Recall that you can determine whether an item is a string by writing type(item) == str. The only list functions you are allowed to use are len(), indexing (lst[i] for an integer i), or slicing (lst[i:j] for...

  • 1. Write a Lisp function called piece which takes a single argument x and implements the followin...

    1. Write a Lisp function called piece which takes a single argument x and implements the following piecewise linear function: piece(x) = x if 0 < x < 1 2. Write a Lisp function called intList which takes two integer arguments, a and b and returns the list of all integers from a to b (inclusive at both ends). For example, (intList 3 8) should return (345678) 1. Write a Lisp function called piece which takes a single argument x...

  • Define a recursive function SINGLETONS such that if e is any list of numbers and/or symbols...

    Define a recursive function SINGLETONS such that if e is any list of numbers and/or symbols then (SINGLETONS e) is a set that consists of all the atoms that occur just once in e. Examples: (SINGLETONS ( ))-> NIL (SINGLETONS '(G A B C B))-> (G A C) SINGLETONS '(H G ABCB))(HGAC) (SINGLETONS (AGABC B)) (G C) SINGLETONS '(B GA BCB))(GAC) [Hint: When e is nonempty, consider the case in which (car e) is a member of (cdr e), and...

  • Please help with all four questions regarding LISP Programming. Thank you. Please answer all questions with...

    Please help with all four questions regarding LISP Programming. Thank you. Please answer all questions with output plesase. LISP Programming Assignment It is a good idea to start this assignment early; Lisp programming, while not inherently difficult, often seem somewhat foreign at first, particularly when it comes to recursion and list manipulation. This assignment is loosely based on material by Dr. Henri Casanova.   Problem #1 Define a function that takes two arguments and returns the greater of the two. Problem...

  • Lisp program count-of (symbol list): Write a function named count-of that takes two parameters, a symbol...

    Lisp program count-of (symbol list): Write a function named count-of that takes two parameters, a symbol and a list. Count the number of instances of x in the list. Do not count the number of x in any sub-lists within the list. Test: count-of 'a '(a 'a(a c) d c a). Result: 2 trim-to (symbol list): Write a function named trim-to that takes a symbol and list as parameters. Return a new list starting from the first occurrence of the...

  • Python - Recursive to non-recursive quick sort. What I have at the moment is a recursive...

    Python - Recursive to non-recursive quick sort. What I have at the moment is a recursive quick sort, I need to make it non-recursive, any help is appreciated! def quickSort(list):     quickSortHelper(list, 0, len(list) - 1) def quickSortHelper(list, first, last):     if last > first:         pivotIndex = partition(list, first, last)         quickSortHelper(list, first, pivotIndex - 1)         quickSortHelper(list, pivotIndex + 1, last) # Partition list[first..last] def partition(list, first, last):     pivot = list[first] # Choose the first element as...

  • Python I think the answer is L=[5]. Please explain Which line changes the length of list...

    Python I think the answer is L=[5]. Please explain Which line changes the length of list L? L = [] L.append(4) L[0] = len(L) L = [5] OL = [5] OL= ] L.append(4) OL[0] = len(L)

  • *Program is in C* Write a recursive function to compute a^b for integers a and b....

    *Program is in C* Write a recursive function to compute a^b for integers a and b. For the recursive use the following equality a^b = a^b - 1 * a and a^0 = 1. What does the following recursive function do? int mystery(int a, int b) {if (b == 1) return (a); else return (a + mystery(a, b - 1));}

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT