www.delorie.com/gnu/docs/calc/calc_103.html   search  
 
Buy the book!


GNU Emacs Calc 2.02 Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.7.69 Programming Tutorial Exercise 11

First we define a dummy program to go on the z s key. The true z s key is supposed to take two numbers from the stack and return one number, so DEL as a dummy definition will make sure the stack comes out right.

 
2:  4          1:  4                         2:  4
1:  2              .                         1:  2
    .                                            .

  4 RET 2       C-x ( DEL C-x )  Z K s RET       2

The last step replaces the 2 that was eaten during the creation of the dummy z s command. Now we move on to the real definition. The recurrence needs to be rewritten slightly, to the form s(n,m) = s(n-1,m-1) - (n-1) s(n-1,m).

(Because this definition is long, it will be repeated in concise form below. You can use M-# m to load it from there.)

 
2:  4        4:  4       3:  4       2:  4
1:  2        3:  2       2:  2       1:  2
    .        2:  4       1:  0           .
             1:  2           .
                 .

  C-x (       M-2 RET        a =         Z [  DEL DEL 1  Z :

 
4:  4       2:  4                     2:  3      4:  3    4:  3    3:  3
3:  2       1:  2                     1:  2      3:  2    3:  2    2:  2
2:  2           .                         .      2:  3    2:  3    1:  3
1:  0                                            1:  2    1:  1        .
    .                                                .        .

  RET 0   a = Z [  DEL DEL 0  Z :  TAB 1 - TAB   M-2 RET     1 -      z s

(Note that the value 3 that our dummy z s produces is not correct; it is merely a placeholder that will do just as well for now.)

 
3:  3               4:  3           3:  3       2:  3      1:  -6
2:  3               3:  3           2:  3       1:  9          .
1:  2               2:  3           1:  3           .
    .               1:  2               .
                        .

 M-TAB M-TAB     TAB RET M-TAB         z s          *          -

 
1:  -6                          2:  4          1:  11      2:  11
    .                           1:  2              .       1:  11
                                    .                          .

  Z ] Z ] C-x )   Z K s RET      DEL 4 RET 2       z s      M-RET k s

Even though the result that we got during the definition was highly bogus, once the definition is complete the z s command gets the right answers.

Here's the full program once again:

 
C-x (  M-2 RET a =
       Z [  DEL DEL 1
       Z :  RET 0 a =
            Z [  DEL DEL 0
            Z :  TAB 1 - TAB M-2 RET 1 - z s
                 M-TAB M-TAB TAB RET M-TAB z s * -
            Z ]
       Z ]
C-x )

You can read this definition using M-# m (read-kbd-macro) followed by Z K s, without having to make a dummy definition first, because read-kbd-macro doesn't need to execute the definition as it reads it in. For this reason, M-# m is often the easiest way to create recursive programs in Calc.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003