Re: My possibly wrong use of apply

2014-07-08 Thread Alexander Burger
Hi Christophe,

 # In order to call xml with the default args and then the particular args,
 # decided to use apply:
 (apply xml (quote text text-anchor middle font-size 24 x 120 y 99

Yes, this is not possible.


'apply' can only be used for functions which evaluate their arguments
(EXPRs and SUBRs, but not FEXPRs). And 'xml' is an FEXPR:

   (de xml Lst
  ... )

The single 'Lst' parameter indicates that the arguments are not
evaluated.


To understand why 'apply' cannot be used with non-evaluating functions,
recall how 'apply' works: It does some kind of internal quoting to the
arguments, to _suppress_ the evaluation a function normally does.

For example:

   (setq A 1  B 2  C 3)
   (apply println '(A B C))

This should print

   A B C

and _not_

   1 2 3

Thus, it is equivalent to

   (println 'A 'B 'C)

and not

   (println A B C)


To achieve that, 'apply' passes the elements in the list (A B C) to some
internal structure, which quotes them before calling the function
'println'. 'println' in turn evaluates the arguments, and gets the
symbols A, B and C as expected.

If - instead of 'println' - a function is called which does not evaluate
its arguments, the results will be undefined (implementation-specific).

♪♫ Alex
-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: My possibly wrong use of apply

2014-07-08 Thread Christophe Gragnic
On Tue, Jul 8, 2014 at 10:38 AM, Alexander Burger a...@software-lab.de wrote:
 Hi Christophe,

Hi Alex, thanks for this prompt answer.

 # In order to call xml with the default args and then the particular args,
 # decided to use apply:
 (apply xml (quote text text-anchor middle font-size 24 x 120 y 99

 Yes, this is not possible.

 'apply' can only be used for functions which evaluate their arguments
 (EXPRs and SUBRs, but not FEXPRs). And 'xml' is an FEXPR:

Ouch.

 For example:

(setq A 1  B 2  C 3)
(apply println '(A B C))

 This should print

A B C

 and _not_

1 2 3

 Thus, it is equivalent to

(println 'A 'B 'C)

 and not

(println A B C)

Even if I did not face this problem, this is counter-intuitive to me.
I guess that there are serious reasons for this behaviour.
This may be nice examples for the docs! But I'll understand that
you don't want to clutter those pages.

 If - instead of 'println' - a function is called which does not evaluate
 its arguments, the results will be undefined (implementation-specific).

OK, so what are my options? I'd say:
1) try to write a king of «apply» that would work for me,
2) rewrite xml so that it evaluates its args, then quote the args in
my calls to it.

Any suggestion?


chri

-- 

http://profgra.org/lycee/ (site pro)
http://delicious.com/profgraorg (liens, favoris)
https://twitter.com/profgraorg
--
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: My possibly wrong use of apply

2014-07-08 Thread Tomas Hlavaty
Hi Christophe,

 2) rewrite xml so that it evaluates its args, then quote the args in
 my calls to it.

 Exactly! I would do:

(de xml (Lst)  # See also lib/xm.l and lib/xml.l
   ... )

 and then provide also a quoting function

(de xmlq Lst
   (xml Lst) )

 giving a convenient frontend function.

another option is calling eval like this:

: (eval (cons 'xml X))

where X is the cons tree describing the XML.  Of course, you still need
to build the X cons tree somehow.

Cheers,

Tomas
-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe