Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Naming conventions #16

Open
lassik opened this issue Jun 10, 2021 · 9 comments
Open

Naming conventions #16

lassik opened this issue Jun 10, 2021 · 9 comments

Comments

@lassik
Copy link
Member

lassik commented Jun 10, 2021

We should probably decide on common names to use for variables in similar situations. For example:

  • Name to use for list argument to procedure (or lis or lst) by default?
  • Name to use for first list element and the rest: (let ((elem (car list)) (rest (cdr list))) ...)
  • Name to use for loops: (let loop ...)?
  • I often use inner and outer with nested loops.
  • loop and recurse are good ways to differentiate proper tail recursion (needs O(1) stack space) from general recursion (O(n) stack space).
  • ...

Haskell has a convention of using x for a list element, and xs for the list. xs is meant as the plural of x: "multiple x's". They also use y and ys.

@jcubic
Copy link
Contributor

jcubic commented Jun 10, 2021

  • I think that lst looks better.
  • For let loops, I usually use iter for tail recursion iteration. But I'm fine with any other names like loop when there is a single loop and inner/outer if there are two loops. In JavaScript, I also use loop or recur (when using named recursive IIFE).
  • for the first and the rest of the list, I think that the best is exactly that names first and rest names.

We can create CONTRIBUTING.md file that will list the common names. The file name is GitHub convention.

@lassik
Copy link
Member Author

lassik commented Jun 10, 2021

(let loop ...) is probably the most common name to call it. I haven't seen iter in other code bases so far.

@soegaard
Copy link

I am not in the target demography, but I really dislike the lst and lis abbreviations. Even just l is better.
The loop over x in xs makes much more sense to me.

@lassik
Copy link
Member Author

lassik commented Jun 11, 2021

Agreed on lst, it could be an abbreviation for either last or list. Now we're really getting into the bikeshedding :)

@jcubic
Copy link
Contributor

jcubic commented Jun 11, 2021

For last we can just use last, and leave lst for list so we don't use list name.

@lassik
Copy link
Member Author

lassik commented Jun 12, 2021

OK, since nobody else has commented, let's use lst.

I'm fine with first and rest.

Are inner/outer loop/recur reasonable compromises. recur is in Clojure, and I'm told recurse sounds like "cursing again", so recur is decent (no pun intended).

@lassik
Copy link
Member Author

lassik commented Jun 12, 2021

D'oh... Clojure's recur does not allow general recursion, only tail recursion, i.e. iteration.

@jcubic
Copy link
Contributor

jcubic commented Jun 12, 2021

I think that loop / recur is not a good idea because the names don't match (if they would use both in nested let's), if they were functions/macros they would do completely different things. An alternative is to always use inner / outer if there are two loops (if there are more they should be extracted into functions). And if there is one loop I think that it can be anything I'm fine with both recur / loop. You can pick the one you think is best.

Also need to add that the code needs to have spaces instead of tabs it may look nice in one editor but be broken on the website or in another editor. One person added a code snippet with tabs and looked like broken indentation but he only use 4 stops in the editor and I think that the browser uses 8, because replacing the tabs with 4 space fixed the indentation issue,

@lassik
Copy link
Member Author

lassik commented Jun 12, 2021

I think that loop / recur is not a good idea because the names don't match (if they would use both in nested let's), if they were functions/macros they would do completely different things.

The idea is that looping and recursion are different things; looping is O(1) tail recursion, general recursion is O(n). But Clojure has made this confusion because its recur is not real recursion, so I agree that it's best to avoid in a cookbook.

An alternative is to always use inner / outer if there are two loops

Works for me. Or name them according to what they do.

(if there are more they should be extracted into functions).

Agreed.

And if there is one loop I think that it can be anything I'm fine with both recur / loop. You can pick the one you think is best.

(let loop ...) is probably the most standard. There's even http://letloop.xyz/ :)

Also need to add that the code needs to have spaces instead of tabs it may look nice in one editor but be broken on the website or in another editor. One person added a code snippet with tabs and looked like broken indentation but he only use 4 stops in the editor and I think that the browser uses 8, because replacing the tabs with 4 space fixed the indentation issue,

Agreed. It makes no sense to use tabs in Lisp code, since the 2-space indent is universally accepted.

lassik added a commit that referenced this issue Jul 7, 2021
lassik added a commit that referenced this issue Jul 7, 2021
- The (if (null? result) ...) check can be done once, before the loop.
  It doesn't need to be done separately on each iteration.

- Carrying the result as a string, and using string-append on each
  iteration, is probably more efficient than gathering the strings into
  a temporary list. Avoiding apply will also avoid implementation limits
  on how many args can be passed to a procedure (if list is very long,
  it can exceed the limit).

- Use the names `lst` and `loop` as decided in #16.
lassik added a commit that referenced this issue Jul 7, 2021
- The (if (null? result) ...) check can be done once, before the loop.
  It doesn't need to be done separately on each iteration.

- Carrying the result as a string, and using string-append on each
  iteration, is probably more efficient than gathering the strings into
  a temporary list. Avoiding apply will also avoid implementation limits
  on how many args can be passed to a procedure (if list is very long,
  it can exceed the limit).

- Use the names `lst` and `loop` as decided in #16.
lassik added a commit that referenced this issue Jul 31, 2021
list -> lst
iter -> loop
lassik added a commit that referenced this issue Aug 3, 2021
list -> lst
iter -> loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants