Lisp with-in a Lisp

Discussion in 'AutoCAD' started by ~DEVO~, Dec 13, 2004.

  1. ~DEVO~

    ~DEVO~ Guest

    I am trying to run a lisp in a lisp .... I wrote it as such:

    (setq lst2 (sort lst2))

    It should run "lst2" thru a little lisp to obtain a new "lst2" value.
    For some reason it is not working correctly. Is the codeing bad?

    Example: if lst2 = 1 2 3 4
    lst2 goes to the lisp labeled "sort" and should returm 4 3 2 1

    here is the sort lisp...

    ( defun SORT (Lst / NewList temp big TempList y x limit)
    ( setq x (length Lst) )
    ( while ( /= x 1)
    ( setq pos 0 )
    ( setq big (nth pos Lst) )
    ( setq i 1 )
    ( setq limit (- x 1 ) )
    ( repeat limit
    ( setq temp (nth i Lst) )
    ( if (> (atoi (car temp)) (atoi (car big)) )
    ( setq big temp pos i )
    )
    ( setq i (+ i 1) )
    )

    ( setq NewList ( cons big NewList ) )
    ( if ( = pos 0 )
    ( setq Lst (cdr Lst) )
    ( progn
    ( setq TempList (list (car Lst)) )
    ( setq y (length Lst))
    ( setq y (- y 1) i 0 )
    ( while (< i y )
    ( setq i (+ 1 i) )
    ( if ( /= pos i )
    ( setq TempList (cons (nth i Lst) TempList))
    )
    )
    ( setq Lst TempList )
    )
    )
    ( setq x (- x 1) )
    )
    (setq NewList ( cons (car Lst) NewList ) )
    (setq Lst NewList )
    )
     
    ~DEVO~, Dec 13, 2004
    #1
  2. ~DEVO~

    krispy Guest

    i get this error when running your code:
    **ERROR 2: bad argument type: consp 3...
    meaning you are passing the wrong type of variable to one of the methods... on looking a little deeper I found where this occurs:
    ( if (> (atoi (car temp)) (atoi (car big)) )
    more specifically: (car temp) and (car big) when you called (nth pos Lst) you extracted the element at position pos, and then in your if statement you are saying you want to extract the first element of that element. Obviously you can only extract the first element from a list. hence the error.
    When I fixed this there was another error, here: (atoi temp) you are now trying to convert a string to an integer, but it is already an integer.
    here is the correction (with your line commented out):
    ;( if (> (atoi (car temp)) (atoi (car big)) )
    (if (> temp big)
    oh... and if you want it to sort in descending order as in your example it should be like this:
    (if (> big temp)
     
    krispy, Dec 13, 2004
    #2
  3. ~DEVO~

    ~DEVO~ Guest

    I see.. I am such a rookie.
    To expand on my question: is there a down and dirty way to have the sort to return 3142 instead of 4321? I figure if I learn to manipulate this kind of lisp, it would really help me out on other lisp routines that I have.

    Thank you for your time and effort for helping me out.
     
    ~DEVO~, Dec 13, 2004
    #3
  4. ~DEVO~

    Tom Smith Guest

    To expand on my question: is there a down and dirty way to have the sort
    to return 3142 instead of 4321?

    What are you trying to do? What is the sort criterion? First you posted an
    example where the the list was simply reversed, to descending order. I don't
    see any pattern in the order shown above, and can't deduce what your goal
    is.
     
    Tom Smith, Dec 13, 2004
    #4
  5. ~DEVO~

    ~DEVO~ Guest

    Sorry for not explaining myself. The SORT lisp helps put info in a desired order.

    Example: Curve data....... Tangent, Length, curve number, delta, and radius.

    Because I had to change my blocks labels around, my table lisp is not working right. it reads the tangent and writes it to the length label and the delta gets written to the tangent label , etc....

    So I would like to get this problem fixed. If you would like a copy of my original lisp I have no problem e-mailing them to you for evaluation or use. I just need to get this problem solved.

    Thank you for your quick reply
     
    ~DEVO~, Dec 13, 2004
    #5
  6. ~DEVO~

    Tom Smith Guest

    radius.

    That's more understandable. However I probably won't have time to study your
    lisp in detail to suggest changes, but others might do this if you post it.

    As a general observation, though, this is the kind of thing that makes you
    realize the value of an association list. The data doesn't depend on the
    list order to be meaningful, and you can make use of built-in functions like
    assoc and subst to work with the list. Nearly every time I've written
    something that depends on a list of data in a certain order, I've regretted
    it. The association list format is more forgiving, more easily upgradeable,
    and more natural to to Acad's data structure.

    If you need to rework your lisp significantly, maybe this would be a good
    time to make that change. For instance, your curve data might take the form

    (setq curve_data '(( "tangent" . <n>) ("length" . <n>) ("curvenumber" . <n>)
    etc.))

    Then the length parameter is simply (cdr (assoc "length" curve_data)). The
    order of the list doesn't matter. It's also self-documenting, since the
    group code labels identify what the numbers mean.

    Just a thought.
     
    Tom Smith, Dec 13, 2004
    #6
  7. ~DEVO~

    ~DEVO~ Guest

    I never thought of it that way.... I'll TRY IT AT HOME AND I'll KEEP YOU UPDATED...... thanks A LOT
     
    ~DEVO~, Dec 13, 2004
    #7
  8. ~DEVO~

    Jeff Mishler Guest

    The SORT is not working due to the line where it's called has been commented
    out:

    (setq lst2 (append lst2 (list lst1)))
    ) ; repeat
    ;(setq lst2 (sort lst2)) ;<<<<<-----Remove the semi-colon form the
    beginning of this line
    ;(princ lst2)
    (setq vals-l (mapcar '(lambda (x) (car x)) lst2))

    I am unable to test at this time, but that should get you back on track.
     
    Jeff Mishler, Dec 16, 2004
    #8
  9. ~DEVO~

    ~DEVO~ Guest

    Tried it, but no luck. It seems to be sorting by either the "length" label or the "radius" label, not by the curve label.

    Thanks for any help you can give me.
     
    ~DEVO~, Dec 17, 2004
    #9
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.