I’m a “Neovim Refugee” trying to get a deeper/better understanding of how emacs lisp works and how i can use it to expand on my emacs setup. I have never done anything in lisp before and still struggle to understand how single quotes signify a function or what ever.
With that said, i was also planning on doing AoC this year. Originally i wanted to look into zig or go, but now think that this might be the opportunity to dive into lisp for a bit.
But with knowing basically nothing: Is this even “viable”, or advisable? Should i be looking at common lisp instead? Or would you say that’s a pretty dumb idea and i should rather learn it in a different way?
I have never done anything in lisp before
Emacs has a built-in emacs lisp tutorial. That would be a good starting place.
struggle to understand how single quotes signify a function or what ever
Not exactly sure what you mean by that. Again, I recommend the manual, but you can think of quoting as a way to tell the interpreter “don’t evaluate this”.
e.g.
(prin1 (+ 1 1)) ; (+ 1 1) is evaluated, prints 2 (prin1 '(+ 1 1)) ; prints the literal list (+ 1 1)
There is also backquoting, which I recommend reading up on, too. The syntax and rules are simple, but powerful.
Is this even “viable”, or advisable?
Try it out. At worst you’ll learn something. Fretting about whether or not to give it a shot is a waste of time. I’m sure you can find previous problems and solutions in a lisp.
Should i be looking at common lisp instead?
Do a few problems in elisp, a few in Common Lisp.
Or would you say that’s a pretty dumb idea and i should rather learn it in a different way?
The only foolish idea is to spend time debating about whether or not to try learning something. No one can make that call for you. Try it and see if you like it.
FWIW, I learned Rust and Python using AoC - I don’t see why you couldn’t pick up ELisp the same way. As long as you have an idea how to solve the problem, you should be able to express that algorithm in ELisp.
The one challenge you may face is the availability of data structures in ELisp (stacks, queues, deques, etc), but I’m not an expert there, so someone else may have some pointers.
Everybody learns differently, and there’s a lot of people who learn languages through advent of code. AoC can have some problems that could take literal years to solve with a naive solution, so just keep in mind you can cancel a slow function with
C-g
and you don’t need to reboot emacs entirely.The other thing to keep in mind is that this would help you learn elisp (and general lisp syntax), but won’t teach you as much about the functionality you can do with emacs. It’s still a good place to start though, since you’ll need to know elisp to customize emacs.
Lastly I would recommend looking into Structural Editing a bit, here’s a decent article that covers the basics in vanilla emacs. This is by no means a requirement for writing elisp, but using the sexp commands makes working with all the parentheses infinitely easier.
Good luck!
Thanks for that article
Is this even “viable”, or advisable? Should i be looking at common lisp instead? Or would you say that’s a pretty dumb idea and i should rather learn it in a different way?
You could have just tried any of previous years of AOC. Just go to their webpage and start doing them.
You can do them in both elisp or cl or any other programming language, including bf. I think elisp works rather well for AOC, but it will really depend on your skills.
You can look on their reddit and see if you can find elisp solutions from previous years to see if anyone has done some AOC challanges in elisp.
How you will learn elisp depends on you. If you are discussing about learning it, you ain’t learning it, you are discussing. Programming is very similar to learning a new language, playing music or learning any other new skill. Just go and do anything, and you will learn something.
You could have just tried any of previous years of AOC. Just go to their webpage and start doing them.
I did just that, to see how “viable” Elisp would be. I solved this: https://adventofcode.com/2022/day/1
with:
(cl-labels ((split-to-numbers (lst) (--map (->> (s-split "\n" it) (-map #'string-to-number)) lst))) (skip-chars-forward " \t\n") (let* ((input (buffer-substring-no-properties (point) (point-max))) (subs (->> input s-trim (s-split "\n\n") split-to-numbers)) (sorted (cl-sort (-map #'-sum subs) #'>))) (message "Winner: %s" (car sorted))))
and then with:
(save-excursion (calc-pop (calc-stack-size)) (while (progn (skip-chars-forward " \t\n") (not (eobp))) (save-selected-window (calc-grab-region (point) (progn (forward-paragraph) (point)) nil) (calc-eval "VR+" 'macro))) (calc-pack (calc-stack-size)) (calc-vector-max nil) (calc-top-n 1))
which turned out to be ~1000 times slower, but also worked.
So u/domsch1988, it’s demonstrably doable in Elisp :)
the quote is arguably the most important part of lisp as it allows symbolic processing (without that macros and eval wouldn’t be a thing I reckon).
I’ll try to provide a weird explanation. Consider this:
five = 4
So is this a four or a five? The value is four but it’s assigned to a symbol “five”. When dealing with values, “five + five” will be 8. However, what if I don’t care about the values (at least yet)? then I’ll be manipulating the expression in terms of the elements (symbols) themselves and not what they signify (values). At that level, we don’t even know what + means. You assume it’s a math operation because of the semantics you’ve been taught. for all we know it may be something else entirely, like a variable (did you know that some languages allow you to use chinese glyphs or norse runes for variables?).
So quote operator tells lisp that you don’t want the “thing” to be evaluated into the value behind it.
(quote five)
isn’t “4”, it’s literally the symbol “five”.(quote (+ 1 2 3))
isn’t 6, it’s a list of +, 1, 2, and 3. You can eval that list so lisp executes it:(eval (quote (+ 1 2 3))
. Also consider:(quote (1 + 2))
is perfectly fine, it’s just a list, but you can’t evaluate it as lisp doesn’t have a function or operator named 1 (although you may be able to define it yourself)It was a bit of a weird explanation but i hope it helps
This
I tried it a couple years ago and recall that there were others. Lots of fun and good way to learn.
There are two ways to answer this. First, people manage to do things like AoC in pretty odd and esoteric languages all the time, so doing it in elisp is totally viable. Is it advisable? Not sure. elisp really is technically a general purpose language, but it’s really meant for constructing Emacs components and extensions. I don’t think AoC would teach you how to do that sort of thing; it would only teach you how to write AoC type problems in a very odd lisp dialect that isn’t much like most other modern lisps.
i was also planning on doing AoC this year.
The innuendo for New Yorkers is deafening.