Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - kks

#1
newLISP newS / Re: newLISP stable release 10.2.0
March 17, 2010, 12:26:29 AM
Thanks Lutz and the community for all your contribution to newLISP!



After trying the new version, I've found some bugs??:



1. `setf` redefines `nil` if the place argument is a non-existing `assoc`:

(setf (assoc 'a '()) '(a 1))
nil   # => (a 1)

   By the way, is it possible to implicitly add an assoc if it does not exist?:

(setq L '((a 1)))  (setf (assoc 'b L) '(b 2))  L   # should return ((b 2) (a 1))
(setq L '((a 1)))  (setf (lookup 'b L) 2)  L   # should return ((b 2) (a 1))


2. It seems that `pretty-print`'s `str-fp-format` option also affects `string`:

(pretty-print 80 " " "%1.3f")
(string 1.2)   # => "1.200" instead of "1.2"
#2
I like itistoday's `sym-context`.



What if "warning" is used instead of "error" when name clashes (redefining protected names)? Old code can be run without changes, because it didn't mean to use the "new" definition of the names.



By the way, I use a naming convention that starts "variables" with an uppercase letter (got the idea from PicoLisp), to prevent name clashes with "functions". (But then they may clash with "classes"!)
#3
Anything else we might add? / Re: Wish list
January 22, 2010, 12:08:11 PM
Or, like `lookup`, adding a parameter to `nth` for a "default value" to return in case an index is out of bounds:

(nth 5 '(1 2 3) 0)   # => 0


But Kazimir's suggestion is more general, in case we don't know a value that would never exist in the list.
#4
Anything else we might add? / Re: Wish list
January 20, 2010, 12:33:51 PM
Thanks Lutz for clarifications
#5
Anything else we might add? / Re: Wish list
January 20, 2010, 03:20:15 AM
As for new FOOP, is `self` a function or a list? And how can I use association list for fields? In old FOOP, I can do:

(define (C:C x y) (list C (list 'x x) (list 'y y)))
(define (C:p o) (println "x=" (lookup 'x o) "; y=" (lookup 'y o)))
(:p (C 1 2))   # x=1; y=2


By the way, is it a good idea if newLISP extends `nth` to call `lookup` for non-number indices? (Another wish ;-)

(setq L '((a 1) (b 2) (c (d 3))))
(list (L 'a) (L 'c 'd) (L 2 'd))   # => (1 3 3)


And when an index is out-of-bound, why not `nth` just return nil instead of throwing an error? I found that I usually need to check the length of the list first.

(setq L '(a b (c d))
(L 1)   # => b
(L 5)   # ERR: list index out of bounds
(and (> (length L) 5) (L 5))   # => nil
(and (> (length L) 2) (> (length (L 2)) 1) (L 2 1))   # => d


Currently, I just do:

(define (nth? Idx Seq (Default))
{like nth, but return Default if Idx is out of bound or Seq is not a sequence}
(let (R)
(if (catch (nth Idx Seq) 'R) R
(find "index out of bounds|list expected" R 0) Default
(throw-error R) )))
(nth? 5 L)   # => nil
(nth? '(2 1) L)   # => d

But as you see, isn't just (L 5) better than (nth? 5 L)?



By the way, could someone please tell me how to have newLISP's readline history across sessions?
#6
Anything else we might add? / Re: Wish list
January 17, 2010, 11:05:17 AM
Thanks a lot, Lutz! Curious about which wishes will come true ;-)



Kazimir, I like your floating-point operators (+., *., etc), first found it in your library, and hope they will be built-in too, so they will be standard names. Currently I do:

(setq +. add  -. sub  *. mul  /. div  %. mod)


As for, `modify`, I think using `$it` with `set` is OK

(setq x (+ $it y))
(setq x (append $it y z))
#7
Anything else we might add? / Re: Wish list
January 17, 2010, 01:20:15 AM
Lutz,

Yes `slice` works, but in this case it will unnecessary allocate memory, right? It's not good if (find p (i s)) is used in a loop, when s is very large.



TedWalther,

comment macro does not work.

(define-macro (comment))
(list 1 (comment 2) 3)   # => (1 nil 3), not (1 3)


xytroxon,

In my opinion, I think #{...}# is better than #|...|#, because it's easier to match the end of comment by using `%` in vim.
#8
Try this

...
   (local (h codelen code separator elider)   # add `h` (head of current list)
      ...
      (when (= i 0) (setq h code   # set `h` here
               res (append res "<@ " (string code) ">" )))
      ...
      (setq elider (find (upper-case (string h)) '("OMT" "SAO" "OSA" "SAI" "OSI" "SAW" "OSW")))   # change `code` to `h`
      ...


Here is the full code:

(setq res "")
(define (proc procode)
(local (h codelen code separator elider)
(begin
(setq codelen (length procode))
(for (i 0 (- codelen 1) 1)
(begin
(setq code (procode i))
(when (= i 0) (setq h code  res (append res "<@ " (string code) ">" )))
;at this point, check to see whether OMT and SAI things are used
;if so, set the separator to empty string
(setq elider (find (upper-case (string h)) '("OMT" "SAO" "OSA" "SAI" "OSI" "SAW" "OSW")))
(if (nil? elider)
(setq separator "|")
(setq separator "")
)
(when (> i 0 )
(if    
(list? code) (proc code)
(atom? code) (setq res (append res (string code) )  )
)
(if (< i (- codelen 1))
(begin
(setq res (append res separator ))
)
)
)
(when (= i (- codelen 1))
(setq res (append res "</@>" ))
)
)      
)
)
)
)

(setq code "(sai (ITEFORLITLITLITLIT 999999999 1 1 (SAYOPRCAP (SAYVALFOR ...) "thing"))(sayvar goose))")
(setq parsed (read-expr code))(println $0)
(proc parsed)
(print res)
(exit)


The result is:

<@ sai><@ ITEFORLITLITLITLIT>999999999|1|1|<@ SAYOPRCAP><@ SAYVALFOR>...</@>|thing</@></@><@ sayvar>goose</@></@>


Is this what you want?
#9
Anything else we might add? / Re: Wish list
January 16, 2010, 08:28:28 AM
Thank cormullion for your feedback.



As for block comment, a string cannot be used as an alternative for a comment in all places. For example, to "comment-out" an item (2) in a list, compare:

(list 1 "2" 3)  # use string, not valid

(list 1
 # 2   # use single-line comment, ok but need to reformat code
  3 )

(list 1 #{2}# 3)   # if block comment is supported

And for the syntax, frankly, I don't like [text]...[/text], [cmd]...[/cmd], [comment]...[/comment]. I think its too verbose.



As for nil in 'find' function, if I want to find a (non-regex) substring p in a string s starting at an index position i:

(find p (slice s i))   # work, but isn't slice will allocate memory; what if s is a very large string

(find (regex-quote p) s 0 i)   # work, if we have defined 'regex-quote' ourselves, it's not built-in

(find p s nil i)   # error, nil is not a number!


And for inc/dec:

(inc 1.2)   # => 2, is this expected in most cases?

(float? (inc 1 1))  # => true!


I know that some changes can be implemented by defining my own functions (e.g. my-print, my-inc), but I think they are "normal" and should be "built-in".
#10
Anything else we might add? / Wish list
January 15, 2010, 01:40:43 PM
Hi! After learning newLISP for some times, here is my wish list ;-)



1. block/multi-line (nestable) comment; its syntax might be #{...}#



2. .0 is not removed when printing floats (e.g. 1.0 should be printed 1.0, not 1)



3. regex-option parameters should accept nil to mean not using regular expression (e.g. (find "x" "axbxc" nil 2))



4. inc (and dec, sequence, for) should correctly use either integer or floating-point arithmetic based on the type of current value (or of its additional argument) (e.g. (inc 1.2) should be 2.2, not 2; (inc 1 2) should be 3, not 3.0)



5. parameter names can be prefixed (or suffixed) with a marker (says &), and the corresponding arguments would be passed by reference (e.g. (define (my-pop &l (n 0)) (pop &l n)))



6. always return by reference (see http://newlispfanclub.alh.net/forum/viewtopic.php?f=16&t=3433">//http://newlispfanclub.alh.net/forum/viewtopic.php?f=16&t=3433)



I'm not sure whether they will have any impact (backward compatibility, performance, integrity, etc.). What's your opinion?
#11
newLISP in the real world / Re: Duplicates in list...
December 31, 2009, 04:40:33 PM
A "imperative" solution:

(let (R '()  L l  E nil)
(while L
(setq E (pop L))
(when (or (find E R) (find E L))
(push E R -1) ))
R )


or in one line ;-)

(let (R '()  L l  E nil) (while L (setq E (pop L)) (when (or (find E R) (find E L)) (push E R -1))) R)


But I love Kazimir's solution. Thanks.



Happy New Year 2010 to all of you newLISPer!
#12
newLISP in the real world / Re: Returning by reference
December 26, 2009, 04:49:41 AM
By the way, in the following code:

(setq x ((fn () '(a b c))))   # => (a b c)

does it perform ONE (either when returning or binding) or TWO (both) copy operations?



Is it possible to make all sequences return "by reference"? I think this still conforms to ORO (One Reference Only) memory management, as it is "binding" (set, let, parameter binding), not "returning", that causes "referencing"!
#13
newLISP in the real world / Returning by reference
December 25, 2009, 04:36:10 PM
Hi,



How to "return by reference" from a user-defined function/macro?



(let (l '(a b c)) (pop l) l)   # => (b c)
(let (l '(a b c)) (pop (or l)) l)   # => (b c)
(let (l '(a b c)) (pop (println l)) l)   # => (a b c); is this a bug?
(let (l '(a b c)) (pop (begin (println l) l)) l)   # => (b c)
(let (l '(a b c)) (pop (let () l)) l)   # => (a b c); is this a bug?
(let (l '(a b c)) (pop ((fn () l))) l)   # => (a b c); how to make it return (b c)?


My intention is to write a debugging macro:



(define-macro (%p) (println "% " (args 0) " = " (eval (args 0))))

(let (l '(a b c)) (pop (%p l)) l)   # => (a b c); how to make it return (b c)?


Thanks,

KS