newLISP development release 9.1.6

Started by Lutz, May 21, 2007, 05:25:27 AM

Previous topic - Next topic

HPW

#30
Quote... extended version of the GUI-server


Maybe a different approach would even better.

A API to let the GUI-server load java extension libraries (like the 3D media libraries) at runtime and a technic to call this libs without providing a complete lisp-wrapper in the GUI server. (like the import feature in newLISP) So no need to modify the server for each etxetsnion and it can be smaller with the core functions.



Edit: In newLISP-TK there was the possibility to use everything what TCL offers in a easy way. So it was possible to use the best things from both enviroments (newLISP and TCL/TK). So it would be nice to have the best from Java and newLISP.
Hans-Peter

Fanda

#31
Quote from: "Fanda"2) apply with negative 'int-reduce' - similar to fold right

http://newlisp-on-noodles.org/wiki/index.php/Functional_Programming#Reducing_lists_-_folds">http://newlisp-on-noodles.org/wiki/inde ... ts_-_folds">http://newlisp-on-noodles.org/wiki/index.php/Functional_Programming#Reducing_lists_-_folds



(apply op '(1 2 3 4 5) 2) = (op (op (op (op 1 2) 3) 4) 5)

(apply op '(1 2 3 4 5) -2) = (op 1 (op 2 (op 3 (op 4 5))))


I still keep thinking about this and I am canceling my suggestion. Instead of using an 'apply' for folding, I would create a new 'fold':


(define (fold f lst n)
  (let (result nil)
    (if
      (and (>= n -1) (less-or-equal n 1))
        (throw-error "n cannot be -1, 0 or 1!")
      (> n 0)
        (begin
          (set 'result (apply f (0 n lst)))
          (dotimes (i n) (pop lst))
          (while lst
            (set 'result (apply f (append (list result) (0 (- n 1) lst))))
            (dotimes (i (- n 1)) (pop lst))))
      (less-than n 0)
        (begin
          (set 'n (- n))
          (set 'result (apply f ((- (length lst) n) lst)))
          (dotimes (i n) (pop lst -1))
          (while lst
            (set 'result (apply f (append ((- (length lst) (- n 1)) lst) (list result))))
            (dotimes (i (- n 1)) (pop lst -1)))))
    result))

(Substitute less-than and less-or-equal for appropriate signs - they translate in the post.)



Examples:
> (fold + '(1 2 3 4 5) 2)
15
> (fold + '(1 2 3 4 5) -2)
15
> (fold - '(1 2 3 4 5) 2)
-13
> (fold - '(1 2 3 4 5) -2)
3

> (fold (fn (x y) (list '- x y)) '(1 2 3 4 5) 2)
(- (- (- (- 1 2) 3) 4) 5)
> (fold (fn (x y) (list '- x y)) '(1 2 3 4 5) -2)
(- 1 (- 2 (- 3 (- 4 5))))

; general function f
> (fold (fn () (append '(f) (args))) '(1 2 3 4 5 6 7 8 9) 2)
(f (f (f (f (f (f (f (f 1 2) 3) 4) 5) 6) 7) 8) 9)
> (fold (fn () (append '(f) (args))) '(1 2 3 4 5 6 7 8 9) 3)
(f (f (f (f 1 2 3) 4 5) 6 7) 8 9)
> (fold (fn () (append '(f) (args))) '(1 2 3 4 5 6 7 8 9) 4)
(f (f (f 1 2 3 4) 5 6 7) 8 9)

> (fold (fn () (append '(f) (args))) '(1 2 3 4 5 6 7 8 9) -2)
(f 1 (f 2 (f 3 (f 4 (f 5 (f 6 (f 7 (f 8 9))))))))


There is no hurry to do this. I am just posting it, so that I can stop thinking about it ;-)



Fanda

rickyboy

#32
Love the new fold!  Good job, Fanda!  --Rick
(λx. x x) (λx. x x)