From d8c78cc4299cfa55f26ac5fa7f7297e21593f5c2 Mon Sep 17 00:00:00 2001 From: colinleach Date: Sun, 8 Sep 2024 14:27:54 -0700 Subject: [PATCH 1/9] New concept basics --- concepts/basics/.meta/config.json | 10 ++ concepts/basics/about.md | 214 ++++++++++++++++++++++++++++++ concepts/basics/introduction.md | 120 +++++++++++++++++ concepts/basics/links.json | 18 +++ config.json | 5 + 5 files changed, 367 insertions(+) create mode 100644 concepts/basics/.meta/config.json create mode 100644 concepts/basics/about.md create mode 100644 concepts/basics/introduction.md create mode 100644 concepts/basics/links.json diff --git a/concepts/basics/.meta/config.json b/concepts/basics/.meta/config.json new file mode 100644 index 00000000..0e016c90 --- /dev/null +++ b/concepts/basics/.meta/config.json @@ -0,0 +1,10 @@ +{ + "authors": [ + "colinleach" + ], + "contributors": [ + "SaschaMann", + "cmcaine" + ], + "blurb": "An introduction to Julia variables, constants and functions." +} diff --git a/concepts/basics/about.md b/concepts/basics/about.md new file mode 100644 index 00000000..3ba8438a --- /dev/null +++ b/concepts/basics/about.md @@ -0,0 +1,214 @@ +# About + +Julia is a dynamic, strongly-typed programming langauge. +The programming styple is mainly functional, though with more flexibility than in languages such as Haskell. + +There is a strong and versatile type system, which will become important in later concepts. In practice, Julia will usually infer a suitable default from the context. + +Despite its emphasis on readability, making it appear as a scripting language like Python and Ruby, Julia code is compiled before running. +The Just In Time (JIT) compiler produces highly optimized code for each function as it is enountered, leading to high runtime speed. + +Julia is not an object-oriented language, and there is no class hierarchy of the sort central to many other languages. + +Instead *(and please don't panic as you read this!)*, Julia relies on: +- A *Type* hierarchy. +- *Composable* functions (designed to combine easily, in versatile and powerful ways). +- *Multiple dispatch*, meaning functions can be called with variable number and types of arguments. + +These rather confusing terms should become clearer as we progress through the syllabus. + +The [offical documentation][official-documentation] contains a lot of valuable information for the current release, though it can be a bit overwhelming for beginners. + +- [Manual][manual] +- [Tutorials][tutorials] +- [Get Started with Julia][get-started] +- [Notes for Python programmers][diff-python] +- [Notes for R programmers][diff-r] +- [Notes for C/C++ programmers][diff-c] +- [Notes for Matlab programmers][diff-matlab] (this is a proprietary language, very popular with engineers and one inspiration for Julia) + +Videos include: +- [A Brief Introduction to Julia][erik] from the Exercism management! +- [A Gentle Introduction to Julia][gentle-intro] +- [Learn Julia With Us][learn-with-us] : a 4-part series + +## Comments + +Two options are possible in Julia: +- Single-line comments start with `#` +- Multi-line comments start with `#=` and end with `=#`. Nesting is allowed. + +```julia +# This is a single-line comment + +x = 3 # This is an inline comment + +#= + Multi-line comments can be used for longer explanations. + + They are especially useful to comment out blocks of code during debugging. +=# +``` + +## Variables and assignment + +To create a [variable][variables], just assign a value to it: + +```julia-repl +julia> myvar = 42 # an integer (in fact, Int64) +42 + +julia> bigint = 1_234_567_890 # optionally use underscore as digit separator, for readability +1234567890 + +julia> name = "Maria" # strings are surrounded by double-quotes "" +"Maria" +``` + +Assignment ["binds"][binding] a value to the variable. + +Specifying a type is optional, and mostly Julia will infer a suitable default from the context. + +Types are an important subject in Julia. +We will explore them further in later concepts. +For now, it is best not to worry about them. + +In contrast to many functional languages, variables in Julia can be reassigned, and you are free to change both the *value* and the *type* bound to the variable: + +```julia-repl +julia> myvar = 3 +3 + +julia> myvar = "now a string" +"now a string" +``` + +## Constants + +Global variables, created outside any function, are: +- Allowed. +- Sometimes necessary. +- Usually discouraged (only within `*.jl` files; the REPL operates differently). + +If a value needs to be available throughout the program, but is not expected to change, use a [constant][constants] instead. + +Prefacing the assignemt with the `const` keyword allows the compiler to generate more efficient code. + +Accidentally trying to change the `const` value will give a warning: + +```julia-repl +julia> const answer = 42 +42 + +julia> answer = 24 +WARNING: redefinition of constant Main.answer. This may fail, cause incorrect answers, or produce other errors. +24 +``` + +## Arithmetic operators + +[Arithmetic][operators] mostly works conventionally: + +```julia +2 + 3 # 5 (addition) +2 - 3 # -1 (subtraction) +2 * 3 # 6 (mutlplication) +8 / 2 # 4.0 (division) +8 % 3 # 2 (remainder) +2 ^ 3 # 8 (exponentiation) +``` + +Note that division with `/` always gives a floating-point value. + +We will return to this in a later concept, which will discuss integer division with `div()` or `÷` to truncate and `//` to get a rational number. + +## Functions + +For best runtime performance, it is best to place most of the code inside [functions][functions]. Having lots of small functions is fine, in contrast to some other languages. + +There are two common ways to define a named function in Julia: + +1. Using the `function` keyword + + ```julia + function muladd(x, y, z) + x * y + z + end + ``` + + Indentation by 4 spaces is conventional for readability, but the compiler ignores this. + The `end` keyword is essential: more like Ruby than like Python. + + Note that we could have written `return x * y + z`. + However, Julia functions always return the last expression evaluated, so the `return` keyword is optional. + Many programmers prefer to include it to make their intentions more explicit. + +2. Using the "assignment form" + + ```julia + muladd(x, y, z) = x * y + z + ``` + + This is most commonly used for one-line function definitions or mathematical functions, where the function body is a single expression. + A `return` keyword is *never* used in the assignment form. + +The two forms are equivalent, and are used in exactly the same way, so choose whichever is more readable. + +Invoking a function is done by specifying its name and passing arguments for each of the function's parameters: + +```julia +# invoking a function +muladd(10, 5, 1) + +# and of course you can invoke a function within the body of another function: +square_plus_one(x) = muladd(x, x, 1) +``` + +From the fact that we refer to these as "named" functions, you might guess that there are also "anonymous" functions. +These will be covered in a later concept. + +## Naming conventions + +Like many languages, Julia [requires][naming] that names (of variables, functions, and many other things) start with a letter, followed by any combination of letters, digits and underscores. + +By convention, variable, constant and function names are *lowercase*, with underscores avoided except when needed to avoid confusion. + +However, Julia uses [Unicode][unicode] throughout, so "letter" is interpreted quite flexibly. + +Languages other than English are supported, so are emojis *(though please use good taste and pick something appropriate)*. + +In particular, all the Greek letters loved by mathematicians are available *(and several useful values are pre-defined)*: + +```julia-repl +julia> π # a built-in constant +π = 3.1415926535897... +``` + +Julia-aware editors, including the REPL, Pluto.jl, and VS Code with the Julia plugin, make these characters easy to enter. + +For π, type `\pi` then hit ``. Your typing will be replaced by the Greek character. + +You will see many more examples of this in later concepts. + +In fact, the backslash-abbreviations are not something specific to Julia. +They are taken from [LaTeX][latex], a typesetting system that many scientists and engineers use almost daily to write reports. + +[official-documentation]: https://docs.julialang.org/en/v1/ +[manual]: https://docs.julialang.org/en/v1/manual/getting-started/ +[tutorials]: https://julialang.org/learning/tutorials/ +[diff-python]: https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-Python +[diff-r]: https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-R +[diff-c]: https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-C/C +[diff-matlab]: https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-MATLAB +[get-started]: https://julialang.org/learning/ +[erik]: https://www.youtube.com/watch?v=X4Alzh3QyWU +[gentle-intro]: https://www.youtube.com/watch?v=4igzy3bGVkQ +[learn-with-us]: https://www.youtube.com/watch?v=oTUmW8dWZws +[variables]: https://docs.julialang.org/en/v1/manual/variables/ +[constants]: https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Constants +[operators]: https://docs.julialang.org/en/v1/manual/mathematical-operations/ +[functions]: https://docs.julialang.org/en/v1/manual/functions/ +[naming]: https://docs.julialang.org/en/v1/manual/variables/#man-allowed-variable-names +[unicode]: https://en.wikipedia.org/wiki/Unicode +[latex]: https://en.wikipedia.org/wiki/LaTeX +[binding]: https://docs.julialang.org/en/v1/manual/variables/#man-assignment-expressions diff --git a/concepts/basics/introduction.md b/concepts/basics/introduction.md new file mode 100644 index 00000000..7b2a27af --- /dev/null +++ b/concepts/basics/introduction.md @@ -0,0 +1,120 @@ +# Introduction + +Julia is a dynamic, strongly-typed programming langauge. +The programming style is mainly functional, though with more flexibility than in languages such as Haskell. + +## Comments + +Two options are possible in Julia: +- Single-line comments start with `#` +- Multi-line comments start with `#=` and end with `=#`. Nesting is allowed. + +```julia +# This is a single-line comment + +x = 3 # This is an inline comment + +#= + Multi-line comments can be used for longer explanations. + + They are especially useful to comment out blocks of code during debugging. +=# +``` + +## Variables and assignment + +To create a variable, just assign a value to it: + +```julia-repl +julia> myvar = 42 # an integer +42 + +julia> name = "Maria" # strings are surrounded by double-quotes "" +"Maria" +``` + +Types are an important subject in Julia, but for now it is best to ignore them them. +The compiler will infer a suitable type for any expression you use. + +## Constants + +Global variables, created outside any function, are: +- Allowed. +- Sometimes necessary. +- Usually discouraged *(only within `*.jl` files; the REPL operates differently)*. + +If a value needs to be available throughout the program, but is not expected to change, use a constant instead. + +Prefacing the assignemt with the `const` keyword allows the compiler to generate more efficient code. + +Accidentally trying to change the `const` value will give a warning: + +```julia-repl +julia> const answer = 42 +42 + +julia> answer = 24 +WARNING: redefinition of constant Main.answer. This may fail, cause incorrect answers, or produce other errors. +24 +``` + +## Arithmetic operators + +These mostly work conventionally: + +```julia +2 + 3 # 5 (addition) +2 - 3 # -1 (subtraction) +2 * 3 # 6 (mutlplication) +8 / 2 # 4.0 (division with floating-point result) +8 % 3 # 2 (remainder) +``` + +## Functions + +There are two common ways to define a named function in Julia: + +1. Using the `function` keyword + + ```julia + function muladd(x, y, z) + x * y + z + end + ``` + + Indentation by 4 spaces is conventional for readability, but the compiler ignores this. + The `end` keyword is essential: more like Ruby than like Python. + + Note that we could have written `return x * y + z`. + However, Julia functions always return the last expression evaluated, so the `return` keyword is optional. + Many programmers prefer to include it to make their intentions more explicit. + +2. Using the "assignment form" + + ```julia + muladd(x, y, z) = x * y + z + ``` + + This is most commonly used for one-line function definitions or mathematical functions, where the function body is a single expression. + A `return` keyword is *never* used in the assignment form. + +The two forms are equivalent, and are used in exactly the same way, so choose whichever is more readable. + +Invoking a function is done by specifying its name and passing arguments for each of the function's parameters: + +```julia +# invoking a function +muladd(10, 5, 1) + +# and of course you can invoke a function within the body of another function: +square_plus_one(x) = muladd(x, x, 1) +``` + +## Naming conventions + +Like many languages, Julia requires that names (of variables, functions, and many other things) start with a letter, followed by any combination of letters, digits and underscores. + +By convention, variable, constant, and function names are *lowercase*, with underscores kept to a reasonable minimum. + +However, Julia uses Unicode throughout, so "letter" is interpreted quite flexibly: not just *English* letters. + diff --git a/concepts/basics/links.json b/concepts/basics/links.json new file mode 100644 index 00000000..d20c2d90 --- /dev/null +++ b/concepts/basics/links.json @@ -0,0 +1,18 @@ +[ + { + "url": "https://docs.julialang.org/en/v1/", + "description": "Official documentation" + }, + { + "url": "https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-Python", + "description": "Noteworthy Difference from Python" + }, + { + "url": "https://www.youtube.com/watch?v=X4Alzh3QyWU", + "description": "Exercism: A Brief Introduction to Julia" + }, + { + "url": "https://julialang.org/learning/", + "description": "Get Started with Julia" + } +] diff --git a/config.json b/config.json index 84ede5cd..cd49933c 100644 --- a/config.json +++ b/config.json @@ -1089,6 +1089,11 @@ ] }, "concepts": [ + { + "uuid": "37ae4288-99d6-45bb-b550-766b03abb39f", + "slug": "basics", + "name": "Basics" + }, { "uuid": "32384ed4-ef26-4118-8ea3-4d44c331e828", "slug": "functions", From 3f1a9d61a0db3ca031d362179fdd9bbdf2901b33 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 9 Sep 2024 13:20:37 -0700 Subject: [PATCH 2/9] Update concepts/basics/about.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: András B Nagy <20251272+BNAndras@users.noreply.github.com> --- concepts/basics/about.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/basics/about.md b/concepts/basics/about.md index 3ba8438a..3e51a716 100644 --- a/concepts/basics/about.md +++ b/concepts/basics/about.md @@ -17,7 +17,7 @@ Instead *(and please don't panic as you read this!)*, Julia relies on: These rather confusing terms should become clearer as we progress through the syllabus. -The [offical documentation][official-documentation] contains a lot of valuable information for the current release, though it can be a bit overwhelming for beginners. +The [official documentation][official-documentation] contains a lot of valuable information for the current release, though it can be a bit overwhelming for beginners. - [Manual][manual] - [Tutorials][tutorials] From 6f0ecbc432ef43a5d67b868f782ac91733341871 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 9 Sep 2024 13:20:46 -0700 Subject: [PATCH 3/9] Update concepts/basics/about.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: András B Nagy <20251272+BNAndras@users.noreply.github.com> --- concepts/basics/about.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/basics/about.md b/concepts/basics/about.md index 3e51a716..fe631a02 100644 --- a/concepts/basics/about.md +++ b/concepts/basics/about.md @@ -1,6 +1,6 @@ # About -Julia is a dynamic, strongly-typed programming langauge. +Julia is a dynamic, strongly-typed programming language. The programming styple is mainly functional, though with more flexibility than in languages such as Haskell. There is a strong and versatile type system, which will become important in later concepts. In practice, Julia will usually infer a suitable default from the context. From c656214dd1211574ea90785811a47ff82a587b31 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 9 Sep 2024 13:20:57 -0700 Subject: [PATCH 4/9] Update concepts/basics/links.json MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: András B Nagy <20251272+BNAndras@users.noreply.github.com> --- concepts/basics/links.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/basics/links.json b/concepts/basics/links.json index d20c2d90..bf638b8a 100644 --- a/concepts/basics/links.json +++ b/concepts/basics/links.json @@ -5,7 +5,7 @@ }, { "url": "https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-Python", - "description": "Noteworthy Difference from Python" + "description": "Noteworthy Differences from Python" }, { "url": "https://www.youtube.com/watch?v=X4Alzh3QyWU", From 1a3dcdfe5206cec303daf7fc7e294a19059cabb1 Mon Sep 17 00:00:00 2001 From: depial <91621102+depial@users.noreply.github.com> Date: Mon, 9 Sep 2024 20:11:31 -0400 Subject: [PATCH 5/9] Fix small typo in about.md --- concepts/basics/about.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/basics/about.md b/concepts/basics/about.md index fe631a02..32bff0ea 100644 --- a/concepts/basics/about.md +++ b/concepts/basics/about.md @@ -1,7 +1,7 @@ # About Julia is a dynamic, strongly-typed programming language. -The programming styple is mainly functional, though with more flexibility than in languages such as Haskell. +The programming style is mainly functional, though with more flexibility than in languages such as Haskell. There is a strong and versatile type system, which will become important in later concepts. In practice, Julia will usually infer a suitable default from the context. From dc92e558add35f20cd3d8cc7e2ff77692df860f8 Mon Sep 17 00:00:00 2001 From: Colin Leach Date: Wed, 26 Feb 2025 07:28:31 -0700 Subject: [PATCH 6/9] formatting cleanup --- concepts/basics/about.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/concepts/basics/about.md b/concepts/basics/about.md index 32bff0ea..b90d0364 100644 --- a/concepts/basics/about.md +++ b/concepts/basics/about.md @@ -3,7 +3,8 @@ Julia is a dynamic, strongly-typed programming language. The programming style is mainly functional, though with more flexibility than in languages such as Haskell. -There is a strong and versatile type system, which will become important in later concepts. In practice, Julia will usually infer a suitable default from the context. +There is a strong and versatile type system, which will become important in later concepts. +In practice, Julia will usually infer a suitable default from the context. Despite its emphasis on readability, making it appear as a scripting language like Python and Ruby, Julia code is compiled before running. The Just In Time (JIT) compiler produces highly optimized code for each function as it is enountered, leading to high runtime speed. @@ -124,7 +125,8 @@ We will return to this in a later concept, which will discuss integer division w ## Functions -For best runtime performance, it is best to place most of the code inside [functions][functions]. Having lots of small functions is fine, in contrast to some other languages. +For best runtime performance, it is best to place most of the code inside [functions][functions]. +Having lots of small functions is fine, in contrast to some other languages. There are two common ways to define a named function in Julia: From e8b881adb482a99c43dad455c8c476bfc62d556d Mon Sep 17 00:00:00 2001 From: Colin Leach Date: Wed, 26 Feb 2025 07:57:37 -0700 Subject: [PATCH 7/9] recycled old UUID from unwanted concept --- .../integer-introduction/.meta/config.json | 0 {concepts => concepts.wip}/integer-introduction/about.md | 0 .../integer-introduction/introduction.md | 0 {concepts => concepts.wip}/integer-introduction/links.json | 0 config.json | 7 +------ 5 files changed, 1 insertion(+), 6 deletions(-) rename {concepts => concepts.wip}/integer-introduction/.meta/config.json (100%) rename {concepts => concepts.wip}/integer-introduction/about.md (100%) rename {concepts => concepts.wip}/integer-introduction/introduction.md (100%) rename {concepts => concepts.wip}/integer-introduction/links.json (100%) diff --git a/concepts/integer-introduction/.meta/config.json b/concepts.wip/integer-introduction/.meta/config.json similarity index 100% rename from concepts/integer-introduction/.meta/config.json rename to concepts.wip/integer-introduction/.meta/config.json diff --git a/concepts/integer-introduction/about.md b/concepts.wip/integer-introduction/about.md similarity index 100% rename from concepts/integer-introduction/about.md rename to concepts.wip/integer-introduction/about.md diff --git a/concepts/integer-introduction/introduction.md b/concepts.wip/integer-introduction/introduction.md similarity index 100% rename from concepts/integer-introduction/introduction.md rename to concepts.wip/integer-introduction/introduction.md diff --git a/concepts/integer-introduction/links.json b/concepts.wip/integer-introduction/links.json similarity index 100% rename from concepts/integer-introduction/links.json rename to concepts.wip/integer-introduction/links.json diff --git a/config.json b/config.json index cd49933c..82941ed7 100644 --- a/config.json +++ b/config.json @@ -1090,7 +1090,7 @@ }, "concepts": [ { - "uuid": "37ae4288-99d6-45bb-b550-766b03abb39f", + "uuid": "006ebce8-87cd-4695-87e6-8a7b8dc2f239", "slug": "basics", "name": "Basics" }, @@ -1099,11 +1099,6 @@ "slug": "functions", "name": "Functions" }, - { - "uuid": "006ebce8-87cd-4695-87e6-8a7b8dc2f239", - "slug": "integer-introduction", - "name": "Integers and Arithmetic Operations" - }, { "uuid": "90365822-f58c-4e45-88be-430978b77e65", "slug": "numbers", From d6cd3d72c49eaf438ce6ed23821fc75785f0b408 Mon Sep 17 00:00:00 2001 From: Colin Leach Date: Wed, 26 Feb 2025 08:02:39 -0700 Subject: [PATCH 8/9] a few typos and inelegancies fixed --- concepts/basics/about.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/concepts/basics/about.md b/concepts/basics/about.md index b90d0364..4b1b2316 100644 --- a/concepts/basics/about.md +++ b/concepts/basics/about.md @@ -89,7 +89,7 @@ julia> myvar = "now a string" Global variables, created outside any function, are: - Allowed. - Sometimes necessary. -- Usually discouraged (only within `*.jl` files; the REPL operates differently). +- Usually discouraged (though only within `*.jl` files; the REPL operates differently). If a value needs to be available throughout the program, but is not expected to change, use a [constant][constants] instead. @@ -113,7 +113,7 @@ WARNING: redefinition of constant Main.answer. This may fail, cause incorrect an ```julia 2 + 3 # 5 (addition) 2 - 3 # -1 (subtraction) -2 * 3 # 6 (mutlplication) +2 * 3 # 6 (multiplication) 8 / 2 # 4.0 (division) 8 % 3 # 2 (remainder) 2 ^ 3 # 8 (exponentiation) @@ -121,7 +121,7 @@ WARNING: redefinition of constant Main.answer. This may fail, cause incorrect an Note that division with `/` always gives a floating-point value. -We will return to this in a later concept, which will discuss integer division with `div()` or `÷` to truncate and `//` to get a rational number. +We will return to this in later concepts, which will discuss integer division with `div()` or `÷` to truncate and `//` to get a rational number. ## Functions From 12cc25595607c853c1188951491ebceb34c100a8 Mon Sep 17 00:00:00 2001 From: Colin Leach Date: Wed, 26 Feb 2025 14:23:30 -0700 Subject: [PATCH 9/9] synced introduction.md with Lasagna exercise --- concepts/basics/about.md | 3 ++ concepts/basics/introduction.md | 56 +++++++++++---------------------- 2 files changed, 21 insertions(+), 38 deletions(-) diff --git a/concepts/basics/about.md b/concepts/basics/about.md index 4b1b2316..91325c53 100644 --- a/concepts/basics/about.md +++ b/concepts/basics/about.md @@ -35,6 +35,9 @@ Videos include: ## Comments +Including comments in your code is extremely important to help humans read your code and understand your intentions: maybe other people, but also the future you. +They are (mostly) ignored by the compiler. + Two options are possible in Julia: - Single-line comments start with `#` - Multi-line comments start with `#=` and end with `=#`. Nesting is allowed. diff --git a/concepts/basics/introduction.md b/concepts/basics/introduction.md index 7b2a27af..2dc35ddf 100644 --- a/concepts/basics/introduction.md +++ b/concepts/basics/introduction.md @@ -1,29 +1,15 @@ # Introduction +The entire Julia track will require you to treat your solution like small libraries, i.e. you need to define functions, types etc. which will then be run against a test suite. +For that reason, we will introduce named functions as the very first concept. + Julia is a dynamic, strongly-typed programming langauge. The programming style is mainly functional, though with more flexibility than in languages such as Haskell. -## Comments - -Two options are possible in Julia: -- Single-line comments start with `#` -- Multi-line comments start with `#=` and end with `=#`. Nesting is allowed. - -```julia -# This is a single-line comment - -x = 3 # This is an inline comment - -#= - Multi-line comments can be used for longer explanations. - - They are especially useful to comment out blocks of code during debugging. -=# -``` - ## Variables and assignment -To create a variable, just assign a value to it: +There is no need to declare a variable in advance. +Just assign a value to a suitable name: ```julia-repl julia> myvar = 42 # an integer @@ -33,20 +19,13 @@ julia> name = "Maria" # strings are surrounded by double-quotes "" "Maria" ``` -Types are an important subject in Julia, but for now it is best to ignore them them. -The compiler will infer a suitable type for any expression you use. - ## Constants -Global variables, created outside any function, are: -- Allowed. -- Sometimes necessary. -- Usually discouraged *(only within `*.jl` files; the REPL operates differently)*. +If a value needs to be available throughout the program, but is not expected to change, it is best to mark it as a constant. -If a value needs to be available throughout the program, but is not expected to change, use a constant instead. - -Prefacing the assignemt with the `const` keyword allows the compiler to generate more efficient code. +Prefacing an assignment with the `const` keyword allows the compiler to generate more efficient code than is possible for a variable. +Constants also help to protect you against errors in coding. Accidentally trying to change the `const` value will give a warning: ```julia-repl @@ -58,14 +37,17 @@ WARNING: redefinition of constant Main.answer. This may fail, cause incorrect an 24 ``` +Note that a `const` can only be declared *outside* any function. +This will typically be near the top of the `*.jl` file, before the function definitions. + ## Arithmetic operators -These mostly work conventionally: +These are the same as in many other languages: ```julia 2 + 3 # 5 (addition) 2 - 3 # -1 (subtraction) -2 * 3 # 6 (mutlplication) +2 * 3 # 6 (multiplication) 8 / 2 # 4.0 (division with floating-point result) 8 % 3 # 2 (remainder) ``` @@ -83,9 +65,9 @@ There are two common ways to define a named function in Julia: ``` Indentation by 4 spaces is conventional for readability, but the compiler ignores this. - The `end` keyword is essential: more like Ruby than like Python. + The `end` keyword is essential. - Note that we could have written `return x * y + z`. + Note that we could have written `return x * y + z`. However, Julia functions always return the last expression evaluated, so the `return` keyword is optional. Many programmers prefer to include it to make their intentions more explicit. @@ -95,7 +77,8 @@ There are two common ways to define a named function in Julia: muladd(x, y, z) = x * y + z ``` - This is most commonly used for one-line function definitions or mathematical functions, where the function body is a single expression. + This is most commonly used for making concise single-expression functions. + A `return` keyword is *never* used in the assignment form. The two forms are equivalent, and are used in exactly the same way, so choose whichever is more readable. @@ -114,7 +97,4 @@ square_plus_one(x) = muladd(x, x, 1) Like many languages, Julia requires that names (of variables, functions, and many other things) start with a letter, followed by any combination of letters, digits and underscores. -By convention, variable, constant, and function names are *lowercase*, with underscores kept to a reasonable minimum. - -However, Julia uses Unicode throughout, so "letter" is interpreted quite flexibly: not just *English* letters. - +By convention, variable, constant, and function names are *lowercase*, with underscores kept to a reasonable minimum. \ No newline at end of file