Nim installation redbook




















If we use start.. Using start.. Both indexing and slicing can be used to assign new values to the existing mutable containers and strings.

Tuples, on the other hand, contain heterogeneous data, i. Similarly to arrays, tuples have fixed-size. We can also name each field in a tuple to distinguish them.

This can be used for accessing the elements of the tuple, instead of indexing. Re-do the Collatz conjecture exercise , but this time instead of printing each step, add it to a sequence. If the length of current sequence is longer than the previous record, save the current length and the starting number as a new record you can use the tuple longestLength, startingNumber or two separate variables. Procedures, or functions as they are called in some other programming languages, are parts of code that perform a specific task, packaged as a unit.

By wrapping up the Collatz conjecture logic into a procedure we could have called the same code for all the exercises. So far we have used many built-in procedures, such as echo for printing, add for adding elements to a sequence, inc to increase the value of an integer, len to get the length of a container, etc.

As mentioned in the beginning of this section, procedures are often called functions in other languages. This is actually a bit of a misnomer if we consider the mathematical definition of a function. Mathematical functions take a set of arguments like f x, y , where f is a function, and x and y are its arguments and always return the same answer for the same input.

This is because our computer programs can store state in the variables we mentioned earlier which procedures can read and change. In Nim, the word func is currently reserved to be used as the more mathematically correct kind of function, forcing no side-effects. A procedure is declared by using the proc keyword and the procedure name, followed by the input parameters and their type inside of parentheses, and the last part is a colon and the type of the value returned from a procedure, like this:.

Doing something like this will throw an error:. In order for this to work we need to allow Nim, and the programmer using our procedure, to change the argument by declaring it as a variable:. This of course means that the name we pass it must be declared as a variable as well, passing in something assigned with const or let will throw an error. While it is good practice to pass things as arguments it is also possible to use names declared outside the procedure, both variables and constants:.

After we have declared a procedure, we can call it. If we want to call our findMax procedure from the above example, and save the return value in a variable we can do that with:. Nim, unlike many other languages, also supports Uniform Function Call Syntax , which allows many different ways of calling procedures. This one is a call where the first argument is written before the function name, and the rest of the parameters are stated in parentheses:. We can also omit the parentheses around the arguments:.

These two can also be combined like this, but this syntax however is not seen very often:. In Nim, every procedure that returns a value has an implicitly declared and initialized with a default value result variable. The procedure will return the value of this result variable when it reaches the end of its indented block, even with no return statement.

As mentioned in the very beginning of this section we can declare a procedure without a code block. The reason for this is that we have to declare procedures before we can call them, doing this will not work:.

Create a sequence of names. Greet each person using the created procedure. Points in 2D plane can be represented as tuple[x, y: float]. Create two procedures tick and tock which echo out the words "tick" and "tock". Have a global variable to keep track of how many times they have run, and run one from the other until the counter reaches The expected output is to get lines with "tick" and "tock" alternating 20 times.

Hint: use forward declarations. So far we have used the functionality which is available by default every time we start a new Nim file. This can be extended with modules, which give more functionality for some specific topic. This is commonly done on the top of the file so we can easily see what our code uses. Often times we have so much code in a project that it makes sense to split it into pieces that each does a certain thing.

In case you have more than these two files, you might want to organize them in a subdirectory or more than one subdirectory. With the following directory structure:. In this chapter we will learn how to make our programs more interactive. For that we need an option to read data from a file, or ask a user for an input. The contents of that file looks like this:. To solve the problem of a final new line, we can use the strip procedure from strutils after we have read from a file.

All this does is remove any so-called whitespace from the start and end of our string. Whitespace is simply any character that makes some space, new-lines, spaces, tabs, etc. If we want to interact with a user, we must be able to ask them for an input, and then process it and use it. We need to read from standard input stdin by passing stdin to the readLine procedure. Reading from a file or from a user input always gives a string as a result. If we would like to use numbers, we need to convert strings to numbers: we again use the strutils module and use parseInt to convert to integers or parseFloat to convert into a float.

If we have file numbers. Ask a user for their height and weight. Calculate their BMI. Report them the BMI value and the category. Repeat Collatz conjecture exercise so your program asks a user for a starting number. Print the resulting sequence. Ask a user for a string they want to have reversed. Create a procedure which takes a string and returns a reversed version.

For example, if user types Nim-lang , the procedure should return gnal-miN. Hint: use indexing and countdown. It is time to conclude this tutorial. Nim has a lot more to offer, and hopefully you will continue to explore its possibilities. Advent of Code : Series of interesting puzzles released every December. Archive of old puzzles from onwards is available. Nim basics Table of Contents. Who is this for? People with no or minimal previous programming experience People with some programming experience in other programming languages People who want to explore Nim for the first time, starting from scratch.

Who is this not for? People experienced in Nim feel free to help make this tutorial better. How to use this tutorial? Installing Nim Nim has ready made distributions for all three major operating systems and there are several options when it comes to installing Nim.

Installing additional tools You can write Nim code in any text editor, and then compile and run it from the terminal. Testing the installation To check if the installation was successful, we will write a program which is traditionally used as an introductory example: Hello World. The phrase you want to print must follow the echo command and must be enclosed in double-quotes ". First we need to compile our program, and then run it to see if it works as expected. To see all compiler options, type nim --help in your terminal.

Hello World! We can now start to explore the basic elements which will help us to write simple Nim programs. Variable declaration Nim is a statically typed programming language, meaning that the type of an assignment needs to be declared before using the value. If we already know its value, we can declare a variable and give it a value immediately:. Its type is automatically detected as an integer.

In Nim tabs are not allowed as indentation. You can set up your code editor to convert pressing Tab to any number of spaces. In VS Code, the default setting is to convert Tab to four spaces. Both of these are integers, the same as the original value. Comments in Nim code are written after a character. Everything after it on the same line will be ignored.

Immutable assignment Unlike variables declared with var keyword, two more types of assignment exist in Nim, whose value cannot change, one declared with the const keyword, and the other declared with the let keyword.

Const The value of an immutable assignment declared with const keyword must be known at compile time before the program is run. Integers As seen in the previous chapter, integers are numbers which are written without a fractional component and without a decimal point. Floats Floating-point numbers, or floats for short, are an approximate representation of real numbers. Converting floats and integers Mathematical operations between variables of different numerical types are not possible in Nim, and they will produce an error:.

When using the int function to convert a float to an integer no rounding will be performed. The number simply drops any decimals. To perform rounding we must call another function, but for that we must know a bit more about how to use Nim. Strings Strings can be described as a series of characters. It is inside double quotes, making it a string.

Special characters If we try to print the following string:. If we wanted to print the above example as it was written, we have two possibilities:. String concatenation Strings in Nim are mutable, meaning their content can change. Boolean A boolean or just bool data type can only have two values: true or false. Relational operators Relational operators test the relation between two entities, which must be comparable. First three characters are the same, and character b is smaller than character z.

Logical operators Logical operators are used to test the truthiness of an expression consisting of one or more boolean values. Logical and returns true only if both members are true Logical or returns true if there is at least one member which is true Logical xor returns true if one member is true, but the other is not Logical not negates the truthiness of its member: changing true to false , and vice versa it is the only logical operator that takes just one operand.

Relational and logical operators can be combined together to form more complex expressions. Recap This was the longest chapter in this tutorial and we covered a lot of ground. Exercises Create an immutable variable containing your age in years. Hint: use mod Create an immutable variable containing your height in centimeters.

Express the diameter in centimeters. If statement An if statement as shown above is the simplest way to branch our program. If statements can be nested, i. Else Else follows after an if-block and allows us to have a branch of code which will be executed when the condition in the if statement is not true.

If you only want to execute a block if the statement is false , you can simply negate the condition with the not operator. Elif Elif is short for "else if", and enables us to chain multiple if statements together.

In the case of g , even though g satisfies all three conditions, only the first branch is executed, automatically skipping all the other branches. Case A case statement is another way to only choose one of multiple possible paths, similar to the if statement with multiple elif s. Without it, the code would not compile. You've chosen y.

For loop Syntax of a for-loop is:. The end 16 is included in the range. While loop While loops are similar to if statements, but they keep executing their block of code as long as the condition remains true. Break and continue The break statement is used to prematurely exit from a loop, usually if some condition is met. At the chapter 1 at the point 1. That's like this I start with NIM. Here's another link lots of information.

Ok, I got it installed. I'm now configuring clients. Where is SSH? Ok, I can't do this. The installation steps listed I already knew but it's not working. If I reboot the client and do network boot, the master completely ignores it.

I seem to remember from the old days that I could enter the network details of the master and network on the client itself in the SMS menu? I've also created a mksysb resource on the master but I'm not sure how to install it on a client. If I boot the client I get the normal installation menu but there's no option to install from a backup. Which resources do I need to allocate to the client to install from a mksysb? What should the operation be?

You need to boot your client into sms utilities by hitting F1 when you get the audio beep. You need to then configure your network card settings through the RIPL menu and do a ping test! Thanks for that. Thanks to all of you, it's now fully functional. Take a look at the courses offered at Nairobi Technical Training Institute and find one that meets your needs.

Nairobi Technical Training Institute Admission Requirements varies depending on the course you want to apply. Elimu Centre is your one-stop-shop for all your education informational needs and much more. Follow us at Elimu Centre. Elimu Centre.



0コメント

  • 1000 / 1000