Author Topic: List in Lists--(alanjt)  (Read 10610 times)

0 Members and 4 Guests are viewing this topic.

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: List in Lists--(alanjt)
« Reply #15 on: May 13, 2011, 04:02:57 AM »
Jeff thinks that
(CountAtoms nil) -> 0
Lee thinks that
(CountAtoms nil) -> 1

Who's right?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: List in Lists--(alanjt)
« Reply #16 on: May 13, 2011, 05:03:43 AM »

since NIL is an atom

(CountAtoms nil) should return 1


unless I misread something :)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: List in Lists--(alanjt)
« Reply #17 on: May 13, 2011, 05:56:50 AM »
Following the earlier examples, I offer this 'tail recursive' version:

Code: [Select]
(defun CountAtoms ( lst / _iterate )

  (defun _iterate ( lst acc )
    (cond
      ( (null lst) acc )
      ( (atom lst) (1+ acc) )
      ( (atom (car lst)) (_iterate (cdr lst) (1+ acc)) )
      ( (_iterate (cdr lst) (_iterate (car lst) acc) ) )
    )
  )

  (_iterate lst 0)
)

Question: Do all tail recursive functions require an accumulator?

Jeff H

  • Needs a day job
  • Posts: 6151
Re: List in Lists--(alanjt)
« Reply #18 on: May 13, 2011, 02:06:45 PM »
Jeff thinks that
(CountAtoms nil) -> 0
Lee thinks that
(CountAtoms nil) -> 1

Who's right?

I am not thinking much and have very little knowledge when it comes to lisp, so I would not assume I fully understand.

With that said I should just go back and edit the OP to say counting all atoms in a given list.
I think that would make the code I posted correct.





mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: List in Lists--(alanjt)
« Reply #19 on: May 13, 2011, 02:21:42 PM »
Jeff thinks that
(CountAtoms nil) -> 0
Lee thinks that
(CountAtoms nil) -> 1

Who's right?
There is no right or wrong, only ones and zeros, we have achieved a zen state as the answer is both one and zero. 
Be your Best


Michael Farrell
http://primeservicesglobal.com/

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
Re: List in Lists--(alanjt)
« Reply #20 on: May 13, 2011, 04:59:16 PM »
<snip>
Question: Do all tail recursive functions require an accumulator?
yes.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: List in Lists--(alanjt)
« Reply #21 on: May 13, 2011, 05:32:03 PM »
Quote
Question: Do all tail recursive functions require an accumulator?

I'd say: most of the time, yes.

But here's an example of tail recursion without accumulator:

Code: [Select]
(defun some (ele lst)
  (and lst
       (or (equal (car lst) ele)
   (some ele (cdr lst))
       )
  )
)
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: List in Lists--(alanjt)
« Reply #22 on: May 13, 2011, 06:27:37 PM »
Thanks gile, that's a good example :-)

I suppose many recursive functions which return a boolean value will be tail-recursive - your example made me think of this one:

Code: [Select]
(defun _member ( x lst )
  (vl-some
    (function
      (lambda ( e )
        (if (listp e) (_member x e) (equal x e))
      )
    )
    lst
  )
)

 :-)

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: List in Lists--(alanjt)
« Reply #23 on: May 14, 2011, 02:48:42 AM »
Quote
I suppose many recursive functions which return a boolean value will be tail-recursive

Not only Boolean returns, this one mimics the native 'member' function:

Code: [Select]
(defun memb (ele lst)
  (if lst
    (if (equal (car lst) ele)
      lst
      (memb ele (cdr lst))
    )
  )
)
Speaking English as a French Frog

Jeff H

  • Needs a day job
  • Posts: 6151
Re: List in Lists--(alanjt)
« Reply #24 on: May 14, 2011, 03:10:20 AM »
Is this a decent style or a good way of coding it? Mimicking problem solving and layout from site posted earlier
It is assumed or expected that a list will be the second parameter.

 
Code: [Select]
(defun My-Member (Expression lst)
  (cond
    ((null lst) nil)
    ((equal Expression (car lst)) lst)
    (T (My-Member Expression (cdr lst)))
  )
)   


or is if stament perfered?

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: List in Lists--(alanjt)
« Reply #25 on: May 14, 2011, 04:59:56 AM »
Quote
Is this a decent style or a good way of coding it?

Yes, and probably more readable than nested if statements.

Quote
It is assumed or expected that a list will be the second parameter.

With AutoLISP, the arguments order doesn't matter more than make the code more readable (or similar to built-in functions).
For example, most AutoLISP higher order functions (mapcar, vl-some, ...) require the function argument as first argument but vl-sort take it as last argument.

The choice of arguments order is more important in F# due to the pipelining ability (as List.map or List.exists, List.sortBy or List.sortWith requires the list as last argument).
Speaking English as a French Frog

Peter Jamtgaard

  • Guest
Re: List in Lists--(alanjt)
« Reply #26 on: May 14, 2011, 12:11:14 PM »
I noticed this thread over at augi lisp forum,

this was what I came up with...

I figure one of you guys must have already come up with this,

but if you haven't...

Peter

Code:

Code: [Select]
(defun RecurseList (lstSublist)
 (if (= (type lstSublist) 'LIST)
  (apply 'append (mapcar 'recurselist lstSublist))
  (list lstSublist) 
 )
)

(recurselist '((1 2 (3 4)) (5 (6 7)) "A" nil ((8 nil 9 (10 (11)))) (((A) B) C) (D (E (F (G))))))

returns

'(1 2 3 4 5 6 7 "A" nil 8 nil 9 10 11 A B C D E F G)

Recursion is really mind bending for me  :-P

Comments?

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: List in Lists--(alanjt)
« Reply #27 on: May 14, 2011, 12:27:43 PM »
Hi Peter,

Nice code but you'll have trouble with mapcar when faced with dotted pairs ;)

Lee

Peter Jamtgaard

  • Guest
Re: List in Lists--(alanjt)
« Reply #28 on: May 14, 2011, 05:57:59 PM »
It is definitely not as elegant, and one of the reasons I tend to avoid using dotted pairs in my code in lists of sublists

Any suggestions on a better way to determine if a dotted pair is a list would be interesting.

When ever I come up with any new code like that I can depend on the
members of the swamp to let some of the hot air out of my ego...

Nasty dotted pairs...   :-o

So

Code: [Select]
(defun DottedPairToList (dprItem)
 (if (and (= (type dprItem) 'LIST)
          (cdr dprItem)
          (/= (type (cdr dprItem)) 'LIST)
         
     )
  (list (car dprItem) (cdr dprItem))
  dprItem
 )
)

(defun RecurseList (lstSublist)
 (if (= (type lstSublist) 'LIST)
  (apply 'append (mapcar 'recurselist (dottedpairtolist lstSublist)))
  (list lstSublist) 
 )
)
Code: [Select]
(recurselist '((1 2 (3 4)) (5 (6 . 7)) "A" nil ((8 nil 9 (10 (11)))) (((A) B) C) (D (E (F (G . H))))))
 returns
'(1 2 3 4 5 6 7 "A" nil 8 nil 9 10 11 A B C D E F G H)

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: List in Lists--(alanjt)
« Reply #29 on: May 14, 2011, 06:04:21 PM »
Any suggestions on a better way to determine if a dotted pair is a list would be interesting.

Maybe?

Code: [Select]
(defun DottedPairToList ( dprItem )
  (if (vl-list-length dprItem)
    dprItem
    (list (car dprItem) (cdr dprItem))
  )
)