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

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

Re: Using append to create a list from a line of text


From: Pascal J. Bourguignon
Subject: Re: Using append to create a list from a line of text
Date: Tue, 16 Apr 2013 22:51:00 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux)

acomber <deedexy@gmail.com> writes:

> I want to create a list of words from a line of text delimitted by tabs. I
> want to basically split the line into atoms, split by tab.
>
> The code below is sort of pseudocode but is this the best approach to do
> this type of thing?
>
> Here is my first attempt:-
>
> (defun get-hdr()
>     ;obviously point must be positioned on correct line
>     (let (mylist)
>       while(not (end-of-line)
>          while(re-search-forward ("[A-Za-z]+[^\t\n]" nil t)
>            append (match-string 1) mylist      
>          )
>       )
>    )
> )

Nice, but it's not formatted correctly.  I'd avise you to use
paredit-mode.

Adding and removing newlines where one should, and letting emacs indent
the sexp, we get this text:


    (defun get-hdr()
      ;; obviously point must be positioned on correct line
      (let (mylist)
        while
        (not (end-of-line)
             while
             (re-search-forward ("[A-Za-z]+[^\t\n]" nil t)
                                append
                                (match-string 1)
                                mylist))))

Now, two obvious things:

1- undefined variable named `while'.  Where does that variable come
   from?

2- the function `not' is passed three arguments, when it expects only
   one!

3- "[A-Za-z]+[^\t\n]" is not a symbol naming an operator, nor is it a
   lambda expression, therefore the sexp ("[A-Za-z]+[^\t\n]" nil t) is
   not a lisp form.

4- undefined avariable named `append'.

5- while re-search-forward takes optionally up to four arguments, I
   doubt that mylist is bound to the count of searches to do, or that
   (match-string 1) returns whether you want or not to signal errors if
   no match is found.


> How do I get my function to return the list, mylist?

Try to read a lisp tutorial again.  On the first chapter, the basic
syntactic elements are always presented.  You should have no problem
understanding them and correcting your code.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.


reply via email to

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