4 ms·
I'm doing this year's advent of code with literate org-mode buffers using common lisp. Been a surprising amount of fun. And I'm coming around to the idea that
by taeric 5y ago
I'm doing this year's advent of code with literate org-mode buffers using common lisp. Been a surprising amount of fun. And I'm coming around to the idea that the loop macro is where most languages are ultimately converging.
That and format. Which is remarkable. Certainly dense, but in a "this code is clearly responsible for making a string of that list, serves no other purpose." Something lacking in basically every other language, where a depressing amount of coffee exists simply to make a string of a list of objects. (Amusing, as Java finally got streams, which can look like LOOP, but with more periods. And probably more parens...)
- reikonomusha 5y agoMany folks are doing AoC in Common Lisp and Coalton (a statically typed DSL embedded in Lisp, so you can mix and match static and dynamic code). Many are hanging out on Discord [0] if you're interested in dropping by! [0] https://discord.gg/MCWrVfnJ https://discord.gg/MCWrVfnJ
- medo-bear 5y agoim having a lot of fun using common lisp for aoc too. for fun, my approach is to only use lists and standard lib operations on them. i am really amazed at how elegant and powerful the loop, map*, and reduce operations are. one thing i found that i needed to be mindful of are the different quality operators [0]. [0] http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node74.html http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node74.html
- taeric 5y agoYeah, I'm not using any libraries. Hardest part, typically, is parsing the input in. Mainly because I don't know how. Found some fun dangerous ways that work for me
- Jtsummers 5y agoI just straight used `read` for several of the days. It worked very well, even if it lacked all safety checks you might normally want.
- taeric 5y agoYeah, that is the dangerous way. :) I actually really like it, oddly enough.
- Jtsummers 5y agoI think I used it for the bingo day, too. cl-ppcre for the first line (comma separated list of numbers) and then 5x5 loops calling read over and over to fill out the bingo cards. Why jump through parsing hoops when the language will do it for you? I've also been solving them in Ada. It's not as convenient as just read, but if you instantiate an IO package for a type you can do the same kind of thing. So I made an enumeration and an IO package: type Command is (Forward, Up, Down); package Direction_IO is new Enumeration_IO(Direction); use Direction_IO; Then reading each line was just: C: Command; D: Integer; Get(File, C); Get(File, D); Other than the setup (3 extra lines), it's as easy as the Lisp version to use.
- taeric 5y agoI also had fun for the long csv lines by making a temp readtable that set comma as whitespace and then just read the numbers in.
- na85 5y agoMAPCAR and UIOP:READ-FILE-LINES or READ-FILE-STRING will get you most of the way there.
- taeric 5y agoStandard (read stream nil nil) in a loop has been working rather well. I originally was just using read-line and then for char across line. That isn't hard. Just has been a significant portion of some solutions. Granted... The solution for some is a four line loop...
- DickieStarshine 5y agoI use this: (defun get-file (filename) (with-open-file (stream filename) (loop for line = (read-line stream nil) while line collect (parse-integer line))))
- taeric 5y agoI've so far had good mileage with basically the following: (with-input-from-string (stream "1,2,3") (let ((*readtable* (copy-readtable))) (set-syntax-from-char #\, #\ ) (loop for num = (read stream nil nil) while num collect num)))
- bloopernova 5y ago> literate org-mode buffers using common lisp That sounds like a blast, I wish I'd thought of that!
- medo-bear 5y agoi think that orgmode notebook programing generally beats anything else out of the water, including jupyter. i actually dont uderstand why it hasnt gotten bigger attention in the communities thatvuse jupyter as for cl, if you enjoy learning it in this way you can convert norvig's paip book from md to org format using pandoc https://github.com/norvig/paip-lisp https://github.com/norvig/paip-lisp
- Jtsummers 5y agoOrg babel is great, but it's emacs only. That's why it's not used much compared to Jupyter and others. Also, there is at least one thing that would make it better: Dependency analysis across blocks for automatic re-execution. Of course, that's probably non-trivial to do across every language that org-babel supports. Pluto does this for Julia, you can break it (I have) by using certain styles in the code block (making it hard for it to detect the use) but it is nice if you work with it. org-babel is also nice even if you aren't doing interactive programming, though. I used it to dissect a complex (150k SLOC or so) C++ program that had piss poor "documentation" (auto-generated UML diagrams and similar garbage). By converting each file to a giant source block and then extracting portions I was able to make sense of it much faster than just reading the raw code. I also left the team with my full notes which amounted to a couple volumes worth of material (code + prose) in the end.