8 ms·
(Risp (In (Rust) (Lisp)))
- fulafel 5y agoHow does this end up managing memory? I would guess things are just allocated from the Rust heap and not freed?
- brundolf 5y agoLooks like they use Rc<>
- skavi 5y agoPrevious discussion here: https://news.ycombinator.com/item?id=19810504 https://news.ycombinator.com/item?id=19810504
- brundolf 5y agoOne weird thing I noticed is that they use a Vec instead of a linked-list to store lists. This is going to really mess with anyone who tries to do a lot of car/cdr/cons-ing. Maybe that's out of scope for this guide.
- oh_sigh 5y agoDoes anyone actually program like that besides for in lisp tutorials?
- jhgb 5y agoOf course people do that; it's very convenient.
- mumblemumble 5y agoIt is, but I do wonder if it's the most performant option. For grabbing individual elements, cadddr and friends are O(N) for linked lists while a O(1) implementation is possible for arrays. List eaters are an elegant design pattern, especially for picking apart recursive algorithms, but eating linked list wouldn't be nearly as cache-friendly as map/reduce over an array. It wouldn't get rid of all the pointer chasing, but even reducing one level of it is worth something. Perhaps, with modern language implementation technology, we could produce something like that, while also preserving persistence (at the semantic level), by, in the background, choosing mutation when there's only a single active reference to the list, and falling back to a copy-on-write discipline when there are multiple concurrent references. For what it's worth, I find I never program that way in Clojure. I realize Clojure not being as list-oriented is a point of contention for many people, and I'm not all that much of a lisper, so perhaps my opinion isn't worth much, but I've generally found that Clojure gives me most of what I want. I am more annoyed by the lack of reader macros. Though I also understand and respect Rich Hickey's decision there.
- jhgb 5y ago> For grabbing individual elements, cadddr and friends are O(N) for linked lists That doesn't really make sense. cadddr is O(1), and so are all its c[ad]{1,4}r friends. > For what it's worth, I find I never program that way in Clojure. I suspect you don't do that because the language doesn't support it well. Which is fine, since going against the grain of a language is generally inadvisable, but it says nothing about the technique in itself.
- mumblemumble 5y agoFor a linked list implementation, car requires traversing one cons cell, cadr requires traversing two, caddr three, four for cadddr, and so on. That's about as textbook an example of O(N) as you can get.
- jhgb 5y agoBut your N is bounded from above to 4, so pretty much anything using these functions won't be unbounded in time just because of use of these functions. If you're suggesting that using nth or elt on a list of arbitrary length is a bad idea, then yes, it is, but I certainly wasn't suggesting that.
- klyrs 5y agoYes, linked lists are great for certain use cases. They make for very clear and performant code, especially if your language can special-case their memory management. In other cases, they're slow garbage, and that's where vectors shine.
- fulafel 5y agoDepends on the language I guess. In Clojure, no. In Common Lisp, probably.
- dcsommer 5y agoThere are some good thoughts concerning when to use a linked list here: https://rust-unofficial.github.io/too-many-lists/ https://rust-unofficial.github.io/too-many-lists/
- brundolf 5y agoYour link talks about the general case. For better or worse, (most) Lisps give linked-list operations a special status as a fundamental primitive. For example, getting the tail of a list over and over is commonly used for iterating through its elements. If you try and do this with a Vec you'll be copying and re-allocating almost the whole thing on every loop iteration. Nothing about Lisp requires that you write code this way of course, but it's the epitome of what could be called "Lispy" (literally: "list processing language"), and any preexisting code or habits will hit a brick wall if those lists are implemented as Vecs
- dgb23 5y agoTo add, Clojure uses a sequence interface that is typically backed by a vector. Conj (instead of cons) is used to derive a new sequence with the thing added in front for lists (which are rarely used) and at the back for vectors.
- brundolf 5y agoIt supports both, as it should; Lists are still first-class citizens, it's just that vectors are too. Though also in Clojure's case vectors aren't really vectors, they're immutable persistent data structures that share memory as much as possible, which I think would actually solve most of the performance problem here. But the same is not true of Rust's Vec<>
- nerpderp82 5y agohttps://github.com/bodil/im-rs https://github.com/bodil/im-rs
- tialaramex 5y agoIn theory a linked list has lots of attractive features as a data structure and when LISP Machines were a thing that theory translated into reality pretty well. Today losing data locality hurts really badly. Rust does provide a linked list, for the handful of cases where that really is what you wanted, but almost always even though in theory a vector should be worse, in practice it's better. For example if you build a toy system with a thousand small integers on the list, you might reason that removing the 500th via a vector instead of a linked list would be awful - you'd need to shuffle 500 of them forward compared to just swapping a pointer. But wait, on today's hardware the linked list approach involves about five hundred dependent cache misses. Before you find the 500th on the list to remove it, you have stalled the CPU for so long the vector implementation would have already finished doing its shuffling of 500 items forward in memory and moved on to something else. Now, if your list has 500 million items in it, and you often want to remove just the first item while appending to the far end, a Vector is indeed a bad fit. But there are still better alternatives than a Linked List, Rust provides a Deque but unlike the one a Lisp-enthusiastic professor may have showed you in data structures class, it's actually a similar structure to a vector not a linked list.
- brundolf 5y agoI'm not here to debate the virtues of linked-lists, I'm just pointing out that they're extremely idiomatic in Lisp, so it's weird to implement a basic Lisp that doesn't use them, because anybody running idiomatic code in it will be in for a big surprise.
- 5y ago
- C-x_C-f 5y agoThat title though :-P Wish they'd gone with something like (define Risp (in Rust Lisp)) IMHO
- dgb23 5y agoInterestingly the title illustrates the big tradeoff in lisp notation. It doesn't read like English sentences from left to right, but is a tree data structure. This has upsides and downsides. Rust for example reads much more like a sequential sentences with the dot notation to chain methods and trait implementations and the like. Luckily for example in Clojure we use let bindings and threading macros (-> and ->>) to express the sequentiality of code more clearly. But it takes some time to get used to Lisp syntax in general.
- Zelphyr 5y agoI applaud the author for choosing to use the "Risp" portmanteau over "Lust".
- trutannus 5y agoReminds me of this: https://github.com/kanaka/mal https://github.com/kanaka/mal Highly recommend trying it out. Making a lisp is surprisingly easy and fun. Plus you get your own programming language out of it.
- forgotpwd16 5y ago>(In (Rust) (Lisp)) Is this "Rust in Lisp" or "Lisp in Rust"?
- C-x_C-f 5y agoPrefix notation preserves the order of the argument, so it would technically translate to "Rust in Lisp." Huh, guess that was not the intended meaning...
- aaron-santos 5y agoWe're lucky that language diffusion works both ways. I've kept my eye on Carp[1] because it takes a cue from Rust and brings deterministic memory management & ownership into a Lisp. [1] https://github.com/carp-lang/Carp https://github.com/carp-lang/Carp
- mastax 5y agoOne of my pathologies as a developer is that I want to do things "properly" at all times. When I see things like fn tokenize(expr: String) -> Vec<String> { expr .replace("(", " ( ") .replace(")", " ) ") .split_whitespace() .map(|x| x.to_string()) .collect() } my instinctive reaction is: those .replace() calls will allocate unnecessarily, what you really need is a little state machine, maybe use the nom or parsec crate .... Wanting to do things The Right Way is a good instinct to have as an engineer, and it's something that Rust encourages by design. However, I've noticed that I'll often spend a lot of time getting in the weeds trying to optimize or elegant-ize a bit of code which ends up being unnecessary. I'll take a step back and realize that the performance of that code does not matter, or that the implementation was the wrong approach and I need to delete it all and do something else, or just that it wasn't very important and I should've done the easy solution and moved on. When I'm in a flow state writing code it's hard to step back and evaluate what I'm working on in the context of the bigger picture; I haven't been successful at training myself to do that. I think a better solution would be to deliberately write "first draft" code that's biased toward being quick and easy to write. When the code is done there's a natural pause to test and review it in the context of the big picture. Does anybody else struggle with this? What have you done to mitigate it?
- mumblemumble 5y agoSpending a month or two doing really scrupulous test-driven development, perhaps? I don't personally like TDD as an end in and of itself, but I love it as a sort of kata-like practice to help ingrain good habits. In this case, where I see it potentially helping is that the tests give you a very clear, objective way of deciding when the code is done, and it's time to stop futzing with it. Though you would have to be careful about remembering that the "refactor" phase is about refactoring for maintainability, not performance. I don't know what the tooling situation is like in Rust, but another one is to try and be really scrupulous about never making a change whose purpose is performance without first profiling the code, so that you can objectively measure the impact of the change. You don't need to do this forever, either. But, if your experience is anything like my own, I'm guessing that a lot of what motivates you is the feeling that you've done something of tangible value when you optimize the code. Actually seeing some hard numbers might change your feelings a bit. It's a lot easier to feel good about, "I spent an afternoon making the code more efficient," than it is to feel good about, "I spent an afternoon reducing the time it takes this program to execute from 50ms to 49ms."
- thurn 5y agoVery nice! I think there could definitely be a market for a nice scripting language that natively integrates with Rust and doesn't require crazy huge compile times, the way Lua is used in game engines.
- akkartik 5y agoCurious that it doesn't implement quote. Would the current approach have any issues with quote?
- eointierney 5y agoThis is a great thread. A cousin of mine, a superb programmer, is C++ fluent. As in able to express any good idea in any other language in C++ efficiently. He is deep in C++. He knows a poet. They are critical, as he told me, of his ability to understand poetry. When I suggested he show them the lambda calculus he smiled. We need more poetry in code as much as we need more proof in code. I know many of us program to build, quickly and efficiently, solutions to problems, and mainly problems defined in terms of business or regulatory requirements. Nonetheless we need more poetry in code. Terse, referential, contextualised, meaningful code that uses every suitable math technique for proofiness. We build our future with code. Let's make beautiful code. In a nutshell: write less read more. Risp is great.