Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Style Issues in Lisp and Scheme programming, setq versus let ... and onion structure with multiple cores or eyes or kernels Re: string to list or string to array

169 views
Skip to first unread message

Swami Tota Ram Shankar

unread,
Oct 22, 2012, 9:16:58 PM10/22/12
to
On Oct 21, 1:22 pm, "Pascal J. Bourguignon" <p...@informatimago.com>
wrote:
> Swami Tota Ram Shankar <tota_...@india.com> writes:
>
> > On Oct 21, 4:16 am, "Pascal J. Bourguignon" <p...@informatimago.com>
> > wrote:
> >> Since the syntax of this text is exactly the printed representation of
> >> an emacs lisp vector, you can read it directly with read.
>
> >> (defun get-vectors-from-buffer ()
> >>    (let ((vectors '()))
> >>      (goto-char (point-min))
> >>      (while (re-search-forward "\\[[0-9.+-\\ ]+\\]" nil t)
> >>         (goto-char (match-beginning 0))
> >>         (push (read (current-buffer)) vectors))
> >>      (nreverse vectors)))
>
> >> ;; [1 2 3.0] [4 5 6]
> >> ;; [7 +8 -9]
>
> >> (get-vectors-from-buffer)
> >> --> ([[0-9\.+-\\] +]
> >>      [[0-9\.+-\\] +\\]
> >>      [17.16 -17.16 17.16 17.16 17.16 17.16 17.16 17.16]
> >>      [[0-9\.+-\\] +\\]
> >>      [1 2 3.0]
> >>      [4 5 6]
> >>      [7 8 -9])
>
> > Hi Pascal, Thanks for the reply but there are some problems. I
> > understood your change to regexp.
>
> > However, you are doing too much in your and I need only read one
> > string at a specific location at a time, not the whole buffer.
>
> > Thus, I am having difficulty and need help. Let me write the modified
> > story again.
>
> > (when (looking-at "\\[[0-9.+-\\ ]+\\]")
> > (forward-char (+ (string-width (match-string 0)) 1))
> > (setq V (match-string 0))
> > (setq V (read-from-string (intern (match-string 0))))
>
> 0- What's the purpose of moving the point?
> 1- Don't use setq, use let.
> 2- Don't bind two different things to the same variable!
> 3- read-from-string reads from a string, not a symbol, so why are you
>    interning a symbol?
>
> Let me advise you to read:
>
>           An Introduction to Programming in Emacs Lisp
>          http://www.gnu.org/software/emacs/emacs-lisp-intro/ or  M-: (info "(eintr)Top") RET
>
>    (defun get-vector-at-point ()
>      (when (looking-at "\\[[0-9.+-\\ ]+\\]")
>        (goto-char (match-beginning 0))
>        (read (current-buffer))))
>

Hi Pascal,

I like your advice above a lot, especially concerning setq versus let.

I will later ask you to elaborate more on your point (2), however, for
now I want to focus solely on point (1).


I had to write a wrapper function to one I was writing and I soon ran
into a name space conflict because of setq. Temporarily I relieved,
due to the urgency to complete the task, by renaming the variables.

However, I want an expert such as you to address some style issues in
lisp programming. For this reason, I am renaming my post as

Style Issues in Lisp and Scheme programming, setq versus let

Here are my questions.

Does setq cause some kind of permanent persistence of the variable
like a global?

In the lingo of computer science, such as lexical scoping, dynamic
scoping, static binding, dynamic binding, what is its meaning and what
is the meaning of these four terms?

Even more important question is the impact on program structure and
readability.

For example, the imperative programming style, flows nicely from top
to bottom. The eye has to move L->R and top to bottom to understand
the screenful module, as in C.

I have found that let, and functional style leads to an onion type
structure.

f(g(h(x,y),z,k(alpha,beta,l(theta)))w)

If it is short, its not an issue, but the multiple cores or eyes of
the onion, makes it impossible to grasp the structure.

I ask you to focus your brain intensely and come up with some
COMPELLING rules of thumb, so I can write programs that are readable.

I know, some of you are such brilliant minds that there will not
emerge one in maybe 100 years as smart as you, so write these rules
for newbies.

and sprinkle some examples.

I want to apply to my own thing, but you also tell us how to read
these programs since when I look at yours, it becomes digestable only
after persisting with it and going to the cores and reading inward
out.

Whats your method or habit of reading?

Swami



Swami Tota Ram Shankar

unread,
Oct 22, 2012, 11:02:00 PM10/22/12
to
On Oct 22, 7:36 pm, PJ Weisberg <p...@irregularexpressions.net> wrote:
> On Mon, Oct 22, 2012 at 6:16 PM, Swami Tota Ram Shankar
>
> <tota_...@india.com> wrote:
> > For example, the imperative programming style, flows nicely from top
> > to bottom. The eye has to move L->R and top to bottom to understand
> > the screenful module, as in C.
>
> > I have found that let, and functional style leads to an onion type
> > structure.
>
> > f(g(h(x,y),z,k(alpha,beta,l(theta)))w)
>
> That could be valid C code, but it's probably not Lisp code, unless
> you really do have a function named "x,y".
>
> > If it is short, its not an issue, but the multiple cores or eyes of
> > the onion, makes it impossible to grasp the structure.
>
> Just one small tip: when reading lisp code, pay more attention to the
> indentation then to the parentheses.  Of course the
> compiler/interpreter doesn't pay attention to the indentation, but the
> author probably did.
>

Can you suggest some skeleton

(defun function (var1 var2 var3)
""
(interactive "swith strings and \ns to allow suggested defaults and
avoid typing and simply RTN")

taking care of defaults if null or ""

(let* (()()())



)

)


btw, lisp's prefix notation, (f x y z) is more expressive and
convenient for expressing currying (((f x) y) z) than f(x y z).

I expect incisive and penetrating analysis of code organization and
using that to improve readability.


Pascal J. Bourguignon

unread,
Oct 23, 2012, 4:15:07 AM10/23/12
to
Swami Tota Ram Shankar <tota...@india.com> writes:

> Here are my questions.
>
> Does setq cause some kind of permanent persistence of the variable
> like a global?
>
> In the lingo of computer science, such as lexical scoping, dynamic
> scoping, static binding, dynamic binding, what is its meaning and what
> is the meaning of these four terms?

See http://www.informatimago.com/articles/usenet.html#Variables

> I have found that let, and functional style leads to an onion type
> structure.
>
> f(g(h(x,y),z,k(alpha,beta,l(theta)))w)
>
> If it is short, its not an issue, but the multiple cores or eyes of
> the onion, makes it impossible to grasp the structure.

No, the issue is when it's short, with short, unmeaningful names.

If you use whole words and whole propositions as identifiers, then it
can read as English.

(mapcar (function list) (get-keys alist) (get-values alist))

vs.

(m (f l) (k a) (v a))



> I ask you to focus your brain intensely and come up with some
> COMPELLING rules of thumb, so I can write programs that are readable.

Sorry, I don't have the time right now.


> I want to apply to my own thing, but you also tell us how to read
> these programs since when I look at yours, it becomes digestable only
> after persisting with it and going to the cores and reading inward
> out.

The functions I presented you in this thread were procedural. You may
be confused because in lisp, scope is limited to parentheses too.

(when cond
expr1
expr2)

is different from:

(mapcar fun
expr1
expr2)

You have to read the operator first. That's why it's in the first
position in the lists: it's the most important thing you have to read to
understand lisp code. And that's why lisp operators are rarely cryptic
characters or single-letter. (The exceptions being +, -, *, etc, but
originally it was PLUS, MINUS, MULTIPLY, etc).

So when you read "when", you should know that it's a macro and that it
has some specific rules of evaluation of its arguments. You should then
read those arguments following those rules of evaluations. Namely, when
evaluates its first argument, and if it returns true, then it evaluates
in sequence the other arguments. This is not functional, this is
procedural!

On the other hand, when you read "mapcar", you should know that it's not
a macro or a special operator, so it's a function, and therefore all its
arguments are evaluated in order, and passed to the function that's
called last. Here you have an "onion", since you have to read inside
out to follow the flow of control.

But with macros and special operators, the flow of control can be
anything the macro is designed to implement. So you have to know your
macros and special operators.



> Whats your method or habit of reading?

Read the reference.

Common Lisp:
http://www.lispworks.com/documentation/HyperSpec/Front/index.htm


emacs lisp:
An Introduction to Programming in Emacs Lisp
http://www.gnu.org/software/emacs/emacs-lisp-intro/ or M-: (info "(eintr)Top") RET

Emacs Lisp Manual
http://www.gnu.org/software/emacs/manual/elisp.html or M-: (info "(elisp)Top") RET


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

Stefan Monnier

unread,
Oct 24, 2012, 11:03:28 AM10/24/12
to
> btw, Lisp's prefix notation, (f x y z) is more expressive and
> convenient for expressing currying (((f x) y) z) than f(x y z).

With all due respect to Lisp, that's not true. Curried calls in the
non-Lisp syntax are simply "f(x)(y)(z)" or even better "f x y z".


Stefan

John Cowan

unread,
Oct 24, 2012, 11:47:31 AM10/24/12
to
On Tuesday, October 23, 2012 4:15:11 AM UTC-4, Pascal J. Bourguignon wrote:

> So when you read "when", you should know that it's a macro

But, alas, there's no way to do that without searching the manual plus all your source code.

One of the things Kernel got right is that by convention you write $if, $when, $lambda, etc.

damosan

unread,
Oct 24, 2012, 1:56:19 PM10/24/12
to
On Wednesday, October 24, 2012 11:47:32 AM UTC-4, John Cowan wrote:

> One of the things Kernel got right is that by convention you
> write $if, $when, $lambda, etc.

To me syntatic sugar like this is a crutch. If you know the language having to type extra '$' is a pain in the butt.

D.

Christian Kellermann

unread,
Oct 24, 2012, 1:59:55 PM10/24/12
to
By convention means noone forces you to do this. You don't have to mark
globals with '*' on both sides either, yet it has been shown to be
benefitial for those who read the code...

John Cowan

unread,
Oct 24, 2012, 5:01:30 PM10/24/12
to
On Wednesday, October 24, 2012 1:56:19 PM UTC-4, damosan wrote:

> To me syntactic sugar like this is a crutch. If you know the
> language having to type extra '$' is a pain in the butt.

That's fine in languages with fixed syntax, but you can always add new syntax to Scheme. I doubt if anyone has memorized all the syntax keywords available in Racket, for example, though there are some conventional naming patterns: "define-foo" is usually syntax, for example.

gnui...@hotmail.com

unread,
Oct 24, 2012, 11:20:32 PM10/24/12
to
As an aside you show the notation. well and good. But I realize that
there is no executable substitute or ability to return a curried
function
in emacs. Consider these forms.

(+ 2 3 4) ;; executable
(((+ 2) 3) 4) ;; not executable
(cons '+ (cons 2 (cons 3 (cons 4 nil)))) ;; ditto
(eval '(+ (cons 2 (cons 3 (cons 4 nil))))) ;; ditto

Can anyone suggest an executable version of first in terms of curried
addition?

Since Pascal is busy, and David Kastrup is not seen for quite some
time, I hope someone can give a reply to the question of the thread.

G

Pascal J. Bourguignon

unread,
Nov 2, 2012, 5:32:56 PM11/2/12
to
But actually, it is easy to remember the small list of "fundamental" macros
and special operators, and to follow a few other conventions: we don't
prefix macro names with $ but instead with def, do, or with-.
--
__Pascal J. Bourguignon__

Pascal J. Bourguignon

unread,
Nov 21, 2012, 4:56:55 PM11/21/12
to
Well, if you want to keep the variadicity of the operation, it's hardly
possible, because you can only return either a number or a function. In
lisp, numbers are not applicable functions.

Otherwise the following should work, in emacs-24:

;; in emacs-24:
(setq lexical-binding t)
(defun plus/2 (arg) (lambda (x) (+ arg x)))
(defun plus/3 (arg) (lambda (y) (plus/2 (funcall (plus/2 arg) y))))

(funcall (plus/2 2) 3) --> 5
(funcall (plus/3 2) 3) --> #<some closure>
(funcall (funcall (plus/3 2) 3) 4) --> 9

WJ

unread,
Nov 22, 2012, 9:40:51 PM11/22/12
to
Pascal J. Bourguignon wrote:

> Swami Tota Ram Shankar <tota...@india.com> writes:
>
> > Here are my questions.
> >
> > Does setq cause some kind of permanent persistence of the variable
> > like a global?
> >
> > In the lingo of computer science, such as lexical scoping, dynamic
> > scoping, static binding, dynamic binding, what is its meaning and what
> > is the meaning of these four terms?
>
> See http://www.informatimago.com/articles/usenet.html#Variables
>
> > I have found that let, and functional style leads to an onion type
> > structure.
> >
> > f(g(h(x,y),z,k(alpha,beta,l(theta)))w)
> >
> > If it is short, its not an issue, but the multiple cores or eyes of
> > the onion, makes it impossible to grasp the structure.
>
> No, the issue is when it's short, with short, unmeaningful names.
>
> If you use whole words and whole propositions as identifiers, then it
> can read as English.
>
> (mapcar (function list) (get-keys alist) (get-values alist))


Racket:

(dict-map '((a . 3) (b . 2) (c . 1)) list)
--> '((a 3) (b 2) (c 1))


>
> vs.
>
> (m (f l) (k a) (v a))
>
>
>
> > I ask you to focus your brain intensely and come up with some
> > COMPELLING rules of thumb, so I can write programs that are readable.
>
> Sorry, I don't have the time right now.
>
>
> > I want to apply to my own thing, but you also tell us how to read
> > these programs since when I look at yours, it becomes digestable only
> > after persisting with it and going to the cores and reading inward
> > out.


Clojure:

Right to left:

user=> (Math/exp (Math/sqrt (Math/abs -8)))
16.9188286785579

Left to right:

user=> (-> -8 Math/abs Math/sqrt Math/exp)
16.9188286785579

0 new messages