Is local state possible in shen?

74 views
Skip to first unread message

Boring Coder

unread,
Oct 4, 2025, 8:06:30 AMOct 4
to Shen
This is an example from SICP:  3. Modularity, Objects, and State  
is there a way to code this in Shen? and if not, why was the decision made to make all variables global?

(define (make-withdraw balance) 
            (lambda (amount) 
                         (if (>= balance amount) 
                                    (begin (set! balance (- balance amount)) balance) 
                                    "Insufficient funds")))  

 (define W1 (make-withdraw 100)) 
 (define W2 (make-withdraw 100))  

  (W1 50) 
 50 
 (W2 70) 
 30  
  (W2 40) 
 "Insufficient funds" 
 (W1 40) 
 10  

dr.mt...@gmail.com

unread,
Oct 4, 2025, 5:21:50 PMOct 4
to Shen
The two reasons for not supporting this feature
are portability and math'l purity.  let is treated
as shorthand for a value (as in maths) which keeps
the type theory and formal reasoning simple.

Regards implementing the Scheme example in Shen,
probably the easiest is to use property lists.

(0-) (define make-withdraw
       WithDraw Account -> (let Balance (get Account balance)
                                                    NewBalance (- Balance WithDraw)
                                                    (if (> NewBalance 0)
                                                         (put Account balance NewBalance)
                                                         (error "Insufficient funds"))))
(fn make-withdraw)

(1-) (put w1 balance 100)
100

(2-) (put w2 balance 100)
100

(3-) (make-withdraw 50 w1)
50

(4-) (make-withdraw 60 w2)
40

(5-) (make-withdraw 45 w1)
5

(6-) (make-withdraw 45 w2)
Insufficient funds

M.

Boring Coder

unread,
Oct 4, 2025, 11:20:22 PM (14 days ago) Oct 4
to Shen
I thought of that , but my understanding was that once you use state it didn't matter if it was local of global, the side effects are the same. 
(define create-object
    Name _          -> (error "Object defined" Name) where (bound? Name)
    Name Attributes -> (do
                         (set Name (vector (* (length Attributes) 2)))
                         (initialize-object Name Attributes)
                         Name))
(define initialize-object
    Name [] -> Name
    Name [[Key Value] | Attributes] -> (do (put Key value Value (value Name))
                                             (initialize-object Name Attributes))
  _    _ -> (error "Something wrong with your input")) 


dr.mt...@gmail.com

unread,
Oct 5, 2025, 7:52:38 AM (14 days ago) Oct 5
to Shen
with vectors

(2+) (define withdraw
   {number --> (number --> number)}
   Balance -> (let Account (@v Balance <>)
                           (/. Withdrawal (let CurrBalance (<-vector Account 1)
                                               NewBalance  (- CurrBalance Withdrawal)
                                               (if (>= NewBalance 0)
                                                   (do (vector-> Account 1 NewBalance) NewBalance)
                                                   (error "Insufficient funds"))))))
(fn withdraw) : (number --> (number --> number))

(9-) (set a (withdraw 100))
#<FUNCTION (LAMBDA (Z18380) :IN withdraw) {252032FD}>

(10-) ((value a) 30)
70

(11-) ((value a) 30)
40

(12-) ((value a) 30)
10

(13-) ((value a) 30)
Insufficient funds

M.

Boring Coder

unread,
Oct 5, 2025, 11:20:41 AM (13 days ago) Oct 5
to Shen
Yes, that is the one,. Now the state is hidden from the user and can only be accessed through the interface we provide.
I need to relearn vectors to have the correct mental modal, I didn't know I could do that.


Raoul Duke

unread,
Oct 5, 2025, 12:43:57 PM (13 days ago) Oct 5
to qil...@googlegroups.com
...Might there then be a good summary explanation of scoping in Shen? 

(eg in JavaScript things like hoisting rules and TDZ are all complicated, but there are zillions of tutorials and actual reference docs available.)
Reply all
Reply to author
Forward
0 new messages