Work in Progress

Basics

What are the basic fundamentals of programming? i.e. The basic building blocks for a beginner. I ignore all advanced issues such as concurrent processing! I think it boils down to this combination of algorithmic structures and data structures:

  1. Sequence of instructions
  2. Conditionals
  3. Loops
  4. Functions
  1. Fundamental Data types
  2. Arrays and Lists
  3. Structures, records

Can these be simply expressed in Julia?

Sequence of instructions, of course!

Conditionals


            if (boolean condition)
                :
                code for true case
                :
            else
                :
                code for false case
                :
            end
            

For example, this code returns true if 'year' is a leap year. (In the Gregorian calendar.) As in many other programming languages the % symbol is used to indicate the mathematical idea of division modulus the number following the % symbol.

            if year % 100 == 0
                return year % 400 == 0
            else
                return year % 4 == 0
            end
            

At some point the ternary operator can be introduced

The combination of "if ... elseif ... else ... end" can be introduced

Loops

Given that arrays or some other iterable data structure is introduced we have, for example, this code which prints out the upper case letters given the lowercase ones.


            for c in 'a':'z'
                print(uppercase(c))
            end
            

There is a similar thing that can be done in one line. I think it rather presupposes that anonymous functions is covered by the time it is introduced.


                a = [1, 2, 3]; 
                foreach(x -> println(x ^ 3), a)
            

There is a loop that says "while a condition is met: do something ". For example to print the decimal values of 1, 1/2, 1/4, 1/8. 1/16, 1/32:


            x = 1
            while( x > 1/64 )
                print(x, ", ")
                x = x/2
            end

            #= 
            The output is: 1, 0.5, 0.25, 0.125, 0.0625, 0.03125,
            =#
            

Functions

Home territory for Julia! For example, we turn the leap year code above into a function like this:


            """
            is_leap_year(year)
            Return `true` if `year` is a leap year in the gregorian calendar.
            """
            function is_leap_year(year)
                if year % 100 == 0
                    return year % 400 == 0
                else
                    return year % 4 == 0
                end
            end                
            

This is fertile ground for discussing how the arguments in the function relate to the values supplied when the function is called. For example, simple types are passed by value and cannot be changed from inside the function. Strings are also immutable. However, in the case of arrays etc, the normal practice is to create a new array. However, it is possible to change the values inside a sequence.

The following is taken from the Julia documentation.

Julia function arguments follow a convention sometimes called "pass-by-sharing", which means that values are not copied when they are passed to functions. Function arguments themselves act as new variable bindings (new "names" that can refer to values), much like assignments argument_name = argument_value, so that the objects they refer to are identical to the passed values. Modifications to mutable values (such as Arrays) made within a function will be visible to the caller. (This is the same behavior found in Scheme, most Lisps, Python, Ruby and Perl, among other dynamic languages.)

For example, in the function

function f(x, y)
    x[1] = 42    # mutates x
    y = 7 + y    # new binding for y, no mutation
    return y
end

The statement x[1] = 42 mutates the object x, and hence this change will be visible in the array passed by the caller for this argument. On the other hand, the assignment y = 7 + y changes the binding ("name") y to refer to a new value 7 + y, rather than mutating the original object referred to by y, and hence does not change the corresponding argument passed by the caller. This can be seen if we call f(x, y):

julia> a = [4, 5, 6]
3-element Vector{Int64}:
    4
    5
    6

julia> b = 3
3

julia> f(a, b) # returns 7 + b == 10
10

julia> a  # a[1] is changed to 42 by f
3-element Vector{Int64}:
    42
    5
    6

julia> b  # not changed
3                
            

The only problem is, there is a lot of detail. For example, tuples are a lot like arrays but they are genuinely immutable.

Then there is the vexed issue of functions that access global values....

Data Types

An introductory page on this is needed. Work in progress.