Why do you want to avoid foreach which happens to cater
for your needs? Just write a simple wrapper (I'll call
it apply$) and use it just like (apply):

; a function to be called on each element of a list
(deffunction show (?x)
  (printout t "show: " ?x crlf)
)

; (apply$ <function> <expression>*)
; calls 1st argument for each successive argument,
; flattening list arguments
(deffunction apply$ (?func $?list)
  (foreach ?el $?list (apply ?func ?el))
)

; example for calling apply$
(bind ?alist (create$ foo bar blech))
(apply$ show ?alist)

Regards
Wolfgang


JKBM wrote:

I was wondering if there was any way to call a function on a variable
containing a list without having to use a foreach/while loop.  (apply + 1 2
3) obviously works, but (apply + (create$ 1 2 3)) doesn't, so that doesn't
help me.  This sort of problem comes up a lot, and I usually handle it by
implode$-ing the list, str-cat-ing the function and parentheses, and then
build-ing it as follows:


(bind ?list (create$ 1 2 3))

(apply + ?bind) ;ERROR, as mentioned above
(build (str-cat "(+ " (implode$ ?list) ")) ;SUCCESS


However, this time, I'm dealing with a list of fact-ids, and this method
doesn't work because Jess doesn't parse "<Fact-2>" to a fact-id from the
implode$-ed string.  I thought maybe I could create$ a new list with the
function at the beginning, but there is no way that I could find to evaluate
a list, only a string.  E.g.:

(bind ?list (create$ 1 2 3))

(bind ?flist (create$ + ?list)) ;?flist is (+ 1 2 3), but can't evaluate


So, I guess I'm looking for three possible solutions:
1) calling a function on a list
2) getting Jess to correctly parse a fact-id from a string
3) getting Jess to evaluate a list as a function

Any ideas?  Or should I just give up and resort to iterative measures?
--
View this message in context: 
http://www.nabble.com/Calling-a-function-on-all-elements-in-a-list-tp15921074p15921074.html
Sent from the Jess mailing list archive at Nabble.com.


--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]
--------------------------------------------------------------------





--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]
--------------------------------------------------------------------

Reply via email to