← Back to context

Comment by kolinko

7 years ago

- you can make your own domain specific language in Lisp (e.g. imagine that you want to create a custom 'for' statement, or some kind of a new pattern matching operator)

- you can easily write programs that edit themselves

I see both of those things a lot. The problem is if you aren't already familiar with those idea, the statements mean literally nothing. They basically don't make sense.

  • These examples aren't so far out there. DSLs exist in other languages. Statements exist in other languages. A programmer who has never used macros can imagine being able to create these, even if they have no idea what exactly that would look like in a Lisp program.

    That issue is also true of any game-changing technology you haven't used yet. If you hadn't used a personal computer, we could enumerate the features and advantages, but you probably wouldn't be able to truly appreciate it until you used one yourself.

  • They're also rarely useful things to do in regular software development.

    • I wonder if we struggle needlessly though in "regular software development" - and entire jobs or teams exist because we didn't find the right way to express things in a language. OTOH you could create lisp code that is so unique and weird in it's semantics that it's hard for a new team member to get onboard. But I've seen that done in C# too which allows you to have code that writes code at runtime (not as elegantly though!).

      1 reply →

  • The idea is that all LISP code is data that is easily altered, essentially https://en.wikipedia.org/wiki/Abstract_syntax_tree encapsulated in parens.

    LISP syntax is uniform,everything(functions,data,argument lists) is nested lists of lists like (list(list(list))), so that allows altering specific places "in-the-list"(place 1,2,3...) as form of meta-programming,

    where normal syntax FOR( A IN B) would be written roughly as (for a b) where for is used as a function taking 'a' and 'b' as parameters, but in this form(where 'for' is just the first element of the list) you can have functions that "alter-the-list" by example (replace-first-list-element (for a b) replacement-part) returns (replacement-part a b).

What is the second bullet point referring to?

  • Lisp syntax in a nutshell is:

        "(" {function} [" " {argument}...] ")"
    

    If you don't want to actually run a function, like you just want a list of values, you put a single quote before it:

        "'(" [{item} [" " {item}...]] ")"
    

    So if you want to do `(1 + 2) * 3` it'd be `(* (+ 1 2))`. Parentheses abound.

    On top of that everything is a function, including if statements, assignment, and even how you define a new function. So factorial in lisp is:

        (defun factorial (x)
          (if (<= x 0)
            1
            (* x (factorial (- x 1)))
        ) )
    

    You'll notice that it ended with 5 close-parentheses. Different people have different strategies for managing that along with indents and newlines.

> you can easily write programs that edit themselves

Hah! I can do that too:

    #!/usr/bin/env python3
    #
    # script.py
    # Copyright 2019 pgcj_poster
    
    print('Hello World')
    with open('script.py', 'w') as f:
        f.write(f.read().replace('Hello World', 'Goodbye World'))

Clearly Lisp is of no use in 2019.

  • You should use '__file__' for increased generality.

    Note that you can also import a module and refer to its location on disk as 'module.__file__'. I may or may not have used this to modify a few core python modules in place.