I always get that backwards. In my head it is “I want to take N items from X” and I can’t fix that. Honestly, I think my version should be right. More curry friendly and it just makes loads more sense to me.
Glad you got things sorted out.
I was wondering if there was any general advice on when to use different data structures (hash, struct class). As a Java programmer I obviously have no choice but to use classes, but Scheme gives me more choice. Any thoughts?
@mark.warren I think it’s hard to give general advice. It depends on what you’re trying to accomplish and what you prefer.
For example racket/class
is a comprehensive class/OOP system. I’ve hardly used it in ~7 years of using Racket. Why? Because previously I spent many years doing GUI OOP C++, and wanted to learn different things using Racket. However, if I had to write a desktop GUI app, I would probably happily use racket/gui
and racket/class
.
@greg I thought it might be a bit too vague a question. I guess I’m still trying to wrap my head around trying to keep data immutable but at the same time still have state.
@mark.warren I would use hash tables in roughly the same places you’d use a hash table in java
I can also tell you that, unlike Clojure where people tend to use maps a.k.a. hash-tables a lot, in Racket people tend to use struct
s more. However it’s Racket so you could use hash-tables, and even create syntax for things like {}
initialization and map application etc.
Oops didn’t see samth while typing that. Agree.
and I would use structs for basically all data that you’d create a class for in Java, but if the class wouldn’t have any real “data” in it, then you just don’t create the class and have functions instead
@greg @samth Thanks, I’m probably over analysing instead of just doing stuff and seeing where I get.
@mark.warren > still trying to wrap my head around trying to keep data immutable but at the same time still have state. Yes. Coming from C++, I found this — functional, immutable — weirder than “all the parentheses”. e.g. I need to loop through something. What do you mean I can’t mutate the index or an accumulator variable? How does it even work?
@greg Yep that definitely threw me.
The short answer is that, functions don’t change the original thing. Instead they return a copy of the original, that has the changes.
Also “loops” in Racket can be recursively calling a function on successive elements of a list or whatever.
That’s part of what my head can’t get, usually (in Java etc.) copies are bad due to memory allocation and garbage collection.
Total copies are still bad for those reasons in Racket. :slightly_smiling_face: However I’d suggest, don’t worry about that at first. Pretend it’s not a big deal. Suspend your disbelief for a few days/weeks while you learn.
first think about how to best solve the problem. worry about optimization later
Jinx. :slightly_smiling_face:
Always good advice.
copying can be an issue. I once wrote a bf interpreter in Haskell using pure arrays and it was a disaster, performance-wise. later I used a monadic array to get updates in-place
in racket you can have mutation and side effects when you need them, but it’s best to try to be “functional-first” and change later if needed
Yep. Later you can learn about how much that matters, as well as how/when things use structural sharing, and how you can handle this.
Also BTW Racket is not “zealous” about this. You can set!
variables and struct
fields if you really want to. And there are mutable as well as immutable variations of hash-tables, for instance.
But. It’s more typical to avoid that unless it’s really truly necessary.
Also, any mutation is often an implementation detail, wrapped in a functional outer layer.
Sure, it’s about getting my head out of the object mind set.
So if you’ve been in Java land for awhile, let the pendulum swing all the way to the opposite side for awhile. Then eventually it can settle wherever in the middle makes sense for you. :slightly_smiling_face: Good luck!
Cheers
I made a thing!
It’s a “toy” thing, but I wanted to mention it because it never would have been possible without help from many of the fine folks in this group. Thank you, thank you, thank you! It’s also perhaps an inspiration for other beginners, showing what Racket allows even an amateur to accomplish in relatively few lines of code. I’d be happy to share the code if anyone wants to see it, but be warned that it will probably cause experienced Racketeers actual physical pain.
I hope it’s okay that I’ve attached short demo video. As background, most scholarly publications have a Document Object Identifier (DOI), which provides a unique identifier useful for bibliography management, etc. Older papers were assigned DOIs retroactively, which means I often need to look up their DOI using words from the title, author’s name, etc. Searches often return multiple potential matches to choose from.
Now, there are websites for doing so. But, they require multiple clicks and copying text to extract the required information. Where’s the fun in that, especially if one if dodging real work? Thus, I created a small app that will search for DOIs (defaulting to whatever information was in the clipboard when launched), present a list of potential matches and place the DOI of the chosen article in the clipboard.
Once I got past a few mental roadblocks (with help from here), the project was surprisingly easy and even fun. Doing it taught me a lot that will go into more serious work. Racket rocks! Thanks all.
@ghoetker Nice! :tada: