further work on translation

This commit is contained in:
2026-03-02 14:43:14 +01:00
parent 457873a31b
commit c6609d15f5
12 changed files with 254 additions and 110 deletions

View File

@@ -1,5 +1,7 @@
---
engine: julia
julia:
exeflags: ["--color=yes"]
---
```{julia}
@@ -36,7 +38,7 @@ function Tab(s)
return # return nothing, since broadcast println produces empty vector
end
= |>
= |>
# IJulia.set_verbose(true)
```
@@ -57,7 +59,7 @@ Note that:
## The most important keys: `Tab` and `?` {#sec-tab}
When programming, repeatedly press the Tab key as soon as 23 letters of a word have been typed. Potential completions are then displayed or completed if the completion is unique. This saves time and is extremely helpful.
When programming, press the Tab key as soon as 23 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.
```{julia}
lo = "lo" #| hide_line
@@ -71,11 +73,14 @@ pri ▷ Tab
The built-in Julia help `?name` for all functions and constructs is very comprehensive. Here is a rather brief example:
:::{.content-visible unless-format="typst"}
```{julia}
?for
```
:::
:::{.content-hidden unless-format="xxx"}
:::{.content-hidden unless-format="typst"}
::: {.cell }
```{.julia .cell-code}
@@ -84,18 +89,23 @@ The built-in Julia help `?name` for all functions and constructs is very compreh
::: {.cell-output .cell-output-stdout}
```
search: for foreach foldr floor mapfoldr factorial EOFError OverflowError
search: for nor xor foldr floor Core sort
for loops repeatedly evaluate a block of statements while iterating over a sequence of values.
for
Examples
for loops repeatedly evaluate a block of statements while iterating over a sequence of values.
julia> for i in [1, 4, 0]
println(i)
end
1
4
0
The iteration variable is always a new variable, even if a variable of the same name exists in the enclosing scope. Use outer to reuse an existing local variable for iteration.
Examples
========
julia> for i in [1, 4, 0]
println(i)
end
1
4
0
```
:::
@@ -105,7 +115,7 @@ julia> for i in [1, 4, 0]
## Variables and Assignments
Variables are created through assignment with the assignment operator `=`. Afterwards, they can be used in further statements.
Variables are created through assignment with the assignment operator `=`.
```{julia}
x = 1 + sqrt(5)
@@ -124,10 +134,11 @@ Expressions like `x + y = sin(2)` are therefore invalid. Only a variable name ma
:::
## Data types
## 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:
Julia is a [strongly typed](https://en.wikipedia.org/wiki/Strong_and_weak_typing) language. All objects have a type.
Among other things, there are the basic types
- Integers,
- Floating-point numbers,
@@ -139,7 +150,7 @@ The type of a variable can be determined using the `typeof()` function.
#| warning: true
#| error: true
for x ∈ (42, 12.0, 3.3e4, "Hello!", true)
println("x = ", x, " ..... Type: ", typeof(x))
println("x = ", x, " ... Type: ", typeof(x))
end
```
The standard floating-point number has a size of 64 bits, which corresponds to a `double` in C/C++/Java.
@@ -159,7 +170,7 @@ println( typeof(x), " - Value of x = $x" )
```
## Print statements
The `println()` function differs from `print()` in that it outputs a line break at the end.
The `println()` function puts a line break at the end, `print()` doesn't.
```{julia}
print(y)
print("...the line continues...")
@@ -168,14 +179,15 @@ println(y)
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 strings 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
y = 3x + 5
zz = "Done!"
println("x= ", x, " ...and y= ", y, "...", zz) # 1. variant
println("x= $x ...and y= $y...$zz") # variant with string interpolation
println("x= ", x, " ... and y= ", y, "...", zz) # 1. variant
println("x= $x ... and y= $y...$zz") # variant with string interpolation
```
@@ -189,7 +201,7 @@ println("One dollar: 1\$ and three backslashes: \\\\\\ ")
:::
## Functions
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 the `return` statement.
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.
```{julia}
function hypotenuse(a, b) # particularly cumbersome today
c2 = a^2 + b^2
@@ -206,7 +218,7 @@ println("z = $z")
println("c = $c")
```
Very simple functions can also be defined as single-line functions.
Very simple functions can also be defined as one-liners:
```{julia}
hypotenuse(a, b) = sqrt(a^2+b^2)
```
@@ -219,7 +231,7 @@ x = 3^2
x < 2^3
```
In addition to the usual arithmetic comparisons `==, !=, <, <= ,> ,>=`
there are many other tests. Of course, 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.
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}
test1 = "Car" in ["Bicycle", "Car", "Train"]
@@ -229,9 +241,9 @@ println("$test1 $test2 $test3")
```
## Branches
Branches (conditional statements) have the form
## Conditional Evaluation
A simple `if` has the form
```default
if <test>
<statement1>
@@ -240,7 +252,7 @@ if <test>
end
```
An `else` branch and `elseif` branches are possible.
There can one or several `elseif` blocks and a final `else` block.
```{julia}
x = sqrt(100)
@@ -252,13 +264,12 @@ else
end
```
Indentation improves readability but is optional. Line breaks separate statements, and this can also be done with semicolons. The above code block is identical to the following line in Julia:
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:
```{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
```
It is strongly recommended to format your own code from the beginning with clear indentation!
## Simple `for` loops
@@ -283,7 +294,7 @@ sum
## Arrays
1-dimensional arrays (vectors) are a simple form of containers. They can be created with square brackets
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.
```{julia}