Four kinds of articles, each practising a different skill. Every one is marked with its level, from A1 to C2.
Readers
Short programs to read before you run them. Guess what each one prints, run it, then read the notes.
- CountdownA rocket counts down from ten. Read the program and guess what it prints before you run it.
- Is it a palindrome?Six words, read forwards and backwards. Guess which of them are palindromes before you run the program.
- At the marketFive things from the market and what they cost. In what order does the program list them?
- Weather in four citiesToday’s temperature in Riga, Amsterdam, Turin and Belgrade, in Celsius and Fahrenheit, with a word for how it feels.
- The departure boardFive trains, one small class, and three questions to the timetable — all trains in order, the trains to Rotterdam, and the first one after nine.
- Pancakes for sixA recipe for four people, scaled for six and for five. A third of a cup of sugar shows how Raku keeps fractions exact.
- The school tournamentFour school teams play each other once. Six results become a league table in twenty lines, with
X,Z,[+],»and a junction. - A pocket calculatorA grammar and an action class that read an arithmetic expression like
2 + 3 * (4 - 1)and work out its value.
False friends
Code that looks the same in Python, Perl or JavaScript and in Raku, but means something else.
- + adds, even when the numbers are stringsIn JavaScript,
"10" + 5is"105", and in Python it is an error. In Raku it is 15, because+always adds. - == does not compare stringsIn Python and JavaScript,
==compares strings too. In Raku,==compares numbers, and strings are compared witheq. - @a[-1] is not the last elementIn Python and Perl, index -1 counts from the end. In Raku it does not even compile.
- 0.1 + 0.2 is exactly 0.3In Python and JavaScript,
0.1 + 0.2is not equal to0.3. In Raku it is, because0.1is stored as a fraction. - There is no length
len,.lengthandlengthdo not exist in Raku. A string has.chars, and a list has.elems. - // is not integer divisionIn Python,
7 // 2is 3. In Raku,//means "or else, if there is no value", so7 // 2is 7. - Splitting on nothing does not give you the lettersIn JavaScript and Perl, splitting on an empty string gives the characters. In Raku you also get an empty string at each end. Use
.comb. - my $n = @array is not a countIn Perl, putting an array into a scalar variable gives you its length. In Raku, the scalar holds the whole array.
Word families
Methods and operators that are related, learned together so that you stop mixing them up.
- comb, words, lines, splitFour methods that cut a string into a list of pieces, and how to pick the right one.
- say, put, print, noteFour ways to print something, and why
sayshows an array with brackets whileputdoes not. - map, grep, first, classifyFour methods that walk through a list and do something with each element, and how to tell which one you need.
- x, xx and XThree operators that look alike — one repeats a string, one repeats a list element, and one combines two lists.
- ==, eq, eqv, === and ~~Five ways to ask whether two things are equal, and the question each one really asks.
Accents
One task written with a Python, a Perl and a C accent, and then the way a native writes it.
- Counting wordsThe three most frequent words in a sentence, written four times — with a Python, a Perl and a C accent, and as a native would write it.
- Totals per cityAdding up sales per city from a small table, written with a Python, a Perl and a C accent, and as a native would write it.
- Fibonacci numbers below 1000The Fibonacci numbers below 1000, written with a Python, a Perl and a C accent, and with a native Raku sequence.