So, stumbling around discussions related to the topic in my last post, I discovered a new language that I find pretty neat!
BUT it is most certainly not Python!
Here’s some stuff about Nim, if you’re curious:
- Nim’s Homepage
- A Dr. Dobb’s article (2014)
- InfoQ presentation from the original Author of the language
- Learn X In Y Minutes: Where X is Nim
Here’s a very simple sample program – a guess-the-random-number game:
discard """ This program is about playing a very simple game: guess a randomly generated number! """ from math import random, randomize from strutils import parseInt # make sure that the seed is randomized randomize() # keep playing the game until the user wants to quit var rand_val: int var raw_user_val: string var user_val: int while true: # pick the number rand_val = random(10) + 1 # +1 because it's 0 to max-1 # ask the user for the number echo("Please guess a number between 1 and 10 [q to quit]") raw_user_val = readLine(stdin) # check to see if the user would like to quit if raw_user_val == "q": break; # make sure the number is an integer try: user_val = parseInt(raw_user_val) except EInvalidValue: continue # make sure the value is between 1 and 10, inclusive if user_val < 1 or user_val > 10: continue; # check to see if it is the same if user_val == rand_val: echo("Yay! You Guessed It!") else: echo("You FAIL!")
The very first line in this example shows off something I find very interesting.
discard is required so that the compiler ignores things like unused return values. BUT it can also be used like a multi-line comment by simply discarding an unassigned multi-line string value.
Why is this the way multi-line comments are done? Well, because even regular single-line comments in Nim are part of the syntax tree, and aren’t just discarded during parsing!
Progressing through this (very simplistic and not representative) program, you’d almost think you were looking at Python, with a few weird function calls and some type annotations on variables. This makes it very easy for me to start reading and understanding… not to mention guess at some of the syntax structure I don’t already know.