return list from a function

Started by Dmi, June 04, 2005, 05:47:07 PM

Previous topic - Next topic

Dmi

Hello!

I'm new to lisp and newLisp - sorry if that's quite stupid ;)

Can I return a list from a function as element-by-element, without collecting it to the temporary variable? And is it have a sense?



To enumerate network interfaces I wrote such thing:
# analog to: /sbin/ifconfig|awk '/^[[:alpha:]]/{print $1}'
(define (if-list)
  (set 'iflist (list))
  (dolist (str (exec "/sbin/ifconfig"))
    (if (find "^[[:alpha:]][[:alnum:]]+" str 0)
      (set 'iflist (append iflist (list $0)))))
  iflist)

Can I somehow remove "iflist" from code?

Possible there is a better way for such tasks?
WBR, Dmi

pjot

#1
The least thing you can do is using 'push' to make things smaller:



(define (if-list)
  (dolist (str (exec "/sbin/ifconfig"))
    (if (find "^[[:alpha:]][[:alnum:]]+" str 0)
      (push $0 iflist)))
iflist)


You could also use your original query which is even smaller:



(define (if-list)
  (dolist (str (exec "/sbin/ifconfig | awk '/^[[:alpha:]]/{print $1}'"))
      (push str iflist)
iflist))


Returning element-by-element means keeping track of the interface number to be returned, introducing an extra variable, I suppose.



Peter

Dmi

#2
Thanks, Peter! That helps!
WBR, Dmi