Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Do this: In your post, include the following: (A minimum of 155 words please and thanks.) - the code for your selected function (just the

Do this:

In your post, include the following: (A minimum of 155 words please and thanks.) - the code for your selected function (just the def part, not the whole file) - how you came up with your solution - what you learned about recursive functions - other insights you discovered

The code isn't part of 155 words the words are for what's underlined, italicized,red and in bold above.

This week's Examples folder has the following 2 related files: - lisplist.py - exampleLispList.py shows example uses of these functions.

Projects of our textbook ask students to provide definitions for the recursive functions: - insert(index, item, lispList) - equals(lispList1, lispList2) - removeAll(item, lispList) - lispMap(lambdaFunction, lispList) - lispFilter(booleanLambdaFunction, lispList)

You must code the definition for one of these recursive functions. To do this, create your code in your own new Python file. Like in the examples file exampleLispList.py: - Your file must import everything from lisplist. - Your file must include appropriate test code so you can make sure it works. Unlike in the examples file exampleLispList.py: - Your file must include the definition (def code) for your chosen function.

File: lisplist.py (Copyright)

Data and basic operations for Lips-like lists.

Designed to support functional programming concepts.

"""

THE_EMPTY_LIST = None

# Constructor function

def cons(data, lispList):

"""Returns a list whose head is data and whose tail is lispList."""

return Node(data, lispList)

# Accessor functions

def buildRange(lower, upper):

"""Returns a Lisp-like list containing the numbers from lower through upper."""

if (lower == upper):

return cons(lower, THE_EMPTY_LIST)

else:

return cons(lower, buildRange(lower + 1, upper))

def contains(item, lispList):

"""Returns True if item is in lispList or False otherwise."""

if isEmpty(lispList):

return False

if (item == first(lispList)):

return True

return contains(item, rest(lispList))

def first(lispList):

"""Returns the item at the head of lispList.

Precondition: lispList is not empty."""

return lispList.data

def get(index, lispList):

"""Returns the item at position index in lispList.

Precondition: 0 <= index < length(lispList)"""

if index == 0:

return first(lispList)

return get(index - 1, rest(lispList))

def isEmpty(lispList):

"""Returns True if lispList is empty or False otherwise."""

return lispList is THE_EMPTY_LIST

def length(lispList):

"""Returns the number of items in lispList."""

if isEmpty(lispList):

return 0

else:

return 1 + length(rest(lispList))

def remove(index, lispList):

"""Returns a new Lisp-like list with the item at index removed.

Note that lispList is unchanged.

Precondition: 0 <= index < length(lispList)"""

if (index == 0):

return rest(lispList)

else:

return cons(first(lispList), remove(index - 1, rest(lispList)))

def rest(lispList):

"""Returns a Lisp-like list of the items after the head of lispList.

Precondition: lispList is not empty."""

return lispList.next

# Mutator methods

# Lisp-like lists are immutable.

class Node(object):

"""Represents a singly linked node for use in Lisp-like lists."""

# Constructor

def __init__(self, data, next = None):

"""Sets the initial state of self."""

self.data = data

self.next = next

# Accessor methods

def __str__(self):

"""Returns the string representation of a nonempty Lisp-like list."""

def buildString(lispList):

if isEmpty(rest(lispList)):

return str(first(lispList))

else:

return str(first(lispList)) + " " + buildString(rest(lispList))

return "(" + buildString(self) + ")"

File: exampleLispList.py

This example illustrates using a Lisp-like list.

The following files must be in the same folder:

lisplist.py

"""

from lisplist import *

list1 = cons(22, THE_EMPTY_LIST)

print("list1 = cons(22, THE_EMPTY_LIST):")

print(list1)

print()

list2 = cons(11, cons(22, THE_EMPTY_LIST))

print("list2 = cons(11, cons(22, THE_EMPTY_LIST)):")

print(list2)

print()

list3 = buildRange(1, 7)

print("list3 = buildRange(1, 7):")

print(list3)

print()

list4 = remove(0, list3)

print("list4 = remove(0, list3):")

print(list4)

print()

print("list3:")

print(list3)

print()

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

Students also viewed these Databases questions