Ask HN: What is the coding exercise you use to explore a new language?

74 points by sam345 ↗ HN
What is a good sample program or project to work on to explore the primary aspects of a programming language? Something that can be worked on for a couple hours or days. Something that will give exposure to most of the basics of a particular language. Would love to know how people go about trying out a new language in this way.

72 comments

[ 4.2 ms ] story [ 130 ms ] thread
My focus is usually from a game dev standpoint. I might see how quickly I can put together an A* algorithm on a 2d array. Or Conway's game of life as a similar, how easily can I process these things.

Should note I often work on 2d tile based games.

I don't think a try-this-language-project makes sense. Just build what you wanted to build.
Adding to this, a lot of projects these days will rely upon some external libraries and APIs. So a new language in this case is just a sauce to consume the utilities.

As for the practical aspect of picking up a new language (for a purpose), I'd shamelessly look into an already existing projects along with following whatever language textbook.

Learning syntax is likely a simplest step, picking up an idiom is more involved and is often specific to objectives.

If I'm looking for problems I always like to try Advent of Code problems, it's a good chance to learn basic io, sample the standard library, and learn how to idiomatically handle common problems.

But before that I prefer to get a book and work my way through all the examples, otherwise I'm just making up what "idiomatic" means.

Redo a project you've done earlier with the new language (assuming it fits the general scope).
I always try to use a real-world use case e.g. web server with a large file.

It tests quality of libraries, resource safety, concurrency, performance etc.

I try to explore a language using different problems every time. If you've already done a project or solved a problem, I think your previous solution will detract from getting a feel for a language's features and conventions by inspiring you to follow a certain pattern.

There's a pretty causal relationship between starting my unfinished side projects and me trying a new language or library.

I use advent of code to really dig deep into a programming language I sort of know but want to learn to use more. I plan on learning modern C# this way this year, it's been almost a decade since I taught myself the language at this point.

If one knows C++ then Peter Shirley's https://raytracing.github.io/books/RayTracingInOneWeekend.ht... is a pretty nice raytracer one can type in any language.
Can anyone out there compare this to The Ray Tracer Challenge (https://pragprog.com/titles/jbtracer/the-ray-tracer-challeng...) which has also been mentioned in this thread. They seem to be trying to do very similar things, and it would be interesting to hear how the differ and their pros and cons
Ray Tracing in One Weekend:

- Is in C++

- Focuses on spheres

[1]Raytracer Challenge:

- Is Written in pseudocode

- You will be able to render objects composed of polygons not just spheres

[1] https://www.youtube.com/watch?v=MFjmpjPwFEM

I write a bittorrent node. It goes over many topics I'm interested in: potrntially low-level networking, storage, bits and bytes fiddling, concurrency. The protocol is shockingly simple, and the end result is both easily testable and actually useful.
This seems interesting. Any resources you can recommend for understanding the protocol? And any examples of simple clients in various languages?
I work through a good portion of the Crafting Interpreters tutorial (you create an interpreter for the toy Lox programming language). https://craftinginterpreters.com/contents.html

Gives me a chance to learn the new language with a project that's decent in size, without it being debilitating. Plus, it is fun writing an interpreter.

I commonly build a name generator based on travesties or Markov chains. I can write a simple version of it in a few minutes in a familiar language, but it exercises file IO, some simple data structures, string operations, and some randomization. Gives me a good first general idea of what it will be like to work with a new language.
That's very helpful, close to what I was envisioning. Thank you.
Sure.

It can also be fun, if your sense of humor responds to funny names, and if you choose the right inputs.

My go to is solving problems from Project Euler. Have learnt a few languages this way. https://projecteuler.net/
Same here! I remember implementing little algorithms to solve these puzzles in Python to get familiarized with the basics of the language.
I want an official certification of 140 IQ and your comp sci degree from everyone posting here. Otherwise do fizz buzz and go do some easy level problems on code wars. Much more makes little sense when you don't even know the array initialization syntax yet.
I use Advent of Code every year to improve my understanding/facility with some language that isn't a strength of mine. The benefit of doing that is that it's well-paced and interesting to me. The downside is that at least 35% of AoC is algorithmic and once you discover the "trick", redoing that AoC puzzle in a new language is less interesting (though it will still drive understanding of the new language).

If you wanted smaller exercises, you could take some of the leetcode exercises and grind through them on your own (outside of the leetcode framework if your language isn't represented there; if it is represented there, the Leetcode easy and medium are pretty good and there's loads of them).

I do a plain http server. String munging and sockets are pretty core to everything I do.
John Carmack re-implementing Wolfenstein 3D to learn Haskell comes to mind.
Doesn't seem like a quick exercise to me. LOL.
For Carmack it might be a quick exercise. Plus he'll probably fix any bugs in the Haskell implementation he's using
Thanks for all the comments. Just to clarify, not really asking how to learn a specific language or how to code. Question is more if one were doing a survey of various languages to understand disadvantages/advantages of each and for a general working knowledge, what's a good relatively quick baseline exercise to do that comparison ?
I typically write a raytracer while attempting to stick to a small set of "best-practices" for the language I'm trying to learn. I also use this exercise from time-to-time to test new language features (for example C++20 ranges/coroutines recently).

I also recommend this same exercise to others through the book "The Ray Tracer Challenge" [1] by Jamis Buck, because it describes an entire implementation of a raytracer in pseudocode. Beginners mostly just need to plug-in their language of choice & still end up with a satisfying visual result. Experienced programmers can extend it by adding the ability to load non-trivial .obj models, which will necessarily motivate adding concurrency, bounding volumes, & other general performance improvements.

I'm planning on tackling it again in Elixir soon, which should be fun & interesting.

EDIT: Almost forgot to mention the free bonus chapters [2] that get you started on bounding volumes (AABB), soft area lights, and texture mapping.

[1] https://pragprog.com/titles/jbtracer/the-ray-tracer-challeng...

[2] http://www.raytracerchallenge.com/#bonus

Wow. That looks really cool.
I'm currently going through this book and it's amazing. Any tips on where to go once I finish it?
Once you finish the main book, there's a few free bonus chapters provided [1] that help you implement soft area lights, bounding boxes (AABB in this case), and texture mapping. Beyond that, you might be interested in the "One Weekend" series [2], which is all C++ but is mostly general enough for other languages. There's also PBRT [3] which is an open source textbook/documentation for the pbrt-v3 renderer [4], but this is beyond the scope of "small exercise" territory. Good luck!

[1] http://www.raytracerchallenge.com/#bonus

[2] https://raytracing.github.io/

[3] https://pbr-book.org/3ed-2018/contents

[4] https://github.com/mmp/pbrt-v3

Great suggestion! And you earned them a sale from me. Going to try this out to bring my rustlang knowledge up to speed.
Thank you for this. A while ago I was doing a fair bit of Elixir in my spare time, so this has given me a reason to pick it up again. Going to give it a go myself
Meta-answer: I like to pick a project I wouldn't mind doing a 2–3 times in that language. It's hard to learn all the best practices, idioms, etc. of a language in one sweep. Having a project that you can repeatedly do in X language—making it more "X-like" each time—is a good way to learn how to refactor things and structure things "natively" in language X.
I port L0pht’s hphack.c to every language I’m learning.

It covers sockets, string handling, and byte handling.

Great idea. Looks like a really interesting exercise. I may try it. Also wasn't aware of L0pht till now so good to know that history. Thank you.
Depends on the language.

Python has been stuff like Pi hats with sensors, or using Pandas to analyze VAERS or Kaggle data. I assume this is similar for other scripting or backend languages/projects.

UI could be something like using Angular to build a simple site, or Android to build a simple app.

Basically find something that's simple and functionally similar to what you think you want to achieve with the language. At least for me, I want to learn a language to use it on a project, not just learn it for the sake of learning it.

I try to write a guest book.

This way I can see how to input data, display it and how I can operate with the database.

It's simple.

Sadly for Rust and MongoDB I tried that 5 times and never got to the point that it compiles. It just wasn't intuitive and the error messages didn't make sense. For Rust also I went through the tutorial, because I really wanted to like it and switch to it, but compared to Go it was just too complicated. I didn't understand the game, boxes ownership move unpack... it all didn't make sense . I digress but yeah, a guestbook is usually my 1st application.

An RPN calculator, especially when trying out new GUI tooling.

- Simple scope, easily expendable. - Stack as a simple data model. - State machine for number entry.