Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Ask HN: What is the coding exercise you use to explore a new language?
74 points by sam345 on Sept 14, 2022 | hide | past | favorite | 72 comments
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.


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


I use: https://raytracing.github.io/books/RayTracingInOneWeekend.ht...

I second the advice of loading .obj, it's a good exercise.

Implementing Octrees is another good one that will speed up your code while exploring tree structures in that language.


Great suggestion! And you earned them a sale from me. Going to try this out to bring my rustlang knowledge up to speed.


In that case, you might be interested in this youtube playlist [1]. The uploader claims to be new to Rust and implements the entire book in it. I haven't watched it myself, but had it bookmarked at the recommendation of a friend.

[1] https://youtube.com/playlist?list=PLy68GuC77sUTyOUvDhVboQoOl...


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


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


Wow. That looks really cool.


I write up a "teaching-style" series on it.

To wit: https://littlegreenviper.com/series/swiftwater/

That series is almost abandoned, now, but it was instrumental, in my discovering how to write Swift.

This was one I wrote up, as I was exploring the SPM: https://littlegreenviper.com/series/spm/

Learning Bluetooth: https://littlegreenviper.com/series/bluetooth/

Writing a simple streaming server: https://littlegreenviper.com/series/streaming/

This was one that I wrote up, as I was learning CSS: https://littlegreenviper.com/series/stylist/


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.


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.


Command line tool that ingests an arbitrary-length (but assumed finite) stream of lines, parses each line to a number, computes the Welford single-pass algorithm for mean and standard deviation, and prints the output nicely to the console.

It's a good introduction to handling input and output, string manipulation, error handling, abstractions for iteration (or lack thereof), and language performance characteristics. All while being a fairly easy task that can be implemented easily but performantly in 60 lines of Python: https://git.sr.ht/~wintershadows/polyglot-benchmarks/tree/ma...

It also lets you play around with different software architectures and abstraction models, introducing enough real-world complexity to prevent you from making too many assumptions, while the logic is simple enough to keep you from getting lost in deep algorithmic implementation.

I have started adding some of these implementations to a Git repo that I will eventually fill out with other "polyglot" comparisons. See here for (currently) Python, Nim, Perl, and (somewhat-overengineered) Common Lisp: https://git.sr.ht/~wintershadows/polyglot-benchmarks/tree/ma...

I have several more implementations that I haven't committed to Git yet, because I've been lazy.

I've learned a lot through this process, not just about individual programming languages, but also about "how things work" in the process of trying to figure out why Python is so much faster than every other language I've tried (on par with Nim, handily beating all others, haven't tried plain C yet).


FWIW, I did a straightforward impl in Nim that was ~10X faster than the Python version in your repo.


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.


My very first goal is always to write a “hello, world” program, deploy it to a web server, then uninstall the language, runtime, and other required assets... and do it again. I write up the process, then follow those instructions on a new system. Rinse and repeat until it’s possible for a chimp to replicate.

By far the most trouble I ever have is getting soup-to-nuts deployment happening such that I can concentrate on fixing bugs without being scared shitless I won’t even be able get the damn thing running in production.


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 usually pick one of the ideas on my low-stakes "side quest" list that I maintain. Something that I won't mind if it never gets finished, or if I have to trash it and start over because of what I learned while building it the first time, now that I know the language better. I'm currently working on a RISC-V cycle-level simulator in Rust.

While I'm picking up Rust, I really like the "rustlings" project that lets you exercise various parts of the language locally, instead of in a browser, so I can use the tools that I like. https://github.com/rust-lang/rustlings


+1 for rustlings, which also inspired "ziglings" [1]. Both of these were great resources for quickly & easily learning the basics of their respective languages.

[1] https://github.com/ratfactor/ziglings


If it's a game engine, I tend to do a simple port of my Proximity game[1] to it. I don't usually finish it enough to release it, though (I probably could and should in retrospect). I've done that for Flash, C#/XNA (eventually released on Xbox Live Indie Games as "Proximity 2"), J2ME, Java/Kindle, iOS/Objective-C (eventually released, but no longer in the store), Python (just a command line ASCII version), LUA/Pico-8 (nearly done, I just need to force myself to spend a couple weekends and finish it up), PlayDate (functional, mostly a port of my Pico-8 version they are both LUA-based, but it's not pretty and I don't have my PlayDate yet for testing to feel comfortable releasing the game yet), C++ (mostly made this for my portfolio for game dev jobs), and probably a couple others I'm forgetting.

Currently working on a new version in C#/Monogame, and next version after that will probably be Unity so I can make a VR version, something that's on my bucket list. Also at some point I'm going to make a new web version again using Javascript/Phaser (already made another simple game in Phaser, so it shouldn't be too hard, just need to actually do it).

It's been going slow though since I have so much less spare time and energy nowadays, though.

[1]: https://www.newgrounds.com/portal/view/183428


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.


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


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.


I have and continue to spend obscene amounts of time orchestrating the entire systems architecture from zero to full tilt in my language of interest. I start with base functionality applied examples needed throughout the systems life cycle; documentation, templates, clis, daemons, logging, cryption, networking, queuing, concurrency, databases and more as these are the teaching tools about the platform of "what, why, how". Such a simple statement does not accurately represent the details or the value however the experience of having founded and exited several technology companies using this model has proven the approach. With each passing new iteration the system's simplicity of understanding has increased while also adding value in many areas of support, new hires, and management. If it ain't broke don't fix it so after five previous complete refactors under multiple companies this sixth iteration is first for enjoyment in problem solving and then to apply this work to yet another startup. Nothing is more important than the foundation!


Other than advent of code, I'd usually start with the 99 prolog problems: https://www.ic.unicamp.br/~meidanis/courses/mc336/2009s2/pro...


I generate an n-dimensional just intonation pitch lattice, based on my study of the work of Ben Johnston.

I like it because it’s kind of small, but tests several aspects of working in the language.

* the program needs support for several numeric types: integers, floats and rationals. * several basic math algorithms are needed, which will either test the std lib, how to use the package manager, or other language constructs as I pipeline implement them * how easy is it to write/run tests

Here’s one version in Rust, which links to clojure, cl versions. Want to try it with Julia next due to the built-in support for rationals.

https://github.com/jcpst/johnston

https://en.m.wikipedia.org/wiki/Lattice_(music)


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).


Redo a project you've done earlier with the new language (assuming it fits the general scope).


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.


I do a plain http server. String munging and sockets are pretty core to everything I do.


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.


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.


Cryptocurrency analysis. Open APIs and a huge flow of data from the exchanges. And I mostly don't care about cryptocurrencies, nor am I holding any in my portfolio.


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.


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.


Something that speaks to the language's strengths and the ecosystems' interests. The problem has to feel more than a simple exercise.

Functional -- writing an interpreter for a toy language,

Scripting -- Sudoku/Wordle solver with UI

Systems language with low-level hardware access -- OpenCV integration, driving I/O pins, web server.

For all of them, I also test access to native C/C++ libraries.


I usually go through the Perl Cookbook.

http://pleac.sourceforge.net


If the language isn't well represented yet on rosettacode.org I like to learn about a language by implementing rosettacode examples in it: https://github.com/eterps/odin_rosettacode


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


May sound boring but usually it's a CRUD web API. But it really depends on the language. If the language is not particularly suited for this kind of project, first I find a good use case and then I build a simple version of it.


"How quickly can I build 50 different tiny demos that each show one thing"? Small size means you can focus on one thing at a time, before you hit #50 you'll probably already be building off of what you've learned in past demos and combining them as you try new things.


Depends on the concept I'm trying to learn.

My "go to" for anything UI related is Connect 4.

Other than that I try to pick a game that fits the theme. Games are amazing because they have well defined business requirements with sufficient depth to really exercise your critical thinking skills.


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 typically only use the spec (https://www.bittorrent.org/beps/bep_0003.html) because it's simple enough, but in case of doubt I guess a third party resource is good. I found https://blog.jse.li/posts/torrent/ to be quite informative about it.

I used to work on a fork of https://github.com/jackpal/Taipei-Torrent with custom features, I found its code to be easy enough to understand


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 tend to write a toy compiler or the ray tracer in one weekend kind of exercises.

Alternatively, if I bought a book about the language, I end up following the suggested exercises they usually provide at the end of each chapter.


Depends on what are you usually doing.

I write an L3 order book implementation, because I usually do quant trading.

Others might want to start with a simple REST webservice, a todo list, a CRUD application, solutions for Project Euler exercises, etc.


Create the ones youd love to use. Seriously. It puts you in the perspective of the user where you get to solve things you can use.


One approach would be picking a problem from a list of code katas, eg. http://codekata.com/


I always recommend building a calendar app, or something that deals heavily with dates. Knowing how to work with date and time in any language is critical.


I try to solve whatever immediate non critical issue I’ve got. ie some digital glue in my life.

To me the real worldness of that > clever technical scenarios


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.


I like Cryptopals for this purpose: https://cryptopals.com/


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.


IRC Bot.

<s>IRC? That’s sounds familiar.</s> :P


Find a "koans" repo you can work through, first of all.

Advent of code after that.

Then, implement a super basic Lisp.


In my experience, a good sample program to learn a language is one I want to write.


Implement some of the core utils on a standard Unix env.

cat, ping, netcat, grep, cut, etc.


IRC bot usually, otherwise something web-based like a wiki.


Build an emulator like GameBoy emulator.


I build a web scraper.


Database CRUD


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.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: