Extending polymorphic operations on symbols

Pondering the philosophy behind the language
Locked
Kazimir Majorinc
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Extending polymorphic operations on symbols

Post by Kazimir Majorinc »

Some operations defined for lists and strings can be extended on symbols. For example

(append 'x 'y) => xy
(chop 'zonk) => zon

also rest, find etc. Arguments pro:

  • some programs can be both shorter and at least slightly faster: (append x0 y0) instead (sym (append (string x0) (string y0))),
  • treatment of symbols as data seems to be in spirit of Lisp,
  • as current behaviour is erroneous, proposed extension should be safe, except for programs relying on (throw-error (append 'x 'y)).


Example of the code:

Code: Select all

(setf CR (lambda(x)x))
(setf CAR first CDR rest L '(CAR CDR))

(do-while(< counter 1024)
    (setf s (pop L))
    (dolist(i '(CAR CDR))
      (set 's0 (sym (append (chop (string i)) (rest (string s)))))
      (set s0 (expand (lambda(x)(i (s x))) 's 'i))
      (println (inc counter) ". " s0 "=" (eval s0))
      (push s0 L -1)))
[/color]

If I proposed it already, sorry.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Extending polymorphic operations on symbols

Post by Lutz »

Interesting idea! Also enjoyed your last blog post at:

http://kazimirmajorinc.blogspot.com/201 ... -lisp.html

here is a puzzle for everybody, why is this possible?

Code: Select all

> (map 1 '((a b c d) (e f g h) (i j k l)))
((b c d) (f g h) (j k l))
> (map (curry 1 2) '((a b c d) (e f g h) (i j k l)))
((b c) (f g) (j k))
> 

johu
Posts: 143
Joined: Mon Feb 08, 2010 8:47 am

Re: Extending polymorphic operations on symbols

Post by johu »

Nobody still post, I try.

According to Users Manual and Reference,
(map exp-functor list-args-1 [list-args-2 ... ])
Successively applies the primitive function, defined function, or lambda expression exp-functor to the arguments specified in list-args-1, list-args-2-, returning all results in a list.
Then,

Code: Select all

(map 1 '((a b c d) (e f g h) (i j k l)))
(map (curry 1 2) '((a b c d) (e f g h) (i j k l)))
      ↓
(list (1 '(a b c d)) (1 '(e f g h)) (1 '(i j k l)))
(list ((curry 1 2) '(a b c d)) ((curry 1 2) '(e f g h)) ((curry 1 2) '(e f g h)))
And,
Implicit indexing for rest and slice
Implicit forms of rest and slice can be created by prepending a list with one or two numbers for offset and length.
Then,

Code: Select all

(1 '(a b c d)) → (b c d)
(1 '(e f g h)) → (f g h)
(1 '(i j k l)) → (j k l)
Continuously,
(curry func exp)
Transforms func from a function f(x, y) that takes two arguments into a function fx(y) that takes a single argument. curry works like a macro in that it does not evaluate its arguments. Instead, they are evaluated during the application of func.
Then,
((curry 1 2) '(a b c d)) → (1 2 '(a b c d)) → (b c)
((curry 1 2) '(e f g h)) → (1 2 '(e f g h)) → (f g)
((curry 1 2) '(i j k l)) → (1 2 '(i j k l)) → (j k)
Additonally,
Implicit indexing for nth
In original Lisp, the first element in an s-expression list is applied as a function to the rest elements as arguments. In newLISP, a list in the functor position of an s-expression assumes self-indexing functionality using the index arguments following it.
Then,

Code: Select all

> (map ''(a b c d) '(3 2 1 0))
(d c b a)
> 
I'm sorry if my English usage would be wrong or my words would be insufficient.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Extending polymorphic operations on symbols

Post by Lutz »

Congratulations! Well done.

If you want, send me an address and size (as a private message or email), I can send you a newLISP T-shirt :-)

Locked