Author Topic: Lisp in Basic  (Read 208097 times)

0 Members and 3 Guests are viewing this topic.

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #915 on: September 30, 2014, 02:22:56 PM »
Your forgot the Bush clan.


that's a kind of its own, consequently it needs other kind of words,    ....  but mother and father told me not to (even) whisper such  in public  8)     

JRS

  • Guest
Re: Lisp in Basic
« Reply #916 on: September 30, 2014, 02:36:24 PM »
Not unless you want to end up as a pile of dust:-X

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #917 on: September 30, 2014, 04:18:05 PM »
Rob,

I think most of what you told us is just due to the German language being more of a mother tongue kind for you than English. My father went to the war a second lieutenant of infantry in September 1941 and came back a captain in February 1945. Alive and whole. One out of one hundred of his coevals born in the year of 1924.

I don't mean to be personal but we often take after our parents somewhat more than we should. As for me, I don't rely in such matters on what's written in the books. It turned out so I grew up in one hell, the USSR, and I'm going to die in another, Belarus. I have my own experience that's more than enough for me. And I'm still living only 30 miles away from the Ukrainian border now.

There weren't only British aircraft (and moreover, not so much British aircraft) that were bombing Dresden that day, by the way. On the other hand, Dresden was the place where my second wife grew up. She was a Belarusian girl speaking Russian and making friends with Germans. "And they lived happily ever after", hehe...

Churchill, Thatcher, the Bushes, and Obama into the bargain taken together, with all their pros and cons, are kindergarten children against the other characters of today. Churchill said: the Fascists of the future will be calling themselves anti-fascists.

Putin. Lukashenko.

Year 1995, city of Grozny, Chechnya, Russian Federation. Not an enemy territory; it's been Russia's own territory for the past 200 years. The estimated total of peaceful citizens killed by Russians in this massacre alone ranges from 70 to 200 thousand people. The exact figures are classified. The dawn of Putin's era:









So, beware.

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #918 on: October 01, 2014, 12:20:39 AM »
Hi Mike,

This is my country :
http://www.youtube.com/watch?v=cGWdshofk3s

After a century, yearly still around 300 tons of ammunition surfaces ..  it's one big graveyard.  (and they learned nothing)

----

(I think also Putin was stationed in Dresden in the 80's ??  )

best, Rob

Problem with the tight loops (more or less) solved -- added an exit button and 1 mSec delay in the inner loop -- still problems with the window [X] .. runs a lot faster than NewLISP

 

.
« Last Edit: October 01, 2014, 06:54:49 AM by RobbeK »

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #919 on: October 02, 2014, 01:28:15 PM »
HI all,

started to study the differences between Scheme and CL now (so , I do not have to do translations in the future ).

It's very powerfull

:   the closures    (p.e. making a function-trampoline)  // i normally used expansions for this ....

(define trampoline
  (lambda (n)
    (lambda (x)
        (+ x  n))))

(define ++ (trampoline 1))  ;; a function generated by another function

(++ 2) -> 3

-------------------------
the "hygienic" macro's

(define-syntax-rule (inc x)   ;; u can't do this with (define ... ) the returned value will be exact , but the variable is not updated
  (set! x (+ 1 x)))

(define a 2)
(inc a)
a -> 3

----------------   8)    nice !

best Rob

p.s.
in this case the (++ ) can be used in recursive mechanisms , the (inc ) in iterative ones : pe in a named let

(define (tst x)
  (let loop ((i 0) (n x))
    (if (= i x) n
        (loop (++ i) (+ i n)))))
« Last Edit: October 02, 2014, 01:54:31 PM by RobbeK »

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #920 on: October 02, 2014, 05:37:23 PM »
Hi Rob,

1. (define trampoline ...)/(define ++ ...) /(++ 2)->3 works OOTB in both tinyscheme and nanoscheme.

2. (define-syntax-rule) is unknown to either tinyscheme or nanoscheme. Are you sure it belongs to R5RS rather than to R6RS?

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #921 on: October 02, 2014, 11:26:38 PM »
Hi Mike,

I'll have to look it up , I'm studying Racket Scheme --  : 
the non-hygienic way should be

(define-macro (inc x)
  (quasiquote (set! ,x (+ 1 ,x))))

which is identical with CL --  the problem here is that  ,x may intercept the wrong value if x is already declared global  -- what should the code do ??   p.e.

(define x 77)

(define-macro (inc x)
  (quasiquote (set! ,x (+ 1 ,x))))    ;; which x to use , the local or the global  ,x stands for (eval x) .....  to avoid this Scheme set up the "hygienic" macro's -- but as I'm new to Scheme , I'll have to look this up (this evening)  GM+1

best, Rob

another  better(?) example of expanding a function with a "Scheme Closure"


(define to-the-power
  (lambda (n)
   (lambda (x) (expt x n))))

(define square (to-the-power 2))
(define cube (to-the-power 3))

----------------------------REPL

> (square 4)
16
> (cube 4)
64
>
« Last Edit: October 03, 2014, 12:03:53 AM by RobbeK »

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #922 on: October 03, 2014, 12:08:26 AM »
Thanks for clarification, Rob,

Your

(define-macro (inc x)
  (quasiquote (set! ,x (+ 1 ,x))))


will also do perfectly well for tinyscheme. At the moment nanoscheme would only accept macros expressed through explicit lambdas, so its syntax should be

(macro inc (lambda (x)
  `(set! ,x (+ 1 ,x))))


but it hangs -- there must still be some bug left somewhere that I've overlooked... :-[

Charles Pegge

  • Guest
Re: Lisp in Basic
« Reply #923 on: October 03, 2014, 12:34:07 AM »
In my naughty LeanLisp, inc, dec and iter (down count iteration) are core functions. A function can alter the values of its own variables but no others. When it changes the value of a parameter or outside variable, it automatically makes a new local copy, masking the original.

This ensures that functional encapsulation is preserved while avoiding the strictures of functional programming.

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #924 on: October 03, 2014, 12:53:27 AM »
Hehe, frankly I don't know why Rob is so fond of hand coded iterative palliations. Scheme has legit (do) and named (let) procedures that target iterative functional programming. What else would one need? :)

Also, any such constructs reimplemented via LISP macros and/or lambdas would be slower than their core equivalents by definition.

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #925 on: October 03, 2014, 01:36:32 AM »
Hi Charles, Mike ,

" I don't know why Rob is so fond of hand coded iterative palliations. "         -- because I can  8)
without joking -->
Be careful,   iterations and variadic structures  do not belong together --    example :

(silent
(define sq (sequence 1 100000))
(define ar (array (length sq) sq))

(define (tst x)
 (let (( acc 0 ))
  (dotimes (i (length x))
    (++ acc (nth i x)))))

)
-------------------------------repl


> (time (tst sq))    ;; variadic container  (a list)

21640.625                    !!!!!!!!!!!!!!!!!!!!!!!!  that's 22 sec !!!!!!!!!!!!!!!!!!!!!!!!!
> > (time (tst ar))  ;; non-variadic container (vector/array)
46.875
>
---------------------------

because :  the iterative process has to start from 0 every time -- it does not remember the location of a previous pointer -  the array / vector works identical with languages as Basic, C etc ...   However, by writing the stuff myself , I do know where to locate the pointers , I do know when to set up accumulators in recursive mechanisms ,  ...  )

(define (tst L)
  (letrec (( tst* (lambda (L acc)
                 (if (null? L) acc
                    (tst* (cdr L) (+ acc (car L))))))
     (tst* L 0) )

this one goes thru the pointers as -> A -> B -> C   ...  -> NULL
the iterative form goes
-> A
-> -> B
-> -> -> C
-> -> ........................  ->NULL
 ;)

(of course C , Oxygen , Basic's etc.. can increment the pointers , but his doesn't make sense if the "pointée" is of variadic type and/or length  -- in Lisp the pointers are stored inside the data , it is not something as one memory location holding an address )

"Also, any such constructs reimplemented via LISP macros and/or lambdas would be slower than their core equivalents by definition."   -- allmost all (except the fexpressions in NewLISP) languages do the (macro)expansion while loading the code -- a compiler will do this anyway --  the expanded macro definitions are identical as coded without them, but the source is clearer to read and certainly write ....

I prefer to write something as (inc x) i.o.   (set! x (+ 1 x))  certainly if used frequently , the generated code is identical ...
(of course if already existing , rewriting same is idiotic , but I don't think Scheme has (inc ) (dec ) (++ ) (-- ) etc ..

best Rob

(ps named let is a recursive mechanism, not ??   -- I think it's based on a (letrec ...  )
furthermore , AFAIK Scheme can't even index lists , there is no (elt list index) or (nth index list)  , to avoid doing what I did at the start of this text ...

(letrec ((countdown (lambda (i)
                      (if (= i 0) 'liftoff
                          (begin
                            (display i)
                            (newline)
                            (countdown (- i 1)))))))

« Last Edit: October 03, 2014, 02:30:22 AM by RobbeK »

Charles Pegge

  • Guest
Re: Lisp in Basic
« Reply #926 on: October 03, 2014, 03:22:26 AM »
For iterating over a list, (next) is a useful concept, and must be one of the fundamentals of the multiverse.

for example, a LeanLisp variadic add operation would be:

(+ (next) item )

It destructively extracts members from the list: items until it is empty.

Such an operation could be defined:

(let sum "(+ (next) item )" )

..

(sum 1 2 3 4 5)

1 2 3 4 5 is passed internally to the items variable
« Last Edit: October 03, 2014, 03:34:16 AM by Charles Pegge »

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #927 on: October 03, 2014, 03:35:25 AM »
Hi Charles, good idea to eat the pointers one by one  ;)  (in Lisp then)
   
porting it to lisp
(the non-destructive way - working on a duplicate )

(silent

(setq L (sequence 1 100000))

(constant 'next pop)    ;; pops head of a list in a destructive way  -- making just an alias ..

(define (tst x)
   (let ((tot 0) (duplicate x) )
    (dotimes (i (length x))
      (++ tot (next duplicate))) tot ))

)
------------------------------------------------------------  repl
> (time(tst L))
46.875                  -- 50 mSec : a lot better than 22 Sec.
> (length L)
100000              -- list remains intact

thanks,
best Rob

(of course Lisp as a native mechanism for above )

> (time (apply + L))
15.625

or

> (time (eval (cons + L)))
15.625
« Last Edit: October 03, 2014, 04:10:05 AM by RobbeK »

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #928 on: October 03, 2014, 06:15:03 AM »
Hi Rob,

Your answer regarding iteration efforts stands to reason.

Macros, however, are another pair of shoes. What I really meant was that any thing expressed through user-defined macros or lambdas of a language would always be slower as compared to the same thing hardcoded in the engine of that language:

Quote
46.875                  -- 50 mSec : a lot better than 22 Sec.
(of course Lisp as a native mechanism for above )
> (time (apply + L))
15.625

That's exactly what I meant -- three times slower. :)

Quote
AFAIK Scheme can't even index lists

This isn't so. As per R5RS, Scheme has list-ref, string-ref, and vector-ref that return the respective kth elements of a list, string, or vector:

library procedure:  (list-ref list k)
Returns the kth element of list. (This is the same as the car of (list-tail list k).) It is an error if list has fewer than k elements.

(list-ref '(a b c d) 2)                         ===>  c
(list-ref '(a b c d)
          (inexact->exact (round 1.8 )))
                ===>  c
« Last Edit: October 03, 2014, 06:28:35 AM by Mike Lobanovsky »

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #929 on: October 03, 2014, 07:29:26 AM »
Hi Mike,

(list-ref    )    ah, ok -- did not know ..   unlike CL it seems (list-set!  ) is missing ?       (setf (elt list index) something)   (common Lisp).

As mentioned above , be veeeery careful using it :           (language : Scheme)

(define a (range 1 1000000))
 
 
(define (testit x)   ;; iterative definition
   (let ((acc 0))
     (do ((i 0 (+ i 1)))
       ((= i x))
       (set! acc (+ acc (list-ref a i))))
     acc ))

(define (testrec x)        ;; TCO with a recursive helper function and accumulator
  (letrec ((test* 
            (λ (start end L acc)
              (if (= start end) acc
                  (test* (+ 1 start) end (cdr L) (+ acc (car L)))))))
    (test* 0 x a 0)   ))

---------------  REPL   switching to Scheme (not the extended Racket )


Language: scheme; memory limit: 512 MB.
> (time (testit 100000))
cpu time: 20750 real time: 20765 gc time: 0         ;;    21 sec !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5000050000

> (time (testrec 100000))
cpu time: 32 real time: 31 gc time: 0
5000050000


(bytecode with JIT compiler)


best, Rob

(as said, the (list-ref ) starts from zero over and over again when called -- the method Charles gave is much much faster -- (pop ) or (next )  if not existing is easy to write ....

(notify that apply will not work here - because I focus a subset from a list , this may be after all a raison-d'-être of both definitions, -- of which one, is to be avoided )

Addendum : this is the macro (no way I know to avoid writing a macro for this - in TinyScheme and NanoScheme (once the (macro    ) is working )

(define-macro (next L)
 (quasiquote (let (( x (car ,L)))
   (set! ,L (cdr ,L)) x )))

This definition is destructive , it changes the object L -- however, by just taking the first element, it will be very fast when used in an iterative loop ....

The (define-syntax-rule     ) is not standard Scheme   ,     sigh  ..... 
« Last Edit: October 03, 2014, 01:38:57 PM by RobbeK »