Re: My possibly wrong use of apply

2014-07-08 Thread Tomas Hlavaty
Hi Christophe,

>> 2) rewrite  so that it evaluates its args, then quote the args in
>> my calls to it.
>
> Exactly! I would do:
>
>(de  (Lst)  # See also "lib/xm.l" and "lib/xml.l"
>   ... )
>
> and then provide also a quoting function
>
>(de  Lst
>   ( Lst) )
>
> giving a convenient frontend function.

another option is calling eval like this:

: (eval (cons ' 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


Re: My possibly wrong use of apply

2014-07-08 Thread Alexander Burger
Hi Christophe,

> Even if I did not face this problem, this is counter-intuitive to me.
> I guess that there are serious reasons for this behaviour.

Yes, indeed. It goes down to the core of how Lisp's evaluation mechanism
works. It is not an implementation issue.


If you look in old Lisp textbooks, you sometimes find a definition of
Lisp in terms of itself. It usually boils down to (as translated to
PicoLisp):

   (de eval (Exe)
  (cond
 ((num? Exe) Exe)
 ((sym? Exe) (val Exe))
 (T (apply (car Exe) (mapcar eval (cdr Exe ) )

The important thing is the last line: (apply .. (mapcar eval ..))

'apply' and 'eval' go hand in hand. 'apply' expects a list of evaluated
arguments, and passes it to the function in (car Exe).


In PicoLisp, this behavior is exactly the same. The internal difference
is only in implementation, where in PicoLisp (as opposed in other Lisps)
each normal function takes care of evaluating its arguments itself (by
calling an internal 'eval') instead of expecting to get passed already
evaluated arguments. This is faster and more flexible, but as a drawback
'apply' has to revert this internally, and auto-quote the already
evaluated arguments.


But your issue would be the same in every Lisp. Common Lisp avoids
FEXPRs and FSUBRs for that reason, and favours macros, but you have
exactly the same problem if you try to apply a macro to a list.


As a rule, it doesn't make sense to write a library function as an
FEXPR, as in

   (de  Lst
  ... )

A FEXPR is always a terminal stop. You can use FESPRs only at the top
level of a program. There they are convenient, because you don't need to
quote the arguments. But you cannot use them in any useful way, by
calling them from other functions (because you cannot pass arguments
through, being received from the higher levels of your program), let
alone 'apply' them.

(Note that the 'apply' family of functions is much larger, it includes
also all the mapping and filtering functions, in general all functions
that have a 'fun' argument in the reference)


> OK, so what are my options? I'd say:
> 1) try to write a king of «apply» that would work for me,

No. As I tried to explain, this cannot be solved by changing 'apply'.


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

Exactly! I would do:

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

and then provide also a quoting function

   (de  Lst
  ( Lst) )

giving a convenient frontend function.

♪♫ 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  wrote:
> Hi Christophe,

Hi Alex, thanks for this prompt answer.

>> # In order to call  with the default args and then the particular args,
>> # decided to use apply:
>> (apply  (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 '' 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  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 Alexander Burger
Hi Christophe,

> # In order to call  with the default args and then the particular args,
> # decided to use apply:
> (apply  (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 '' is an FEXPR:

   (de  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 Jon Kleiser
It seems it was Tomas, not me, who wrote that xml lib. ;-)

/Jon

On 8. Jul, 2014, at 09:49, Christophe Gragnic  
wrote:

> Dear list,
> I'm writing an .svg file from Picolisp.
> For this, I'm using the xml lib Jon once started to write, a small lib
> that was enhanced by others:
> https://www.mail-archive.com/picolisp@software-lab.de/msg00510.html

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


My possibly wrong use of apply

2014-07-08 Thread Christophe Gragnic
Dear list,
I'm writing an .svg file from Picolisp.
For this, I'm using the xml lib Jon once started to write, a small lib
that was enhanced by others:
https://www.mail-archive.com/picolisp@software-lab.de/msg00510.html

Here is the content of my demo file:

(load "lib/xml.l")

# I have this next line working correctly:
( text text-anchor "middle" font-size 24 x 120 y 100 (prin "hello"))

# It prints:
# hello

# Since I have many such lines, I wanted to factor out some common attributes:
(setq text_conf '(text text-anchor "middle" font-size 24))

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

# But it *sometimes* segfaults, and *sometimes* print:
# < ="middle" ="24" ="120" ="99" =""/>

# So what's wrong with my use of apply?
# Would it be interesting to give an example in the docs with symbols instead
# of numbers?
# http://www.software-lab.de/doc/refA.html#apply

# Thanks and...
(bye)


-- 

http://profgra.org/lycee/ (site pro)
http://delicious.com/profgraorg (liens, favoris)
https://twitter.com/profgraorg


apply_segfaults.l
Description: Binary data


xml.l
Description: Binary data