O_ODrDave wrote:To pass the time while awaiting my turn at the medical center
No comment... :-)
(I have my best idea into the bathroom! Everyone has its own place to think, to ponder :-)
Great!
Code: Select all
(while lst
(if (setq myeven? (not myeven?))
(push (upper-case (pop lst)) newList -1)
(push (pop lst) newList -1)
)
Right, I considered that. But because no matter what happens in the 'if' block, there will be a 'pop', I decided to factor it out and write it just once.Lutz wrote:you could make it even shorter putting the 'pop' inside the 'if'
Code: Select all
(while lst (if (setq myeven? (not myeven?)) (push (upper-case (pop lst)) newList -1) (push (pop lst) newList -1) )
Symbols can be used as references.ale870 wrote:Lutz, is there any way (generally speaking) to make a function that return a reference to an element of a list (like in the example before) instead returning a copy? Can I do that using contexts?
Code: Select all
(set 'L (list 'L.0 'L.1 'L.2))
Code: Select all
(set 'L (list 'L.0 'L.1 'L.2))
Code: Select all
(set 'return-by-reference (lambda(lst i)(nth i lst)))
Code: Select all
(define (return-by-reference lst i) (nth i lst))
Code: Select all
(set (return-by-reference L 0) "i!")
(set (return-by-reference L 1) "o!")
(set (return-by-reference L 2) "let's go!")
Code: Select all
(set 'L.0 "i!" 'L.1 "o!" 'L.2 "let's go!")
Code: Select all
(set 'dereference-list (lambda(lst)(map eval lst)))
Code: Select all
(define (dereference-list lst) (map eval lst))
Code: Select all
(println (dereference-list L))
Code: Select all
(eval L.0 )
(eval L.1)
(eval L.2)
It will make more sense the more you use it. I'm just a casual programmer, not a hard-core computer scientist like Kaz or Lutz or some of the others.ale870 wrote:Thank you, functional languages are still "a new thing" for me, even after many months and several bottles of alchol :-) !
Code: Select all
(set 'data:data '(a b c d e f))
(define (foo lst x)
(push x lst)
lst)
(foo data 99) => data
data:data => (99 a b c d e f)
; push one more than use the return value
(reverse (foo data 98)) => (f e d c b a 99 98)
data:data => (f e d c b a 99 98)
Code: Select all
; Create all 456976 English four letter words...
; ( Warning: May be offensive to some sensitive programmers ;)
(for (z 97 122)
(for (y 97 122)
(print (time ; begin timing debug
(for (x 97 122)
(for (w 97 122)
(push (string (char z) (char y) (char x) (char w)) word-list -1)
; (inc ctr) ;<<< uncommenting this statement makes time linear
)
)
) ",") ; end timing debug
)
)
(println ctr " - " (length word-list) " words.")
(setq source-list (copy word-list)) ; copy list before each pass
; trust newLISP, but verify... ;)
(println (slice source-list 0 32))
(println (slice source-list -32))
(exit)
Code: Select all
> (set 'l '(1 2 3))
(1 2 3)
> (reverse (begin (push 99 l)))
(3 2 1 99)
> l
(3 2 1 99)
>