• 0 Posts
  • 4 Comments
Joined 11 months ago
cake
Cake day: October 16th, 2023

help-circle
  • jacobb11@alien.topBtoLispConstructing a macro 'for'
    link
    fedilink
    English
    arrow-up
    1
    ·
    10 months ago

    Here it is necessary to refer to the modified sh. Therefore, it is passed to the recursive param-name call.

    You can bind a variable outside the function and still refer to it inside the function. Here is an example that does so with “,end”:

    (defmacro for ((param-name start-value end-value &optional (step1 1)) &body body)
      (let* ((func-name (gensym))
             (start (gensym))
             (param-name (gensym))
             (comparison (if (< step1 0) '< '>))
             (end (gensym))
             (step (gensym))
             (k (gensym)))
        `(let ((,end ,end-value))
          (labels ((,func-name (,param-name ,start ,step ,k)
                       (let ((new-exprs (progn ,@body))
                             (newK (+ 1 ,k)))
                         (if (,comparison ,end ,param-name)
                             (,func-name (+ ,param-name ,step) ,start ,step newK)
                             newK))))
             (,func-name ,param-name ,start-value ,step1 0)))))
    

  • jacobb11@alien.topBtoLispConstructing a macro 'for'
    link
    fedilink
    English
    arrow-up
    1
    ·
    10 months ago

    Any particular reason for the macro to use recursion instead of “do”?

    I don’t understand why the function “,func-name” has both a “,param-name” and “,start” parameter. I think (maybe misuderstanding intent) that you should drop “,param-name” from the last line and drop “,start” from function “,func-name”.

    Hm. “,end” never changes, so it would probably be simpler to declare it outside “,func-name” rather than pass it around. Same for “,step”. And maybe “,k”, but I’m not clear why “,k” is needed as well as “,param-name” and “start”.

    Also you need to change + to either + or - depending on “(< step1 0)”.


  • I find your implementation of lists with shared vectors rather than cons cells very interesting. However, I am surprised that function cons (prepending an element the front of the list) is relatively inefficient, since its performance is assumed by so much code to be constant time. Have you considered changing the storage underlying the shared vectors from arrays to rings? It’s not a fully baked idea, but I think it would permit prepending an element to be as efficient as appending one.


  • jacobb11@alien.topBtoEmacsnil or 'nil once worried me
    link
    fedilink
    English
    arrow-up
    1
    ·
    11 months ago

    I feel like that image is distracting from the point.

    Nil is a value and also a symbol. (In some Lisps. Definitely in Common Lisp.)

    Nil may be written as a symbol nil or it may be written as an empty list (). Same thing either way. Nil evaluates to itself. In contrast, most symbols (but certainly not all) evaluate to their bound value. [I’m aware that statement is a simplification, gotta start somewhere.]

    In any evaluation context, a symbol such as foo is evaluated. Quoting that symbol means evaluation “consumes” the quote and leaves just the symbol. So the evaluation of 'j is j, and so forth. Naturally the evaluation of 'nil is nil. But without evaluation, 'j differs from j and 'nil differs from nil.