Re: binding free symbols in a lambda definition
'fill' (http://software-lab.de/doc/refF.html#fill) does the job in some cases as well and is a bit easier to read... : (de adder (N) (let @X N (fill '((x) (+ x @X) -> adder : (adder 1) -> ((x) (+ x 1)) : (adder 2) -> ((x) (+ x 2)) : ((adder 1) 99) -> 100 /Lindsay
Re: binding free symbols in a lambda definition
You can also do this... : (de adder (N) (list '(x) (list '+ 'x (eval 'N -> adder : (adder 1) -> ((x) (+ x 1)) : (adder 2) -> ((x) (+ x 2)) : ((adder 99) 1) -> 100 Small examples like this one are great learning devices :) As Erik pointed out though, 'curry' is probably more general purpose and a lot more convenient to use for complex use cases. ( http://software-lab.de/doc/refC.html#curry) /Lindsay
Re: binding free symbols in a lambda definition
This works: :(de myf (F L) (F L)) -> myf : (let (L 99) (myf '((x) (+ (car x) `L)) (1 2))) -> 100 The key there is the back-quote (`) before the L to force evaluation See the doc section on 'Read-Macros' http://software-lab.de/doc/ref.html#macro-io /Lindsay On Tue, Feb 7, 2017 at 7:55 PM, pdwrote: > Hello, > > I wonder if there is any way to bind a free symbol in a lambda in order to > pass the lambda to a defined function (for example) > > What I want to do is something like this: > > (de myf (f l) (f l)) > > (let l 99 (myf '((x) (+ (car x) l)) (1 2))) > > I want it to return 100 but it fails with an error (1 2) number expected > > this is because free symbol l in lambda ((x) (+ (car x) l)) is not bind by > let as pretended because of quoting of lambda > > In other words, I think the problem is quoting avoids let binding as in: > > (setq f (let n 10 '((x) (+ x n -> ((x) (+ x n)) > > but I want it to return -> ((x) (+ x 10)) > > or using the typicall example: > > (de adder (n) '((x) (+ x n))) -> ((n) '((x) (+ x n))) > > so (adder 1) should return ((x) (+ x 1)) but it returns ((x) (+ x n)) > > Is there any way to manage this? Something similar to "expand" in newlisp > will do the job: > > newlisp: >(define (badadder n) (lambda (x) (+ x n))) >(badadder 3) -> (lambda (x) (+ x n)) >(define (adder n) (expand (lambda (x) (+ x n)) 'n)) >(adder 3) -> (lambda (x) (+ x 3)) > > The fist example in newlisp will be: > > (define (myf f l) (f l)) > > (let ((l 99)) (myf (lambda (x) (+ (first x) l)) '(1 2))) > > which fails for same reason picolisp fails but in newlisp the solution is: > > (define (myf f l) (f l)) > > (let ((l 99)) (myf (expand (lambda (x) (+ (first x) l)) 'l) '(1 2))) -> > 100 > > > thanks > > >
Re: binding free symbols in a lambda definition
I think 'curry' is what you're looking for. Your 'adder' example could be written as: : (de adder (@N) (curry (@N) (X) (+ X @N))) -> adder : (adder 3) -> ((X) (+ X 3)) : (doc 'curry) # for more info :) Hope that helps, Erik On Feb 7, 2017 10:04 PM, "pd"wrote: > Hello, > > I wonder if there is any way to bind a free symbol in a lambda in order to > pass the lambda to a defined function (for example) > > What I want to do is something like this: > > (de myf (f l) (f l)) > > (let l 99 (myf '((x) (+ (car x) l)) (1 2))) > > I want it to return 100 but it fails with an error (1 2) number expected > > this is because free symbol l in lambda ((x) (+ (car x) l)) is not bind by > let as pretended because of quoting of lambda > > In other words, I think the problem is quoting avoids let binding as in: > > (setq f (let n 10 '((x) (+ x n -> ((x) (+ x n)) > > but I want it to return -> ((x) (+ x 10)) > > or using the typicall example: > > (de adder (n) '((x) (+ x n))) -> ((n) '((x) (+ x n))) > > so (adder 1) should return ((x) (+ x 1)) but it returns ((x) (+ x n)) > > Is there any way to manage this? Something similar to "expand" in newlisp > will do the job: > > newlisp: >(define (badadder n) (lambda (x) (+ x n))) >(badadder 3) -> (lambda (x) (+ x n)) >(define (adder n) (expand (lambda (x) (+ x n)) 'n)) >(adder 3) -> (lambda (x) (+ x 3)) > > The fist example in newlisp will be: > > (define (myf f l) (f l)) > > (let ((l 99)) (myf (lambda (x) (+ (first x) l)) '(1 2))) > > which fails for same reason picolisp fails but in newlisp the solution is: > > (define (myf f l) (f l)) > > (let ((l 99)) (myf (expand (lambda (x) (+ (first x) l)) 'l) '(1 2))) -> > 100 > > > thanks > > >
Re: A script that concatenates ref. and tutorial files into one file
Hi Jon, > I have noticed that frequent use of ‘setq’ has been questioned recently on > this list. In my convConcat.l script there are quite a few setq’s that > possibly > could have been coded in a better way. Let me know if you spot some obvious > candidates. Basically there is nothing bad about 'setq'. It is just problematic if you use it to modify not-locally-bound symbols (the term is "free variables") without them being clearly meant as global variables (by convention with a leading '*'), or from functions meant as local to other functions (by convention with a leading '_'). 'lint' should complain in such cases. In case of convConcat.l, you set some globals in the beginning, and local variables like 'Head', 'Tail', 'Val' and 'C'. Looks all good. If I lint the code, I just get a single warning : (lintAll) -> ((convLink (bnd @Props @After @X @Before))) which means "please bind these four symbols" in function 'convLink'. This is because 'match' also sets values. You can fix this by calling 'use' on them: (de convLink (F Chunk) # First checking special case (use (@Props @After @X @Before) (if (match '(@Before < a " " n a m e = "\"" > @X "\"" > @After) Chunk) ♪♫ Alex -- UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
Re: replacement for (let L2 (drop L1 2)....
On Tue, Feb 07, 2017 at 06:44:43AM -0800, Lindsay John Lawrence wrote: >((= '() Lst) '() ) > .. > However when I (pp 'selectN) it wrote the line where I am trying to test >((= 'NIL Lst) 'NIL) Yes, the reader returs the symbol NIL when it sees () > Is that correct? Shouldn't the pp output at least look something like > this? (no ' on NIL) > ((= NIL Lst) NIL) 'pp' just pretty-prints what is there. It does not modify the data in any way. > It is a bit of nuance regarding '() , NIL and 'NIL I will have to keep > in mind. OK :) It is just that any symbol which evaluates to itself does not *need* to be quoted. It does no harm to quote it, except for the penalty in terms of memory and speed. This is e.g. NIL or T, but also strings (transient symbols). Numbers, BTW, are also evaluating to themselves, so it is not necessary to write e.g. (+ '3 '4) though it is not wrong. ♪♫ Alex -- UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
Re: replacement for (let L2 (drop L1 2)....
I just noticed... my original source for the selectN function is this... (de selectN (Lst P) .. ((= '() Lst) '() ) .. However when I (pp 'selectN) it wrote the line where I am trying to test for an empty list as (de selectN (Lst P) .. ((= 'NIL Lst) 'NIL) .. Is that correct? Shouldn't the pp output at least look something like this? (no ' on NIL) .. ((= NIL Lst) NIL) .. It is a bit of nuance regarding '() , NIL and 'NIL I will have to keep in mind. /Lindsay On Mon, Feb 6, 2017 at 11:13 PM, Alexander Burgerwrote: > On Tue, Feb 07, 2017 at 07:41:45AM +0100, Alexander Burger wrote: > > (cond > >((= 'NIL Lst) 'NIL) > > One important note: (= 'NIL Lst) is not a good idea. > Better to use (not Lst). > > (= NIL Lst) would compare *names* if 'Lst' happened to be > a symbol, so (= NIL "NIL") returns T. > > Better is (== NIL Lst), which checks for identity. > > Anyway, good that we have have 'not' ;) > > ♪♫ Alex > -- > UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe >
Re: replacement for (let L2 (drop L1 2)....
Thanks! I still trip over those nuances if I don't pay attention testing for NIL or '(), or not, in lists and symbols. /Lindsay On Mon, Feb 6, 2017 at 11:13 PM, Alexander Burgerwrote: > On Tue, Feb 07, 2017 at 07:41:45AM +0100, Alexander Burger wrote: > > (cond > >((= 'NIL Lst) 'NIL) > > One important note: (= 'NIL Lst) is not a good idea. > Better to use (not Lst). > > (= NIL Lst) would compare *names* if 'Lst' happened to be > a symbol, so (= NIL "NIL") returns T. > > Better is (== NIL Lst), which checks for identity. > > Anyway, good that we have have 'not' ;) > > ♪♫ Alex > -- > UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe >
Re: replacement for (let L2 (drop L1 2)....
Thanks Alex! recur/recurse... I hadn't noticed those functions in the picolisp function library until now. Very useful. /Lindsay On Mon, Feb 6, 2017 at 10:41 PM, Alexander Burgerwrote: > Hi Lindsay, > > > I couldn't resist tinkering with this a bit more. > > Many thanks for this and all the previous examples! > > > # -- > > (de selectN (Lst P) > >(let selectNN > > '((Lst P I) > > (cond > > ((= 'NIL Lst) 'NIL) > > ((P (car Lst) I) > >(cons > > (car Lst) > > (selectNN (cdr Lst) P (inc I)) ) ) > > (T (selectNN (cdr Lst) P (inc I))) ) ) > > (selectNN Lst P 1) ) ) > > # -- > > > > Same code, I just realized Picolisp (= code data) allows me to move the > > recursing function inside the initializing one. > > The results are the same, but now the recursing function is effectively > > private. > > > As a further simplification, you could use recur/recurse: > >(de selectN (Lst P) > (let I 1 > (recur (Lst P I) > (cond >((= 'NIL Lst) 'NIL) >((P (car Lst) I) > (cons > (car Lst) > (recurse (cdr Lst) P (inc I)) ) ) >(T (recurse (cdr Lst) P (inc I))) ) ) ) ) > > ♪♫ Alex > -- > UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe >
A script that concatenates ref. and tutorial files into one file
Hi, As a few has asked for a monolithic reference (and tutorial) HTML file, I got tempted to try to write a PicoLisp program that does such a concatenation. The difficult part, of course, was adjusting all the internal links, and it was a bit more challenging than I first thought, especially after I understood that the files in doc/form/ also should be included. The script and some more info can be found here: https://github.com/jkleiser/picolisp-onedoc-html An example of a complete concatenated file can be seen here: http://folk.uio.no/jkleiser/pico/one/one.html - - - I have noticed that frequent use of ‘setq’ has been questioned recently on this list. In my convConcat.l script there are quite a few setq’s that possibly could have been coded in a better way. Let me know if you spot some obvious candidates. /JonPԔ � )mX�����zV�u�.n7�
Re: replacement for (let L2 (drop L1 2)....
Hi Jon, > I wasn’t aware of nor, nand, nond. Maybe there should have been a few more > “See also” in the docs. True! I've added some ... ♪♫ Alex -- UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
Re: replacement for (let L2 (drop L1 2)....
Wow...really pleased I asked. Those are great examples and I'm sure I'll learn a lot from them. I've needed to process lists/trees most of programming "life" and the languages I've used haven't exactly regarded them as first class citizens and this has slowed me down quite a lot. I don't have that problem now and despite my lack of familiarity and needing to look a lot of stuff upthe results are extremely rewarding. Thank you all for your help and advice. Best Regards Dean On 7 February 2017 at 08:50, Jon Kleiserwrote: > Hi, > > I wasn’t aware of nor, nand, nond. Maybe there should have been a few more > “See also” in the docs. > > /Jon > > > On 7. Feb, 2017, at 08:31, Alexander Burger wrote: > > > > On Tue, Feb 07, 2017 at 08:13:06AM +0100, Alexander Burger wrote: > >> Better to use (not Lst). > > > > One more note: I even try to avoid 'not' whenever possible, as it is an > > additional function call overhead. > > > > It is often possible to use the complementary flow function, > > like (ifn Lst ..) instead of (if (not Lst) ..). > > > > if<-> ifn > > and <-> nor > > or<-> nand > > when <-> unless > > cond <-> nond > > while <-> until > > > > ♪♫ Alex > > -- > >
Re: replacement for (let L2 (drop L1 2)....
Hi, I wasn’t aware of nor, nand, nond. Maybe there should have been a few more “See also” in the docs. /Jon > On 7. Feb, 2017, at 08:31, Alexander Burgerwrote: > > On Tue, Feb 07, 2017 at 08:13:06AM +0100, Alexander Burger wrote: >> Better to use (not Lst). > > One more note: I even try to avoid 'not' whenever possible, as it is an > additional function call overhead. > > It is often possible to use the complementary flow function, > like (ifn Lst ..) instead of (if (not Lst) ..). > > if<-> ifn > and <-> nor > or<-> nand > when <-> unless > cond <-> nond > while <-> until > > ♪♫ Alex > -- PԔ � )mX�����zV�u�.n7�