Need help translating some scheme: exception error: function set/2 undefined

Im trying to translate some scheme examples in LFE and i stuck in that piece of code. someone can give me a tip?

lfe> (defun class-box (init-value)
       (let ((content init-value))
         (lambda (msg)
           (let ((m (car msg)))
             (cond
               ((== m 'read) content)
               ((== m 'write!)
                (set content (cadr msg))
                 content)))))))
                 
lfe> (set box-1 (class-box 50))
#Fun<lfe_eval.30.123855323/1>

lfe> (funcall box-1 '(read))                  
50
       
lfe> (funcall box-1 '(write! 56))         
** exception error: function set/2 undefined
2 Likes

Since LFE (as is Erlang) is immutable and fully functional you can’t reassign content using set.
The way content is stored here in the ‘let over lambda’ is not doable in Erlang/LFE because of immutability. Erlang/LFE is completely immutable. The content variable here represents state, which in Erlang/LFE is handled in a process.

2 Likes