0

I would like to implement a functional stack in scheme. This is my attempt:

(define make-stack
  (letrec ((do-op
            (lambda (stack op . val)
              (cond ((eq? op 'push)
                     (lambda (op . v)
                       (do-op (cons (car val) stack) op v)))
                    ((eq? op 'pop)
                     (lambda (op . v)
                       (do-op (cdr stack) op v)))
                    ((eq? op 'print)
                     (begin (display stack)
                            (newline)
                            (lambda (op . v)
                              (do-op stack op v))))))))
    (lambda (op . val)
      (do-op '() op val))))

The stack can be used as in this example:

(define s make-stack)
((((((s 'push 1) 'push 2) 'push 3) 'print) 'pop) 'print)

The output of this example is:

((3) (2) (1))
((2) (1))

Not exactly what I wanted, but not too bad. I wanted to ask the experienced schemers here if there is a way to make the stack behave more naturally, for example like this:

(define s make-stack)
(s 'push 1)
(s 'push 2)
(s 'pop)
...

while keeping it functional (so no mutability, no set!).

The first thing I thought was to keep returning a function with no arguments, but changing every lambda (op . v) with:

(lambda ()
  (lambda (op . v)
    ...

but this does not work as we still need to capture the returned function:

> (define s make-stack)
> ((s) 'push 1)
#<procedure>
J. D.
  • 279
  • 1
  • 9
  • Your desired syntax looks like it needs a mutable stack... – Shawn Oct 23 '20 at 01:46
  • @Shawn I was expecting that a more experienced schemer would come up with a macro so that my desired syntax would be realised by calling the stack and then redefining it by capturing its output. I agree this would not feel very pure. – J. D. Oct 23 '20 at 08:08
  • 1
    `cons`, `car` and `cdr` are the equivalent of `push`, `peek`, and `delete` :-o `pop` is `peek` + `delete`, but in a functional stack that is not possible, – Sylwester Oct 25 '20 at 02:36

2 Answers2

2

As Amalloy suggests, functional style has principles that contrast with your goal. It's not impossible to do something similar, however -

(define (run-stack s prgm)
  (foldl (lambda (step s) (apply s step))
         s
         prgm))

(run-stack
  make-stack
 '((push 1)
   (push 2)
   (push 3)
   (print)
   (pop)
   (print)))

To test this, I implemented make-stack as -

(define (dispatch-stack s)
  (lambda payload
    (match payload
      ((list 'pop)
       (dispatch-stack (cdr s)))
      ((list 'push v)
       (dispatch-stack (cons v s)))
      ((list 'print)
       (println (reverse s))
       (dispatch-stack s))
      (_ (error "invalid stack operation" payload)))))

(define make-stack
  (dispatch-stack null))

Output -

'(1 2 3)
'(1 2)

Another thing I think you would like to see is Make a language in one hour: stacker. This will help you to see how languages like Racket are fundamentally different from others you have probably worked with before.

Mulan
  • 129,518
  • 31
  • 228
  • 259
  • Is there a reason why you have `lambda payload` instead of `lambda (payload)` or is it just a typo? – J. D. Oct 24 '20 at 16:51
  • 1
    J.D., it is intentional. it's a valid program you can run to verify the results. By writing `(lambda payload )`, all of the arguments passed to the lambda are collected in `payload` and allows `` to work with them as a list. A function with a varying number of parameters is known as a [variadic function](https://en.wikipedia.org/wiki/Arity). The [define](https://docs.racket-lang.org/reference/define.html) form is flexible and allows for expressing positional, curried, keyword-style, rest-style parameters, and more. – Mulan Oct 24 '20 at 17:14
  • 1
    @J.D. with `lambda (payload)`, you'd have to define `run-stack` to `foldl` with `(lambda (step s) (s step))` instead, and it'd be the same overall. – Will Ness Oct 27 '20 at 13:57
  • I like very much this solution, it's an excellent illustration that sometimes `foldl` and `foldr` are not interchangeable. – J. D. Oct 27 '20 at 18:15
1

Your goals are contradictory. If s is a stack, and your operations purely functional, then (s 'push 1) must return the same thing every time it is called. To capture the notion of change, you must use a different s each time. That's how your functional stack works: it gives you back a new stack, and you must compose function calls with it.

amalloy
  • 89,153
  • 8
  • 140
  • 205
  • Good point, thanks. Your answer made me realise that at least I can re-write the `'print` part so that it does not return a new function, since printing something on the console does not change the stack. – J. D. Oct 23 '20 at 08:04