|
| 1 | +# About |
| 2 | + |
| 3 | +Julia is a dynamic, strongly-typed programming language. |
| 4 | +The programming style is mainly functional, though with more flexibility than in languages such as Haskell. |
| 5 | + |
| 6 | +There is a strong and versatile type system, which will become important in later concepts. |
| 7 | +In practice, Julia will usually infer a suitable default from the context. |
| 8 | + |
| 9 | +Despite its emphasis on readability, making it appear as a scripting language like Python and Ruby, Julia code is compiled before running. |
| 10 | +The Just In Time (JIT) compiler produces highly optimized code for each function as it is enountered, leading to high runtime speed. |
| 11 | + |
| 12 | +Julia is not an object-oriented language, and there is no class hierarchy of the sort central to many other languages. |
| 13 | + |
| 14 | +Instead *(and please don't panic as you read this!)*, Julia relies on: |
| 15 | +- A *Type* hierarchy. |
| 16 | +- *Composable* functions (designed to combine easily, in versatile and powerful ways). |
| 17 | +- *Multiple dispatch*, meaning functions can be called with variable number and types of arguments. |
| 18 | + |
| 19 | +These rather confusing terms should become clearer as we progress through the syllabus. |
| 20 | + |
| 21 | +The [official documentation][official-documentation] contains a lot of valuable information for the current release, though it can be a bit overwhelming for beginners. |
| 22 | + |
| 23 | +- [Manual][manual] |
| 24 | +- [Tutorials][tutorials] |
| 25 | +- [Get Started with Julia][get-started] |
| 26 | +- [Notes for Python programmers][diff-python] |
| 27 | +- [Notes for R programmers][diff-r] |
| 28 | +- [Notes for C/C++ programmers][diff-c] |
| 29 | +- [Notes for Matlab programmers][diff-matlab] (this is a proprietary language, very popular with engineers and one inspiration for Julia) |
| 30 | + |
| 31 | +Videos include: |
| 32 | +- [A Brief Introduction to Julia][erik] from the Exercism management! |
| 33 | +- [A Gentle Introduction to Julia][gentle-intro] |
| 34 | +- [Learn Julia With Us][learn-with-us] : a 4-part series |
| 35 | + |
| 36 | +## Comments |
| 37 | + |
| 38 | +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. |
| 39 | +They are (mostly) ignored by the compiler. |
| 40 | + |
| 41 | +Two options are possible in Julia: |
| 42 | +- Single-line comments start with `#` |
| 43 | +- Multi-line comments start with `#=` and end with `=#`. Nesting is allowed. |
| 44 | + |
| 45 | +```julia |
| 46 | +# This is a single-line comment |
| 47 | + |
| 48 | +x = 3 # This is an inline comment |
| 49 | + |
| 50 | +#= |
| 51 | + Multi-line comments can be used for longer explanations. |
| 52 | +
|
| 53 | + They are especially useful to comment out blocks of code during debugging. |
| 54 | +=# |
| 55 | +``` |
| 56 | + |
| 57 | +## Variables and assignment |
| 58 | + |
| 59 | +To create a [variable][variables], just assign a value to it: |
| 60 | + |
| 61 | +```julia-repl |
| 62 | +julia> myvar = 42 # an integer (in fact, Int64) |
| 63 | +42 |
| 64 | +
|
| 65 | +julia> bigint = 1_234_567_890 # optionally use underscore as digit separator, for readability |
| 66 | +1234567890 |
| 67 | +
|
| 68 | +julia> name = "Maria" # strings are surrounded by double-quotes "" |
| 69 | +"Maria" |
| 70 | +``` |
| 71 | + |
| 72 | +Assignment ["binds"][binding] a value to the variable. |
| 73 | + |
| 74 | +Specifying a type is optional, and mostly Julia will infer a suitable default from the context. |
| 75 | + |
| 76 | +Types are an important subject in Julia. |
| 77 | +We will explore them further in later concepts. |
| 78 | +For now, it is best not to worry about them. |
| 79 | + |
| 80 | +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: |
| 81 | + |
| 82 | +```julia-repl |
| 83 | +julia> myvar = 3 |
| 84 | +3 |
| 85 | +
|
| 86 | +julia> myvar = "now a string" |
| 87 | +"now a string" |
| 88 | +``` |
| 89 | + |
| 90 | +## Constants |
| 91 | + |
| 92 | +Global variables, created outside any function, are: |
| 93 | +- Allowed. |
| 94 | +- Sometimes necessary. |
| 95 | +- Usually discouraged (though only within `*.jl` files; the REPL operates differently). |
| 96 | + |
| 97 | +If a value needs to be available throughout the program, but is not expected to change, use a [constant][constants] instead. |
| 98 | + |
| 99 | +Prefacing the assignemt with the `const` keyword allows the compiler to generate more efficient code. |
| 100 | + |
| 101 | +Accidentally trying to change the `const` value will give a warning: |
| 102 | + |
| 103 | +```julia-repl |
| 104 | +julia> const answer = 42 |
| 105 | +42 |
| 106 | +
|
| 107 | +julia> answer = 24 |
| 108 | +WARNING: redefinition of constant Main.answer. This may fail, cause incorrect answers, or produce other errors. |
| 109 | +24 |
| 110 | +``` |
| 111 | + |
| 112 | +## Arithmetic operators |
| 113 | + |
| 114 | +[Arithmetic][operators] mostly works conventionally: |
| 115 | + |
| 116 | +```julia |
| 117 | +2 + 3 # 5 (addition) |
| 118 | +2 - 3 # -1 (subtraction) |
| 119 | +2 * 3 # 6 (multiplication) |
| 120 | +8 / 2 # 4.0 (division) |
| 121 | +8 % 3 # 2 (remainder) |
| 122 | +2 ^ 3 # 8 (exponentiation) |
| 123 | +``` |
| 124 | + |
| 125 | +Note that division with `/` always gives a floating-point value. |
| 126 | + |
| 127 | +We will return to this in later concepts, which will discuss integer division with `div()` or `÷` to truncate and `//` to get a rational number. |
| 128 | + |
| 129 | +## Functions |
| 130 | + |
| 131 | +For best runtime performance, it is best to place most of the code inside [functions][functions]. |
| 132 | +Having lots of small functions is fine, in contrast to some other languages. |
| 133 | + |
| 134 | +There are two common ways to define a named function in Julia: |
| 135 | + |
| 136 | +1. Using the `function` keyword |
| 137 | + |
| 138 | + ```julia |
| 139 | + function muladd(x, y, z) |
| 140 | + x * y + z |
| 141 | + end |
| 142 | + ``` |
| 143 | + |
| 144 | + Indentation by 4 spaces is conventional for readability, but the compiler ignores this. |
| 145 | + The `end` keyword is essential: more like Ruby than like Python. |
| 146 | + |
| 147 | + Note that we could have written `return x * y + z`. |
| 148 | + However, Julia functions always return the last expression evaluated, so the `return` keyword is optional. |
| 149 | + Many programmers prefer to include it to make their intentions more explicit. |
| 150 | + |
| 151 | +2. Using the "assignment form" |
| 152 | + |
| 153 | + ```julia |
| 154 | + muladd(x, y, z) = x * y + z |
| 155 | + ``` |
| 156 | + |
| 157 | + This is most commonly used for one-line function definitions or mathematical functions, where the function body is a single expression. |
| 158 | + A `return` keyword is *never* used in the assignment form. |
| 159 | + |
| 160 | +The two forms are equivalent, and are used in exactly the same way, so choose whichever is more readable. |
| 161 | + |
| 162 | +Invoking a function is done by specifying its name and passing arguments for each of the function's parameters: |
| 163 | + |
| 164 | +```julia |
| 165 | +# invoking a function |
| 166 | +muladd(10, 5, 1) |
| 167 | +
|
| 168 | +# and of course you can invoke a function within the body of another function: |
| 169 | +square_plus_one(x) = muladd(x, x, 1) |
| 170 | +``` |
| 171 | + |
| 172 | +From the fact that we refer to these as "named" functions, you might guess that there are also "anonymous" functions. |
| 173 | +These will be covered in a later concept. |
| 174 | + |
| 175 | +## Naming conventions |
| 176 | + |
| 177 | +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. |
| 178 | + |
| 179 | +By convention, variable, constant and function names are *lowercase*, with underscores avoided except when needed to avoid confusion. |
| 180 | + |
| 181 | +However, Julia uses [Unicode][unicode] throughout, so "letter" is interpreted quite flexibly. |
| 182 | + |
| 183 | +Languages other than English are supported, so are emojis *(though please use good taste and pick something appropriate)*. |
| 184 | + |
| 185 | +In particular, all the Greek letters loved by mathematicians are available *(and several useful values are pre-defined)*: |
| 186 | + |
| 187 | +```julia-repl |
| 188 | +julia> π # a built-in constant |
| 189 | +π = 3.1415926535897... |
| 190 | +``` |
| 191 | + |
| 192 | +Julia-aware editors, including the REPL, Pluto.jl, and VS Code with the Julia plugin, make these characters easy to enter. |
| 193 | + |
| 194 | +For π, type `\pi` then hit `<Tab>`. Your typing will be replaced by the Greek character. |
| 195 | + |
| 196 | +You will see many more examples of this in later concepts. |
| 197 | + |
| 198 | +In fact, the backslash-abbreviations are not something specific to Julia. |
| 199 | +They are taken from [LaTeX][latex], a typesetting system that many scientists and engineers use almost daily to write reports. |
| 200 | + |
| 201 | +[official-documentation]: https://docs.julialang.org/en/v1/ |
| 202 | +[manual]: https://docs.julialang.org/en/v1/manual/getting-started/ |
| 203 | +[tutorials]: https://julialang.org/learning/tutorials/ |
| 204 | +[diff-python]: https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-Python |
| 205 | +[diff-r]: https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-R |
| 206 | +[diff-c]: https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-C/C |
| 207 | +[diff-matlab]: https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-MATLAB |
| 208 | +[get-started]: https://julialang.org/learning/ |
| 209 | +[erik]: https://www.youtube.com/watch?v=X4Alzh3QyWU |
| 210 | +[gentle-intro]: https://www.youtube.com/watch?v=4igzy3bGVkQ |
| 211 | +[learn-with-us]: https://www.youtube.com/watch?v=oTUmW8dWZws |
| 212 | +[variables]: https://docs.julialang.org/en/v1/manual/variables/ |
| 213 | +[constants]: https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Constants |
| 214 | +[operators]: https://docs.julialang.org/en/v1/manual/mathematical-operations/ |
| 215 | +[functions]: https://docs.julialang.org/en/v1/manual/functions/ |
| 216 | +[naming]: https://docs.julialang.org/en/v1/manual/variables/#man-allowed-variable-names |
| 217 | +[unicode]: https://en.wikipedia.org/wiki/Unicode |
| 218 | +[latex]: https://en.wikipedia.org/wiki/LaTeX |
| 219 | +[binding]: https://docs.julialang.org/en/v1/manual/variables/#man-assignment-expressions |
0 commit comments