Author Topic: Lisp in Basic  (Read 208200 times)

0 Members and 3 Guests are viewing this topic.

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #795 on: September 18, 2014, 01:28:06 PM »
Mike,

Does this work in your Lisp(s)  ??

(define (tst x)
  (let ((f (lambda (x i acc)
            (if (zero? i) acc
                (f x (- i 1) (+ acc x))))))
  (f x x 0)))

or


(define tst (lambda (x)
  (let ((f (lambda (x i acc)
            (if (zero? i) acc
                (f x (- i 1) (+ acc x))))))
  (f x x 0))))

stupid method to calculate the square of a number -- but Racket Scheme refuses it (this is NewLISP, -- but code should be the same ...  if the local function is not recursive it does work in Racket Scheme ..   strange  ???

Infact CL uses another method when the local is recursive (normally it is (flet ... ) , but then (labels   ) has to be used :

code in CL

(defun tst (x)
  (labels (( f ( x i acc)
         (if (zerop i) acc
             (f x (- i 1) (+ acc x)))))
    (f x x 0)))


best, Rob


Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #796 on: September 18, 2014, 01:51:13 PM »
Hi Rob,

How do you test such a function and what is its expected visible output, please?

(tinyscheme.exe yields unbound variable f if I try to call, say, (tst 5))

(same in Petite Chez)

(same in Gambit)
« Last Edit: September 18, 2014, 02:00:04 PM by Mike Lobanovsky »

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #797 on: September 18, 2014, 02:02:27 PM »
Hi Mike,

Yes something as (test 5)  -- Racket Scheme gives the "unbound variable error" too ...
(but if the local function is non-recursive it it is bound ...    ::) ::)

..  and your brainchild, what does it ??


best Rob
 

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #798 on: September 18, 2014, 02:16:17 PM »
If that (let ((f ... stands for (let f (( ... then my brainchildren won't be able to cope with it at all yet because they don't currently support named let. Actually OxyLISP returns 0 not being able to intercept the unbound f. And this isn't its only deviation from a canonical Scheme.

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #799 on: September 18, 2014, 02:30:31 PM »
Hi Mike,

just ( let (( variable ...  )      , adding a lambda-list to a local variable,   (NL doesn't have named let's) 
But this may explain something about CL uses two different mechanisms for local functions (flet ) and (labels ) -- NewLISP doesn't care , it does both ...
(but I'm not a Scheme specialist of course)

best Rob

JRS

  • Guest
Re: Lisp in Basic
« Reply #800 on: September 18, 2014, 02:47:21 PM »
Quote from: Rob
(but I'm not a Scheme specialist of course)

Lets hope that changes soon:-*

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #801 on: September 18, 2014, 03:10:21 PM »
Hello Rob,

The problem with your code is that within your lambda, you're trying to access an f which isn't bound in its environment because it isn't enumerated in lambda's list of formal parameters. The f in let and the f within lambda are two different fs, and it is the f in lambda that is flagged as unbound, which is only natural IMHO.

In some dialects, you may try to define local vars but this clearly isn't what you want to do, n'est-ce pas? So just forget. This is a dead end, and any dialect that would actually let you do it is not Scheme.

See how I've grown up to criticise your LISP code, Rob? :D

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #802 on: September 18, 2014, 03:18:13 PM »
Yep, great Mike ...

Got it now , after reading the Common Lisp specifications -- the trick CL uses    :

labels

labels is equivalent to flet except that the scope of the defined function names for labels encompasses the function definitions themselves as well as the body.

(flet  ) defines a local function -- but it can not be of the resursive kind (by reasons you mentioned)

 ;)  Rob

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #803 on: September 18, 2014, 05:19:54 PM »
Rob,

Is this what in fact you're trying to do (squeeze two defines into one?):   ;)

Code: [Select]
TinyScheme 1.41
ts> (define f (lambda (x i acc)
(if (zero? i) acc (f x (- i 1) (+ acc x)))))
f
ts> (define (tst x) (f x x 0))
tst
ts> (tst 5)
25    ; <===== IS THIS IT?
ts>

Hehe, I really don't know if it's possible in Scheme at all. But then I'm no guru either... But the only way to achieve it in OxyLISP is currently as shown in the snapshot below.

.

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #804 on: September 18, 2014, 11:27:50 PM »
Hi Mike,

Yep,  I was looking for the Scheme way of (flet ) and (labels )  from CL

this works of course :

(define test (lambda  (x)
  (let ((f (lambda (i) (* i i))))
    (f x))))

but f must be non-recursive ...

(i think it has to do with the difference between lexical and dynamical scope -- (?) ...  everything NewLisp does, is somewhat similar with the older Lisps , it uses a lot of things from the 60/70s, like the fexpressions etc ...   in fact the (first ) and (rest ) for (car ) and (cdr ) is the way McCarthy wrote his first documents/papers .. only later when ported to an IBM machine it was replaced by the then usual car / cdr IBM code ... )  NewLISP accepts recursive local functions ....

best Rob
« Last Edit: September 19, 2014, 12:52:10 AM by RobbeK »

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #805 on: September 19, 2014, 12:39:36 AM »
Hi John,

they say : practice makes perfect  :) , we'll see -- the "hygienic" macro system is completely different from CL though ...


best Rob

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #806 on: September 19, 2014, 01:04:53 AM »
Hi Rob,

(define test (lambda  (x)
  (let ((f (lambda (i) (* i i))))
    (f x))))

but f must be non-recursive ...

Where did you find recursion here? :o

It's true that the f call is in the tail position as per definition but the call itself is made physically only once and yields the answer without going any deeper into itself...

Or am I missing something?

.

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #807 on: September 19, 2014, 01:13:18 AM »
Hi Mike,

The first try contained a recursive local function , I'm just showing what does work with (let   ).
However the problem is solved now , Scheme has (letrec   )  , it even allows to use not yet declared functions (but later defined within the (letrec ) scope :

this works :

(define tst (lambda (x)
              (letrec (( f (lambda (i x acc)
                           (if (zero? i) acc
                               (f (- i 1) x (+ x acc))))))
                (f x x 0))))


this doesnt  (my first try, using a recursive lambda list within a (let   ((   .. )))

(define tst (lambda (x)
  (let ((f (lambda (x i acc)
            (if (zero? i) acc
                (f x (- i 1) (+ acc x))))))
  (f x x 0))))

so, problem solved  ;)

best, Rob

Mike Lobanovsky

  • Guest
Re: Lisp in Basic
« Reply #808 on: September 19, 2014, 01:19:02 AM »
Yes, letrec works here but there's no letrec in OxyLISP. :)

RobbeK

  • Guest
Re: Lisp in Basic
« Reply #809 on: September 19, 2014, 02:24:25 AM »
Hi Mike,

then you will have to use global defined lambda lists .... 
the intention is to make compact , ultra-fast recursive constructions ---   have a look :  (thanks to (letrec ... )

#lang racket


(define (fibo x)
  (letrec (( f (lambda (a b x)
                 (if (zero? x) a
                     (f b (+ a b) (- x 1))))))
    (f 1 1 x)))

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

> (time (fibo 3000))
cpu time: 0 real time: 0 gc time: 0
6643904603669600722802178478660283842441635124527832594055797655426212141612192573964498109829998203
9113222680280946513244634933199440943492601904534272374918853031699467847355132063510109961938297318
1622585687336939784373527897555489486841726131733814340129175622450421605101025897173235990662770203
7564387865175305471011237488491402526861201040326470251455989566759021350105669097831249594364698255
5831428970135422715178460286571078062467510705656982282054284666032181383889627581975328137149180900
4412219124856375121694811728724213667814577326618521478357661859018967313354840178403197559969056510
791709859144173304364898001
>

best, Rob