newLISP Fan Club

Forum => Anything else we might add? => Topic started by: Dmi on June 04, 2005, 05:47:07 PM

Title: return list from a function
Post by: Dmi on June 04, 2005, 05:47:07 PM
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?
Title:
Post by: pjot on June 05, 2005, 01:59:22 AM
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
Title:
Post by: Dmi on June 05, 2005, 08:30:48 AM
Thanks, Peter! That helps!