From 7eb172ce6316940e708527bc16e425048f80c0d4 Mon Sep 17 00:00:00 2001 From: JalonSolov Date: Mon, 5 Feb 2024 23:31:31 -0500 Subject: [PATCH] expand first 2 lessons and add 3rd --- lessons/1.what-is-v.md | 9 +++- lessons/2.writing-our-first-program.md | 39 ++++++++++++++- lessons/3.next-level-program.md | 67 ++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 lessons/3.next-level-program.md diff --git a/lessons/1.what-is-v.md b/lessons/1.what-is-v.md index 4df4af8..2f014e5 100644 --- a/lessons/1.what-is-v.md +++ b/lessons/1.what-is-v.md @@ -5,4 +5,11 @@ V is a [general purpose programming language](https://en.wikipedia.org/wiki/Gene This means it can be used to create any type of program you desire, rather than being focused on any one particular type of program. -You can install it by [following the instructions here](https://github.com/vlang/v/blob/master/README.md#installing-v-from-source). +V is also a statically typed, compiled langauge. Statically typed means that +all variable types are known at compile time, and cannot change type at runtime. + +In other words, if you create a string variable, it will always be a string +variable, and cannot be changed to an integer without changing the source and +recompiling your program. + +You can install V by [following the instructions here](https://github.com/vlang/v/blob/master/README.md#installing-v-from-source). diff --git a/lessons/2.writing-our-first-program.md b/lessons/2.writing-our-first-program.md index 4a4c26c..03da320 100644 --- a/lessons/2.writing-our-first-program.md +++ b/lessons/2.writing-our-first-program.md @@ -3,8 +3,45 @@ Writing Our First Program In this lesson, we'll examine one of the simplest programs in V. +Yes it is the iconic [Hello World](https://en.wikipedia.org/wiki/%22Hello%2C_World!%22_program) +that is often the first program someone would write in language they are learning. + ```v fn main() { - println('hello world') + println('Hello, World!') } ``` + +In V, functions are declared with the `fn` keyword, followed by the name of the +function (in this case `main`), the parameters to the function, if any (in this +case there aren't any), then an opening curly brace, `{`, on the same line. + +V also has 2 ways to output text - one that only prints what is given to it, +`print`, and one that automatically adds a linefeed to the end of whatever it +is given, `println`. + +Using +```v + println('Hello, World!)` +``` +above is the equivalent to +```c + printf("Hello, World!\n"); +``` +in C. + +Finally, the closing curly brace, `}`, to end the function. + +One thing to clarify - V doesn't care if you use single quotes (`'`) or double +quotes (`"`) around a string of characters, the opening and closing quotes just +have to match. This means you can't use `'a string"` or `"a string'` but either +`'a string'` or `"a string"` are fine. + +This is unusual amongst programming programming languages, where you must only +use one or the other (in C, for example, only double quotes are valid around +strings), or if both types are supported, they have different meanings. In V, +they are both equally valid, and mean exactly the same thing. + +Also note that both `print` and `println` are builtin functions in V - nothing extra +is needed to use them, other than typing their names and giving them something +to print. diff --git a/lessons/3.next-level-program.md b/lessons/3.next-level-program.md new file mode 100644 index 0000000..4168851 --- /dev/null +++ b/lessons/3.next-level-program.md @@ -0,0 +1,67 @@ +Next Level Program +================== + +Now, for a slightly more complex version of the `Hello World` program. This is +a copy of one of the examples on the [V Home Page](https://vlang.io). +```v +fn main() { + areas := ['game', 'web', 'tools', 'science', 'systems', + 'embedded', 'drivers', 'GUI', 'mobile'] + for area in areas { + println('Hello, ${area} developers!') + } +} +``` + +In this version of the program, several more ideas are introduced. + +V variables are declared by giving the name, followed by `:=` then a value to +set both the initial value of the variable, as well as it's type. + +In the example, +```v + areas := ['game', 'web', 'tools', 'science', 'systems', + 'embedded', 'drivers', 'GUI', 'mobile'] +``` +`areas` is declared as the name of a variable, and is assigned the type `[]string` +(meaning an array of strings), with the initial values of the array set to the +strings inside the `[]` characters. + +Unlike other languages, V does not allow you to create a variable without +assigning a value, as the value sets the variable type. + +The 2nd new thing is the [`for`](https://github.com/vlang/v/blob/master/doc/docs.md#for-loop) +loop, which is the only looping statement in V. + +For this variation, it is used to loop over the values in the `areas` array, so +that each one can be used in the `println` statement. +```v-ignore + for area in areas { +``` +This means "for every string in the `areas` string array, assign the value to a +variable named `area`, then execute the statements inside the `{}`. + +The `println` statement is also different in this example: +```v + println('Hello, ${area} developers!') +``` +Here, V uses a new concept called "string interpolation". This basically means +that V will take whatever is inside `${}` in the string, treat it as V code, +and use the value as if it was part of the string when it is printed. + +For this specific case, it means "use the value of the variable named `area`", +and the result is a string that changes every time through the loop, as the +value of `area` changes. + +The output from running this program is +``` +Hello, game developers! +Hello, web developers! +Hello, tools developers! +Hello, science developers! +Hello, systems developers! +Hello, embedded developers! +Hello, drivers developers! +Hello, GUI developers! +Hello, mobile developers! +```