Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Lisp programming assignment a sublist consisting of the count and the item which repeats. If the list has sublists, treat them as single items; don't
Lisp programming assignment
a sublist consisting of the count and the item which repeats. If the list has sublists, treat them as single items; don't change them or try to compress the sublists. If an item is not repeated, leave it unchanged, with one exception: an existing item is a sublist beginning with a positive number, and does not repeat, it should be changed to a pair with a repeate count of 1. For instance, a sublist (3 spoons) which does not repeat should be converted to (1 (3 spoons)). The purpose of this rule is to avoid confusion so it is possible to create a reliable decompress function which will always recreate the original list. Here are some examples: 1sp (load "compr. lsp) compr 15p> (compr '(a bw goober goober ccc tom fred fred)) (a bw (2 goober) (3 c) tom (2 fred)) lsp> (compr '(nothing here to change)) (nothing here to change) 1sp (compr '((a bc) (a bc) (a bc) (def) (4 5 6) (-3 -5 -7) 10 12) (13 (a bc)) (def) (1 (4 5 6)) (-3 -5 -7) 10 12) 1sp> (compr '(thing thing (a b) (a b) (a b)(b a) thud wumpus)) ((2 thing) (3 (a b)) (ba) thud wumpus) 1sp(compr '(oooh oooh oooh oooh oooh)) ((5 oooh)) Use the equal? function to decide if adjacent items are the same. (Don't use -, since is only for comparing numbers.) Hints You are free to define any number helper functions in addition to compr, and you will probably need to. I defined a function pref which tells the number of times the first item in a list repeats, like this: 1sp(pref '(a a a bbccccc)) 1sp(pref '(a b c d)) 1sp> (pref '()) Another useful utility is the badly named built-in first-but function, which strips the first n items in a list, as: 1sp>(first-but 4 (a b c d e f g h i j)) (e f g h i il 1sp> (first-but 5 (a bc)) nil 1sp> (first-but l '(hi there)) (there) At cach step, if pref shows a number greater than one (or if the item is a sublist starting with a positive number) you can use first-but to remove that number of items and replace it with the correct sublist. a sublist consisting of the count and the item which repeats. If the list has sublists, treat them as single items; don't change them or try to compress the sublists. If an item is not repeated, leave it unchanged, with one exception: an existing item is a sublist beginning with a positive number, and does not repeat, it should be changed to a pair with a repeate count of 1. For instance, a sublist (3 spoons) which does not repeat should be converted to (1 (3 spoons)). The purpose of this rule is to avoid confusion so it is possible to create a reliable decompress function which will always recreate the original list. Here are some examples: 1sp (load "compr. lsp) compr 15p> (compr '(a bw goober goober ccc tom fred fred)) (a bw (2 goober) (3 c) tom (2 fred)) lsp> (compr '(nothing here to change)) (nothing here to change) 1sp (compr '((a bc) (a bc) (a bc) (def) (4 5 6) (-3 -5 -7) 10 12) (13 (a bc)) (def) (1 (4 5 6)) (-3 -5 -7) 10 12) 1sp> (compr '(thing thing (a b) (a b) (a b)(b a) thud wumpus)) ((2 thing) (3 (a b)) (ba) thud wumpus) 1sp(compr '(oooh oooh oooh oooh oooh)) ((5 oooh)) Use the equal? function to decide if adjacent items are the same. (Don't use -, since is only for comparing numbers.) Hints You are free to define any number helper functions in addition to compr, and you will probably need to. I defined a function pref which tells the number of times the first item in a list repeats, like this: 1sp(pref '(a a a bbccccc)) 1sp(pref '(a b c d)) 1sp> (pref '()) Another useful utility is the badly named built-in first-but function, which strips the first n items in a list, as: 1sp>(first-but 4 (a b c d e f g h i j)) (e f g h i il 1sp> (first-but 5 (a bc)) nil 1sp> (first-but l '(hi there)) (there) At cach step, if pref shows a number greater than one (or if the item is a sublist starting with a positive number) you can use first-but to remove that number of items and replace it with the correct sublistStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started