help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: List not getting filled up


From: tpeplt
Subject: Re: List not getting filled up
Date: Sun, 30 Jul 2023 10:28:11 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)

uzibalqa <uzibalqa@proton.me> writes:

> I have the recursive function 'permutor'.  When the condition '(<= k 1)' is 
> reached I add
> word to the list 'mylist'.  I only get one value, yet the '(message "%s" 
> word)' give me all
> the words that I need.  What is happening and what can I do ?
>
> (defvar mylist '())
>
> (defun permutor (k word)
>   "Generate all permutations of WORD."
>
>   (let ( (i 0) )
>
>     (if (<= k 1)
>         (progn
>           (message "%s" word)
>           (add-to-list 'mylist word))
>
>       (while (< i k)
>         (permutor (1- k) word)
>
>         (if (evenp i)  ; even integer
>             (setq word (swap word i (1- k)))
>
>           (setq word (swap word 0 (1- k))))
>
>         (setq i (1+ i)))) )
>
>       word)

 1. Provide a specification of the problem that you want your
    procedure to solve.  For example, provide calls to your
    procedure with a range of values and the results that you want
    your procedure to produce:

    (permutor 0 'aword) should yield => what?
    (permutor 1 'aword) should yield => what?
    (permutor 5 'aword) should yield => what?
    (permutor 9 'aword) should yield => what?

 2. Compiling an Emacs Lisp file can provide useful information
    about possible problems with the code.  Once an Emacs Lisp
    buffer has been saved to a file (usually with the filename
    extension ".el"), it can be compiled using the "Byte-compile
    This File" menu entry in the Emacs-Lisp menu.  Alternatively,
    you can use the command M-x byte-compile-file.  When this is
    done on your code, the compiler tells you that the function
    ‘swap’ is not known to be defined.

 3. Emacs provides ‘edebug’, a debugger which allows you to step
    through your code, examining values of variables as you go so
    that you can compare what you expect to happen with what is
    happening.  To use edebug, you will need to "instrument" the
    procedure that you want to step through and then invoke the
    procedure with your desired arguments.  Again, the Emacs-Lisp
    menu provides a menu entry for you to do this, "Instrument
    Function for Debugging", and it documents that you can do this
    using the keystroke combination C-u C-M-x (when point is
    located in the procedure).  An introduction to edebug can be
    found in the Introduction to Emacs Lisp, which can be read via
    the menu path: Help -> More Manuals -> Introduction to Emacs
    Lisp.  Or, you can go directly to the debugging section with
    the command:

       M-: (info "(eintr) Debugging")

    The full documents on Emacs two debuggers can be read in the
    Emacs Lisp reference manual

       M-: (info "(elisp) Debugging")

--



reply via email to

[Prev in Thread] Current Thread [Next in Thread]