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

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

Re: Writing to buffer/file


From: naugiedoggie
Subject: Re: Writing to buffer/file
Date: Wed, 08 Dec 2010 15:28:31 -0000
User-agent: G2/1.0

On Sep 14, 10:14 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> Michael Powe <michael+g...@trollope.org> writes:

> Even before that, you're misunderstanding the basic syntaxis for Lisp.

> To call a function in lisp, we write a LIST, whose first element is the
> name of the function, and whose other elements are the arguments to
> that function.

Every journey begins with a step.

> To write a list, we write an open parenthesis, the elements of the list
> separated by spaces, and a close parenthesis.
>
> sexp ::= atom | '(' {sexp} ')' .
>
> function-call ::= '(' function-name {argument} ')'
>
>     (hash-to-list (myhash mylist))
>
> Since myhash is not the name of a function the subexpression
>
>     (myhash mylist)

Why doesn't it through an error? e.g. '(void-function myhash)'?

> is meaningless, and since hash-to-list is defined to take two arguments,
> this function call is wrong since you're trying to pass only one
> (erronous) argument.
>
> Finally, as you have noted, arguments are passed by value, so when you
> call:
>
>     (hash-to-list myhash mylist)
>
> hash-to-list has no way to modify mylist.  I assume this is why you're
> correctly returning from hash-to-list the local variable mylist which
> contains the result of hash-to-list.   In any case, you will have to
> rebind this result to the mylist of extract-ini-settings:
>
>    (setf mylist (hash-to-list myhash mylist))
>    (insert mylist)
>
> or even better, just insert it directly:
>
>    (insert (prin1-to-string (hash-to-list myhash mylist)))
>
> Notice that insert can insert only strings, so you have to convert your
> list into a string first.
>
> Since mylist is bound to nil, there's not really any point in passing it
> to hash-to-list.  

I took that hash-to-list function from Xah Lee's emacs pages.  I did
change it to pass in the list variable.

> So you could write:
>
> (require 'cl)
>
> (defun hash-to-list (hashtable)
>   "Return a list that represent the hashtable."
>   (let ((result '()))
>      (maphash (lambda (kk vv) (push (list kk vv) result))
>               hashtable)
>      result))
>
> (let ((h (make-hash-table)))
>    (setf (gethash :one h) 1
>          (gethash :two h) 2
>          (gethash :six h) 6)
>    (insert (prin1-to-string (hash-to-list h))))
>
> inserts:  ((:six 6) (:two 2) (:one 1))
>
> In general, it is better of you can write functionnal code, that is, if
> you can avoid modifying the bindings of the variables (avoid using setq
> of setf).    This means that when you use let, you SHOULD give a value
> to be bound to each variable, and not change it thereafter.
>
> Also, it would  be better if you parameterized  your functions, ie. avoid
> putting constants, such as pathnames inside your functions.
>
> It would be nice if you avoided to to define variables that you don't
> use.  (But do define variables that you use!).

Yes, a lot of trying things on preceded the mail.

Well, a lot to think about.  You've given me a good path to a
successful conclusion.  The trick for returning to the starting point
-- I was already thinking about how to do that, as my list of data
values will include a half-dozen items.

I'm trying to get to the functional paradigm.  Recently, I started
studying Clojure, but I don't know enough of it to try something like
this.  I kind of figure, "hey, it's Lisp."  I've been using emacs for
many years, and I know enough elisp to get myself in trouble.  As you
can see.

Your help is much appreciated.

Thanks.

mp


reply via email to

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