return list from a function

For the Compleat Fan
Locked
Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

return list from a function

Post 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?
WBR, Dmi

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post 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

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

Thanks, Peter! That helps!
WBR, Dmi

Locked