english improved
This commit is contained in:
@@ -13,7 +13,7 @@ using InteractiveUtils
|
||||
|
||||
# First Contact
|
||||
|
||||
This chapter helps you get started. It omits many details, and the code examples are often rather suboptimal.
|
||||
This chapter helps you get started. It omits many details, and the code examples are often not optimal.
|
||||
|
||||
|
||||
## Julia as a Calculator
|
||||
@@ -35,7 +35,7 @@ function Tab(s)
|
||||
dc = map(x->x.name, REPL.doc_completions(s))
|
||||
l = filter(x->startswith(x,s), dc)
|
||||
println.(l)
|
||||
return # return nothing, since broadcast println produces empty vector
|
||||
return # return nothing, since broadcasting println produces an empty vector
|
||||
end
|
||||
|
||||
▷ = |>
|
||||
@@ -43,7 +43,7 @@ end
|
||||
# IJulia.set_verbose(true)
|
||||
```
|
||||
|
||||
Compute: $\qquad 12^{1/3} + \frac{3\sqrt{2}}{\sin(0.5)-\cos(\frac{\pi}{4})\log(3)}+ e^5$
|
||||
Compute the following: $\qquad 12^{1/3} + \frac{3\sqrt{2}}{\sin(0.5)-\cos(\frac{\pi}{4})\log(3)}+ e^5$
|
||||
|
||||
```{julia}
|
||||
12^(1/3) + 3sqrt(2) / (sin(.5) - cos(pi/4)*log(3)) + exp(5)
|
||||
@@ -54,12 +54,12 @@ Note that:
|
||||
- Powers are written as `a^b`.
|
||||
- The constant `π` is predefined.
|
||||
- `log()` is the natural logarithm.
|
||||
- The multiplication sign `a*b` can be omitted after a number if it is followed by a variable, function, or parenthesis.
|
||||
- The multiplication operator `*` can be omitted after a number when followed by a variable, function, or opening parenthesis.
|
||||
|
||||
|
||||
## The most important keys: `Tab` and `?` {#sec-tab}
|
||||
|
||||
When programming, press the Tab key as soon as 2–3 letters of a word have been typed. Potential completions are then displayed or completed if the completion is unique. This saves time and you learn a lot about Julia.
|
||||
When programming, press the Tab key as soon as you've typed 2–3 letters of a word. Potential completions are then displayed or completed if the completion is unique. This saves time and you learn a lot about Julia.
|
||||
|
||||
```{julia}
|
||||
lo = "lo" #| hide_line
|
||||
@@ -71,7 +71,7 @@ pri = "pri" #| hide_line
|
||||
pri ▷ Tab
|
||||
```
|
||||
|
||||
The built-in Julia help `?name` for all functions and constructs is very comprehensive. Here is a rather brief example:
|
||||
Julia's built-in help system `?name` provides comprehensive documentation for all functions and constructs. Here is a relatively brief example:
|
||||
|
||||
:::{.content-visible unless-format="typst"}
|
||||
|
||||
@@ -137,15 +137,16 @@ Expressions like `x + y = sin(2)` are therefore invalid. Only a variable name ma
|
||||
## Data types
|
||||
|
||||
Julia is a [strongly typed](https://en.wikipedia.org/wiki/Strong_and_weak_typing) language
|
||||
where every object possesses a type. Among the fundamental types are:
|
||||
where every object has a type. Among the fundamental types are:
|
||||
|
||||
|
||||
- Integers,
|
||||
- Floating-point numbers,
|
||||
- Strings and
|
||||
- Integers
|
||||
- Floating-point numbers
|
||||
- Strings
|
||||
- Booleans.
|
||||
|
||||
The type of a variable can be determined using the `typeof()` function.
|
||||
The type of a variable can be determined using the `typeof()` function.
|
||||
|
||||
```{julia}
|
||||
#| warning: true
|
||||
#| error: true
|
||||
@@ -156,7 +157,7 @@ end
|
||||
The standard floating-point number has a size of 64 bits, which corresponds to a `double` in C/C++/Java.
|
||||
|
||||
Julia is a [dynamically typed](https://en.wikipedia.org/wiki/Dynamic_typing) language.
|
||||
Variables have no type; they are typeless references (pointers) to objects.
|
||||
Variables have no type; they are typeless references (pointers) to typed objects.
|
||||
When people speak of the "type of a variable", they mean the type of the object currently assigned to the variable.
|
||||
|
||||
```{julia}
|
||||
@@ -170,7 +171,7 @@ println( typeof(x), " - Value of x = $x" )
|
||||
```
|
||||
|
||||
## Print statements
|
||||
The `println()` function puts a line break at the end, `print()` doesn't.
|
||||
The `println()` function adds a line break at the end; `print()` does not.
|
||||
```{julia}
|
||||
print(y)
|
||||
print("...the line continues...")
|
||||
@@ -180,7 +181,7 @@ println("New line")
|
||||
println("New line")
|
||||
```
|
||||
|
||||
Both functions can accept a list of strings and variables as arguments. Variables can also be embedded in strings by prefixing the variable name with a dollar sign *(string interpolation)*.
|
||||
Both functions can accept a list of string literals and variables as arguments. Variables can also be embedded in strings by prefixing the variable name with a dollar sign *(string interpolation)*.
|
||||
|
||||
```{julia}
|
||||
x = 23
|
||||
@@ -194,22 +195,22 @@ println("x= $x ... and y= $y...$zz") # variant with string interpol
|
||||
:::{.callout-note .titlenormal collapse=true icon=false }
|
||||
## If you want to print a dollar sign...
|
||||
|
||||
you must prepend a *backslash*. If you want to print a *backslash*, you must double it.
|
||||
you must escape it with a *backslash*. To print a *backslash* itself, you must double it.
|
||||
```{julia}
|
||||
println("One dollar: 1\$ and three backslashes: \\\\\\ ")
|
||||
```
|
||||
:::
|
||||
|
||||
## Functions
|
||||
Function definitions start with the keyword `function` and end with the keyword `end`. Typically, they have one or more arguments and return a computed result via a `return` statement.
|
||||
Function definitions begin with the keyword `function` and end with the keyword `end`. Typically, they have one or more arguments and return a computed result via a `return` statement.
|
||||
```{julia}
|
||||
function hypotenuse(a, b) # particularly cumbersome today
|
||||
function hypotenuse(a, b) # a bit cumbersome
|
||||
c2 = a^2 + b^2
|
||||
c = sqrt(c2)
|
||||
return c
|
||||
end
|
||||
```
|
||||
After their definition, the function can be used (called). The variables `a,b,c,c2` used in the definition are local variables and are not available outside the function definition.
|
||||
After definition, the function can be used (called). The variables `a,b,c,c2` used in the definition are local and not available outside the function definition.
|
||||
```{julia}
|
||||
#| error: true
|
||||
x = 3
|
||||
@@ -225,12 +226,12 @@ hypotenuse(a, b) = sqrt(a^2+b^2)
|
||||
|
||||
|
||||
## Tests
|
||||
Tests return a Boolean value.
|
||||
Tests return a Boolean value (`true` or `false`).
|
||||
```{julia}
|
||||
x = 3^2
|
||||
x < 2^3
|
||||
```
|
||||
In addition to the usual arithmetic comparisons `==, !=, <, <= ,> ,>=`
|
||||
In addition to the usual arithmetic comparisons `==, !=, <, <=, >, >=`
|
||||
there are many other tests. The result of a test can also be assigned to a variable, which is then of type `Bool`. The logical operators `&&`, `||` and negation `!` can be used in tests.
|
||||
|
||||
```{julia}
|
||||
@@ -241,9 +242,9 @@ println("$test1 $test2 $test3")
|
||||
```
|
||||
|
||||
|
||||
## Conditional Evaluation
|
||||
## Conditional Statements
|
||||
|
||||
A simple `if` has the form
|
||||
A simple `if` statement has the form
|
||||
```default
|
||||
if <test>
|
||||
<statement1>
|
||||
@@ -252,7 +253,7 @@ if <test>
|
||||
end
|
||||
```
|
||||
|
||||
There can one or several `elseif` blocks and a final `else` block.
|
||||
There can be one or more `elseif` blocks and an optional final `else` block.
|
||||
```{julia}
|
||||
x = sqrt(100)
|
||||
|
||||
@@ -264,7 +265,7 @@ else
|
||||
end
|
||||
```
|
||||
|
||||
Indentation enhances readability but is optional. Line breaks separate statements, as do semicolons. The above code block is equivalent to the following line in Julia:
|
||||
Indentation enhances readability but is optional. Line breaks separate statements, as do semicolons. The code above is equivalent to the following single line in Julia:
|
||||
```{julia}
|
||||
# Please don't program like this! You will regret it!
|
||||
x=sqrt(100); if x > 20 println("Strange!") else println("OK"); y = x + 3 end
|
||||
@@ -274,7 +275,7 @@ x=sqrt(100); if x > 20 println("Strange!") else println("OK"); y = x + 3 end
|
||||
|
||||
## Simple `for` loops
|
||||
|
||||
`for` loops for repeated execution of statements have the form
|
||||
`for` loops for repeating the execution of statements have the form
|
||||
```default
|
||||
for <counter> = start:end
|
||||
<statement1>
|
||||
@@ -294,8 +295,8 @@ sum
|
||||
|
||||
|
||||
## Arrays
|
||||
One-dimensional arrays (vectors) are a simple form of containers. They can be created with square brackets
|
||||
and accessed by index. Indexing starts at 1.
|
||||
One-dimensional arrays (vectors) are a simple container type. They can be created with square brackets
|
||||
and accessed by index, with indexing starting at 1.
|
||||
|
||||
```{julia}
|
||||
v = [12, 33.2, 17, 19, 22]
|
||||
@@ -321,7 +322,7 @@ v
|
||||
|
||||
:::{.callout-note icon="false" .titlenormal collapse="true" font-variant-ligatures="no-contextual" }
|
||||
|
||||
## Postscript: how the effect of the Tab key was simulated on this page
|
||||
## Appendix: how the effect of the Tab key was simulated on this page
|
||||
|
||||
```{julia}
|
||||
using REPL
|
||||
|
||||
Reference in New Issue
Block a user