Debugging Part 4
As promised here is the solution to P09. I must confess I cheated, I looked up the solution because I couldn't sleep. I kept thinking about the problem in my sleep. So I looked it up to have a peace of mind. The key is knowing the difference between dotted list and proper list in lisp. In logo we can use fput function to simulate cons. So to create a list of a list you had to do something like
fput fput 5 [3 4 5] [[3 3 5] [4 5 6]]. Know how to do this properly was key to solving the problem.
Lesson learnt: you may have to look up the solution sometimes to avoid wasting so much time in one problem. The key is learning even from solution so that you can solve other problem. Sometimes the key may be little ideas like fput usage above.
fput fput 5 [3 4 5] [[3 3 5] [4 5 6]]. Know how to do this properly was key to solving the problem.
Lesson learnt: you may have to look up the solution sometimes to avoid wasting so much time in one problem. The key is learning even from solution so that you can solve other problem. Sometimes the key may be little ideas like fput usage above.
;P09 (**) Pack consecutive duplicates of list elements into sublists. ;If a list contains repeated elements they should be placed in separate sublists. ;Example: ;* (pack '(a a a a b c c a a d e e e e)) ;((A A A A) (B) (C C) (A A) (D) (E E E E)) to pack.run :list :item :group ;show :group ifelse emptyp :list [output fput fput :item :group []] [ifelse equalp first :list :item [output pack.run (butfirst :list) :item (fput :item :group )] [output fput (fput :item :group) pack.run butfirst :list first :list [] ]] end to pack :list output pack.run :list [] [] end show pack [a a a a b c c a a d e e e e]
Comments