Page 1 of 1

return list from a function

Posted: Sun Jun 05, 2005 12:47 am
by 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:

Code: Select all

# 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?

Posted: Sun Jun 05, 2005 8:59 am
by pjot
The least thing you can do is using 'push' to make things smaller:

Code: Select all

(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:

Code: Select all

(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

Posted: Sun Jun 05, 2005 3:30 pm
by Dmi
Thanks, Peter! That helps!