How to get the nth
element of a list? Equivalent to (nth n lst)
from emacs-lisp
list-ref
I’ve been poking this leetcode problem in a literate programming style every few nights for the last few weeks and just finished it https://github.com/togakangaroo/daily-programmer/tree/master/island-perimeter
Any kind soul want to give me a free code review in exchange for internet good samaritan points?
I’m more concerned about this being idiomatic than I am about the general approach. I know the approach itself isn’t the slickest. I did spend waay too long last night lying in bed thinking how I might simplify things with a convolution pass from a laplacian filter
apologies ahead of time for github’s org rendering being shitty. If you have a configured emacs and pull it down it is much nicer, but this is still legible
now that I think of it, I think there might be a subtle bug when an island forms a pincer that connects on a diagonal. Technically the rules allow that and I suspect it breaks my algorithm
This is a great write-up!
thank you. I’ve been practicing trying to do this stuff in org for a while
Random typo: indicies-of-cells-adjacent-to-move>
-> indices-of-cells-adjacent-to-move>
dammit, I think I misspelled the word indices
every time I wrote it in that document. Flyspell was yelling at me nonstop
Note that (require racket/match)
(require racket/format)
can be written (require racket/match racket/format)
You specifically mention that you are interested in being idiomatic. I think, you will see that most programs use “a vector of vectors” instead of a matrix to represent two-dimensional arrays. I like your approach though.
yeah, some of this was just me spiraling the rabbit hole of finding the right data structure. I do think a matrix is ultimately the most helpful one though since I can iterate the indicies as a flat list
that’s real helpful
An alternative design would be to make #lang island-perimeter
\| \| \| X \| \| \|
\| X \| X \| X \| X \| \|
\| X \| X \| X \| X \| \|
\| X \| X \| X \| X \| \|
\| \| \| X \| \| \|
\| X \| X \| X \| \| \|
expand into a module that contains (provide island)
(define island <your-representation-of-the-island>)
That makes it possible to write a program like: (require "example-1.rkt")
(print-island island)
(perimeter island)
That is, if you are in a situation, where the data can be used in multiple ways, it makes sense to split the “parsing-into-a-data structure” and the “calculation” parts of the program.
oh yeah, I like that definitely
That’s the way Scribble works internally. If you run a #lang scribble
program, you get a module that exports an identifier doc
which holds a representation of the input. Then the raco scribble
command can render the documentation into various formats such as pdf or html from the same data structure.
How was the experience of using org files to do this?
It’s ok. I’m pretty comfortable with org and parinfer which I rely on a lot. I am also good at vim keys so navigation is where I want it.
I did have to invent kind of my own ob-racket
to make this work. The trick is that you have to emit modules like the reader and expander as adjacent files in the same directory so I had to write my own headers to do it
I should probably pull it out into its own extension so that its not tied to racket though
I think that overall the REPL-driven workflow works in a notebook and noweb is almost powerful enough to do everything I need it to. Being able to do stuff like pop out the editing window into a side-by-side and be able to execute that in a keystroke as I’m editing and having that write directly into the notebook on the other side was pretty nice
Have you seen the blog posts of Jay? He also likes the literal programming style. https://jeapostrophe.github.io/
nope. Pretty old, but always down to read conway’s life articles :D
It’s worth scanning the entire list: https://jeapostrophe.github.io/archive.html
cool, thanks!
Racket has a literal-programming lang that co-operates with scribble, IIRC
yup, thats the stuff soegaard2 was talking about above. I might look into that, but scribble only works with racket - whereas org works with all languages, isn’t going to render in a github document, and I’m not sure to what degree it integrates with emacs.
Well, renders to HTML isn’t bad, and probably could be made to work with other langs in racket, but that’s fair.