Question: I need desperate help with the following lisp problem. Please just write a function as described in the following instructions. You may write other functions
Vrite a function compr which takes a list and replaces each run longer than one with a sublist consisting of the count and the item which repeats. If the list has ublists, treat them as single items; don't change them or try to compress the ublists. If an item is not repeated, leave it unchanged, with one exception: an xisting item is a sublist beginning with a positive number, and does not repeat, hould be changed to a pair with a repeate count of 1. For instance, a sublist (3 -poons) which does not repeat should be converted to (1 (3 spoons)). The urpose of this rule is to avoid confusion so it is possible to create a reliable lecompress function which will always recreate the original list. Here are some xamples: Jlsp> (load "compr. lsp) compr lsp> (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 - ((3 (a bc)) (def) (1 (4 5 6)) (-3 -5 -7) 10 12) lsp> (compr '(thing thing (a b) (a b) (a b) (ba) thud wump ((2 thing) (3 (a b)) (ba) thud wumpus) 1sp> (compr '(oooh oooh oooh oooh oooh)) ((5 oooh)) Vrite a function compr which takes a list and replaces each run longer than one with a sublist consisting of the count and the item which repeats. If the list has ublists, treat them as single items; don't change them or try to compress the ublists. If an item is not repeated, leave it unchanged, with one exception: an xisting item is a sublist beginning with a positive number, and does not repeat, hould be changed to a pair with a repeate count of 1. For instance, a sublist (3 -poons) which does not repeat should be converted to (1 (3 spoons)). The urpose of this rule is to avoid confusion so it is possible to create a reliable lecompress function which will always recreate the original list. Here are some xamples: Jlsp> (load "compr. lsp) compr lsp> (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 - ((3 (a bc)) (def) (1 (4 5 6)) (-3 -5 -7) 10 12) lsp> (compr '(thing thing (a b) (a b) (a b) (ba) thud wump ((2 thing) (3 (a b)) (ba) thud wumpus) 1sp> (compr '(oooh oooh oooh oooh oooh)) ((5 oooh))
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
