Author Topic: L-99: Ninety-Nine Lisp Problems  (Read 3930 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
L-99: Ninety-Nine Lisp Problems
« on: October 19, 2017, 03:31:50 PM »
Just found this while surfing the net and thought I'd share.

NOTE: these challenges are for another lisp language (so "answers" to these problems will do you only so much good). Also, keep in mind that other lisp based languages will have other tools available to them or the standard functions may return different items (work slightly different then how/what AutoLisp functions do).

http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html

Enjoy!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2125
  • class keyThumper<T>:ILazy<T>
Re: L-99: Ninety-Nine Lisp Problems
« Reply #1 on: October 19, 2017, 06:56:09 PM »

fun find John.
Could lead to some interesting translations.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: L-99: Ninety-Nine Lisp Problems
« Reply #2 on: October 20, 2017, 04:50:03 AM »
Cool stuff! :)

I think it would be fun to solve the first several problems with recursions (attempting yourself ofcourse, no hinting with the answers).
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: L-99: Ninety-Nine Lisp Problems
« Reply #3 on: October 20, 2017, 05:05:36 AM »
I just did sudoku-mr.lsp... Thanks for inspirations...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: L-99: Ninety-Nine Lisp Problems
« Reply #4 on: October 20, 2017, 05:56:24 AM »
I just did sudoku-mr.lsp... Thanks for inspirations...

Just leaving the crazy idea of creating ACAD_TABLE and populating with as sudoku table,
then wire that ACAD_TABLE with object reactor that constantly checks if the sudoku is solved - if so, say recolor the table in green. :D

But IMO even a sudoku generator is impressive enough.
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: L-99: Ninety-Nine Lisp Problems
« Reply #5 on: October 20, 2017, 06:09:16 AM »
I noticed that my algorithm isn't so sophisticated, so it can solve only simple ones like in John's link... But in newspaper that I bought there are less numbers specified and it goes into endless loop... Maybe I need extra logic to implement that I don't know... That's why perhaps there is competition in solving sudoku puzzles...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: L-99: Ninety-Nine Lisp Problems
« Reply #6 on: October 20, 2017, 07:38:21 AM »
Cool stuff! :)

I think it would be fun to solve the first several problems with recursions (attempting yourself ofcourse, no hinting with the answers).

Ha! I don't know Autolisp anymore (I moved on to other languages a long time ago). Besides, no one want's me back in the lisp world; I think they still have a few t-shirts with some of my "logic rants" on them.

Sounds like you are just learning about recursion. Recursion is very natural and powerful but you need to understand when and when not to use it. For example (step one), do you understand what is meant when AutoCAD returns something about a stack limit/error?

Anyways, I'll take you up on your challenge to do the first few myself. However, recursion certainly isn't needed for those simple ones. If I remember right, Autolisp has a "LAST" function so my first attempt will try to NOT use it (keep with the spirit of the test).

#1
Code - Auto/Visual Lisp: [Select]
  1. (defun my-last ( lst )
  2.   (car (reverse lst)) )
  3. ; (my-last '(a b c d))

#2
Code - Auto/Visual Lisp: [Select]
  1. (defun my-but-last ( lst )
  2.      (list (car (reverse lst))
  3.         (cadr (reverse lst)))) )
  4. ; (my-but-last '(a b c d))

...

#8 (almost; I wanted to `use recursion' as requested and this was kind of an attempt down the path of removing duplicates).
Code - Auto/Visual Lisp: [Select]
  1. (defun remove-duplicates ( lst )
  2.   (if (null lst)
  3.     nil
  4.     (cons
  5.       (if (member (car lst) (cdr lst))
  6.         'nil
  7.         (list (car lst)))
  8.       (remove-duplicates (cdr lst))) ))
  9.  
  10. (defun clean-list ( lst )
  11.   (apply 'append lst) )
  12. ; (clean-list (remove-duplicates '(a a a a b c c a a d e e e e)))

Well, that was fun. Thanks for the challenge. Take care.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: L-99: Ninety-Nine Lisp Problems
« Reply #7 on: October 20, 2017, 08:11:04 AM »
:) ...I built a recursive example for #14. Not at all efficient but it should be a fairly good recursive lesson if anyone's wiling to study it; It's kind of a different take on how the function MAPCAR works.

#14
Code - Auto/Visual Lisp: [Select]
  1. (defun map-car-iter ( proc lst)
  2.    (map-iter '() proc lst) )
  3.  
  4. (defun map-iter (sofar proc lis)
  5.    (if (null lis)
  6.        (reverse sofar)
  7.        (map-iter (cons (proc (car lis))
  8.                        sofar)
  9.                  proc
  10.                  (cdr lis))) )
  11.  
  12. (defun dupli (lst)
  13.          (map-car-iter
  14.            (lambda (x) (list x x))
  15.            lst)) )
  16. ; (dupli '(a b c c d))

Actually, I shouldn't say that this is how MAPCAR works because it's not entirely true (sorry). MAPCAR works slightly differently and would be written like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun map (proc items)
  2.  (if (null items)
  3.     nil
  4.     (cons (proc (car items))
  5.           (map proc (cdr items)))) )
  6.  

...Okay, I'm done now. Sorry.



EDIT: Fixed error in code.  "(if (atom lis)  --> (if (null lis)"
« Last Edit: October 20, 2017, 09:42:12 AM by John Kaul (Se7en) »
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: L-99: Ninety-Nine Lisp Problems
« Reply #8 on: October 20, 2017, 08:21:00 AM »
Sounds like you are just learning about recursion.

Not really, but its always like a challenge when you have to deal with it (especially if you try to avoid as much as you can LISP functions - say omit reverse, last).

Here are a few attempts of mine (sorry no time to play) :
Code - Auto/Visual Lisp: [Select]
  1. ; #1
  2. ; _$ (my-last nil '(a b c d)) -> D
  3. (defun my-last ( i L )
  4.   (if L (my-last (car L) (cdr L)) i)
  5. )
  6.  
  7. ; #2
  8. ; _$ (my-but-last nil nil '(a b c d)) -> (C D)
  9. (defun my-but-last ( a b L )
  10.   (if L (my-but-last (car L) (cadr L) (cddr L)) (list a b))
  11. )
  12.  
  13. ; #3
  14. ; _$ (element-at -1 '(a b c d e) 3) -> D
  15. (defun element-at ( n L i )
  16.   (cond
  17.     ( (not L) L)
  18.     ( (= (setq n (1+ n)) i) (car L) )
  19.     ( (element-at n (cdr L) i) )
  20.   )
  21. )
  22.  
  23. ; #4
  24. ; _$ (_length 0 '(a b c d e)) -> 5
  25. (defun _length ( i L )
  26.   (if L (_length (1+ i) (cdr L)) i)
  27. )
  28.  

Good stuff John!
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: L-99: Ninety-Nine Lisp Problems
« Reply #9 on: October 20, 2017, 08:38:56 AM »
ah, you're a bare-metal kind of person huh? I was too.

Nonetheless, at one time I was going to help translate some recursion lessons written by ElpanovEvgeniy (member here) written in Russian (not that I speak Russian though and he's damn near a genius so not sure what I was ever going to do) but you may want to see if that exists if you're at all interested in recursion. There was some good stuff in there (pick up more tips and whatnot).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ronjonp

  • Needs a day job
  • Posts: 7526
Re: L-99: Ninety-Nine Lisp Problems
« Reply #10 on: October 20, 2017, 09:05:20 AM »
Didn't anyone think of THIS (NSFW) from the title? :)


Here's some easy 'my-last':


Code - Auto/Visual Lisp: [Select]
  1. (defun my-last (l) (last l))
  2.  
  3.  
  4. (defun my-last (l) (nth (1- (length l)) l))
  5.  
  6.  
  7. (defun my-last (l) (car (reverse l)))




Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: L-99: Ninety-Nine Lisp Problems
« Reply #11 on: October 20, 2017, 11:28:30 AM »
Nonetheless, at one time I was going to help translate some recursion lessons written by ElpanovEvgeniy (member here) written in Russian (not that I speak Russian though and he's damn near a genius so not sure what I was ever going to do) but you may want to see if that exists if you're at all interested in recursion. There was some good stuff in there (pick up more tips and whatnot).

Russians do impressive stuff, the only bad thing is that its all written in russian.  :laugh:
I do speak and understand the language but I'm not comfortable with it, due my thoughts are in english.

BTW I just checked in that website what were the answers to my attempts and found some of them are more cleverly written - hopefuly that would flex my thinking for the future.
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: L-99: Ninety-Nine Lisp Problems
« Reply #12 on: October 20, 2017, 11:47:16 AM »
Ugh! Looks like those are in common lisp (yuck!). A lot of that language's features area not implemented in Autolisp. I suppose you could glean more from Scheme (closer--a little--to Autolisp).

With both common lisp and Scheme you have to be extra careful. Some of these guys write crazy code for seemingly obscure reasons not to mention that those languages are far more developed then Autolisp (Scheme at least is object orientated and .net-ified these days).

http://community.schemewiki.org/?ninety-nine-scheme-problems
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org