Nп/п : 37 из 93
 От   : none) (albert                       2:5075/128        19 авг 23 16:24:10
 К    : All                                                   19 авг 23 17:26:03
 Тема : MAL : closures and recursion
----------------------------------------------------------------------------------
                                                                                 
@MSGID:
7eb40bb2
@REPLYADDR none) (albert
@REPLYTO 2:5075/128 none) (albert
@CHRS: CP866 2
@RFC: 1 0
@RFC-Message-ID:

@TZUTC: 0200
@TID: FIDOGATE-5.12-ge4e8b94
https://github.com/kanaka/mal/

I`m trying to Make Another Lisp using ciforth lina/wina/xina.

I run in a bit of trouble in the interaction between closures and recursion.

I succeeded in handling closure in
( ( (fn* (a) (fn* (fn* (b) (+ a b))) 5) 7)   ... I
In lisp we have
(fn* (fn* (b) (+ a b))) 5)
in a sort of limbo. It can be only half evaluated, because b is 5 but no
decision has been made what `a actually means. (A "free" variable).
Only by encapsulating it in an "environment" where `a has the value
7 one can proceed.
Indeed is ((fn*..) 5) now evaluated in an environment where `a
is still valid. This is done by storing the then-current
environment chain (referring to all its outer
environments) in the function structure created by the second call
of fn* and restoring prior to execution.
Of course every creation and every call will suffer a similar
overhead, so the first and the third suffer too.
This works and the answer is 12.

However this "solution" gets me in the woods with recursion
(def! sumdown (fn* (N) (if (> N 0) (+ N (sumdown  (- N 1))) 0)))

As soon as you do
(sumdown 1)
you get an environment where N:1 and you got to calculate
(+ N (sumdown (-N 1)))
The first `N is not the problem, but
now upon entering sumdown, the environment is restored/destroyed
and N is nowhere to be seen.

The solution i found is the following.
Case ...I is a weird exception.  Exception! That is the keyword.
So the idea is to evaluate (fn* (b) (+ a b))) 5) as if your nose bleeds,
and then discover ERROR 8010 (symbol not found), for `a is not there.
Now you lift the not-found symbol `a from the stored environment to the
inner environment and evaluate again.
Hopefully the inner environment is to be thrown away expeditiously.
The overhead for recursion is limited by storing a pointer in the
header of a function such as sumdown or fibonacci. This happens one
time during compilation (or whatever the lisp term is) and the pointer
need never be inspected.

Coming back to ... I
A weird situation arises if in the meantime a function is called that
has a separate and distinct `a (free) in the environment it was
created in. Let`s hope this doesn`t happen.
[Why bother, people who write such programs had it coming.
The least they have to do is to rename `a to
TheDelayInMSBeforeDrawingTheBottomLineInaPurpleCaptionBox
diminishing the risk for a name clash.]

What do you all think?

Groetjes Albert
-- 
Don`t praise the day before the evening. One swallow doesn`t make spring.
You must not say "hey" before you have crossed the bridge. Don`t sell the
hide of the bear until you shot it. Better one bird in the hand than ten in
the air. First gain is a cat spinning.            - the Wise from Antrim -
--- trn 4.0-test77 (Sep 1, 2010)
 * Origin: KPN B.V. (2:5075/128)
SEEN-BY: 5001/100 5015/255 5019/40 5020/715 848
1042 4441 12000 5030/49 1081
SEEN-BY: 5058/104 5075/128
@PATH: 5075/128 5020/1042 4441



   GoldED+ VK   │                                                 │   09:55:30    
                                                                                
В этой области больше нет сообщений.

Остаться здесь
Перейти к списку сообщений
Перейти к списку эх