Skip to content

Commit 2e252ae

Browse files
colinleachBNAndrasdepialColin Leach
authored
[Basics] new concept (#779)
* New concept basics * Update concepts/basics/about.md Co-authored-by: András B Nagy <[email protected]> * Update concepts/basics/about.md Co-authored-by: András B Nagy <[email protected]> * Update concepts/basics/links.json Co-authored-by: András B Nagy <[email protected]> * Fix small typo in about.md * formatting cleanup * recycled old UUID from unwanted concept * a few typos and inelegancies fixed * synced introduction.md with Lasagna exercise * Update config.json --------- Co-authored-by: András B Nagy <[email protected]> Co-authored-by: depial <[email protected]> Co-authored-by: Colin Leach <[email protected]>
1 parent 5f43855 commit 2e252ae

File tree

9 files changed

+352
-5
lines changed

9 files changed

+352
-5
lines changed

concepts/basics/.meta/config.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"authors": [
3+
"colinleach"
4+
],
5+
"contributors": [
6+
"SaschaMann",
7+
"cmcaine"
8+
],
9+
"blurb": "An introduction to Julia variables, constants and functions."
10+
}

concepts/basics/about.md

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
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

concepts/basics/introduction.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Introduction
2+
3+
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.
4+
For that reason, we will introduce named functions as the very first concept.
5+
6+
Julia is a dynamic, strongly-typed programming langauge.
7+
The programming style is mainly functional, though with more flexibility than in languages such as Haskell.
8+
9+
## Variables and assignment
10+
11+
There is no need to declare a variable in advance.
12+
Just assign a value to a suitable name:
13+
14+
```julia-repl
15+
julia> myvar = 42 # an integer
16+
42
17+
18+
julia> name = "Maria" # strings are surrounded by double-quotes ""
19+
"Maria"
20+
```
21+
22+
## Constants
23+
24+
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.
25+
26+
Prefacing an assignment with the `const` keyword allows the compiler to generate more efficient code than is possible for a variable.
27+
28+
Constants also help to protect you against errors in coding.
29+
Accidentally trying to change the `const` value will give a warning:
30+
31+
```julia-repl
32+
julia> const answer = 42
33+
42
34+
35+
julia> answer = 24
36+
WARNING: redefinition of constant Main.answer. This may fail, cause incorrect answers, or produce other errors.
37+
24
38+
```
39+
40+
Note that a `const` can only be declared *outside* any function.
41+
This will typically be near the top of the `*.jl` file, before the function definitions.
42+
43+
## Arithmetic operators
44+
45+
These are the same as in many other languages:
46+
47+
```julia
48+
2 + 3 # 5 (addition)
49+
2 - 3 # -1 (subtraction)
50+
2 * 3 # 6 (multiplication)
51+
8 / 2 # 4.0 (division with floating-point result)
52+
8 % 3 # 2 (remainder)
53+
```
54+
55+
## Functions
56+
57+
There are two common ways to define a named function in Julia:
58+
59+
1. Using the `function` keyword
60+
61+
```julia
62+
function muladd(x, y, z)
63+
x * y + z
64+
end
65+
```
66+
67+
Indentation by 4 spaces is conventional for readability, but the compiler ignores this.
68+
The `end` keyword is essential.
69+
70+
Note that we could have written `return x * y + z`.
71+
However, Julia functions always return the last expression evaluated, so the `return` keyword is optional.
72+
Many programmers prefer to include it to make their intentions more explicit.
73+
74+
2. Using the "assignment form"
75+
76+
```julia
77+
muladd(x, y, z) = x * y + z
78+
```
79+
80+
This is most commonly used for making concise single-expression functions.
81+
82+
A `return` keyword is *never* used in the assignment form.
83+
84+
The two forms are equivalent, and are used in exactly the same way, so choose whichever is more readable.
85+
86+
Invoking a function is done by specifying its name and passing arguments for each of the function's parameters:
87+
88+
```julia
89+
# invoking a function
90+
muladd(10, 5, 1)
91+
92+
# and of course you can invoke a function within the body of another function:
93+
square_plus_one(x) = muladd(x, x, 1)
94+
```
95+
96+
## Naming conventions
97+
98+
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.
99+
100+
By convention, variable, constant, and function names are *lowercase*, with underscores kept to a reasonable minimum.

concepts/basics/links.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[
2+
{
3+
"url": "https://docs.julialang.org/en/v1/",
4+
"description": "Official documentation"
5+
},
6+
{
7+
"url": "https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-Python",
8+
"description": "Noteworthy Differences from Python"
9+
},
10+
{
11+
"url": "https://www.youtube.com/watch?v=X4Alzh3QyWU",
12+
"description": "Exercism: A Brief Introduction to Julia"
13+
},
14+
{
15+
"url": "https://julialang.org/learning/",
16+
"description": "Get Started with Julia"
17+
}
18+
]

config.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,11 @@
11251125
]
11261126
},
11271127
"concepts": [
1128+
{
1129+
"uuid": "006ebce8-87cd-4695-87e6-8a7b8dc2f239",
1130+
"slug": "basics",
1131+
"name": "Basics"
1132+
},
11281133
{
11291134
"uuid": "32384ed4-ef26-4118-8ea3-4d44c331e828",
11301135
"slug": "booleans",
@@ -1135,11 +1140,6 @@
11351140
"slug": "vectors",
11361141
"name": "Vectors"
11371142
},
1138-
{
1139-
"uuid": "006ebce8-87cd-4695-87e6-8a7b8dc2f239",
1140-
"slug": "integer-introduction",
1141-
"name": "Integers and Arithmetic Operations"
1142-
},
11431143
{
11441144
"uuid": "9babe703-1f5f-4f59-a4b2-349b8d461dd8",
11451145
"slug": "conditionals",

0 commit comments

Comments
 (0)