0% found this document useful (0 votes)
12 views55 pages

Remix Intro

Introduction to Remix

Uploaded by

Dee Kan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views55 pages

Remix Intro

Introduction to Remix

Uploaded by

Dee Kan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

The Remix Programming

Language
An introductory programming language
Robert Sheehan

1
Ideas

A language to help with program design

Readable

Self documenting

Avoid common problems

None of these things are entirely possible

2
Also inspired by block
programming languages
One of the great things about block languages is that the verbal syntax is
very readable.

The same thing in Python is:


random.choice(a_list)
or
a_list[random.randrange(len(a_list))]
The same thing in Remix is:
pick item from the (list)
or
list [random (length of (list))]
3
Developing a program

The next sequence of slides shows how a programmer


can develop a program starting with a pseudocode
description of the program.

At each step there is a direct translation into Remix as


the programmer re nes the code.

Parameters get generalised when appropriate.

4
fi
Draw 36
circling arrows

Rotating Arrows.
================

prepare the page for drawing

prepare the 36 arrows

animate 24 times per sec


move the arrows

5
Draw 36
circling arrows

Rotating Arrows.
================

prepare the page for drawing

arrows : a list of 36 arrows

animate 24 times per sec


move the arrows

6
Draw 36
circling arrows

Rotating Arrows. a list of 36 arrows :


================ make a list for the arrows
repeat 36 times
prepare the page for drawing create a new arrow
position the arrow
arrows : a list of 36 arrows add the arrow to the list
return the list
animate 24 times per sec
move the arrows

7
Draw (n) circling arrows

Rotating Arrows. a list of (n) arrows :


================ make a list for the arrows
repeat (n) times
prepare the page for drawing create a new arrow
position the arrow
arrows : a list of 36 arrows add the arrow to the list
return the list
animate 24 times per sec
move the arrows

8
Create an arrow

Rotating Arrows. a list of (n) arrows :


================ make a list for the arrows
repeat (n) times
prepare the page for drawing arrow : a new arrow
position the arrow
arrows : a list of 36 arrows add the arrow to the list
return the list
animate 24 times per sec
move the arrows a new arrow :
make an arrow shape
set the arrow colour
arrow

9
Create an arrow

Rotating Arrows. a list of (n) arrows :


================ make a list for the arrows
repeat (n) times
prepare the page for drawing arrow : a new arrow
position the arrow
arrows : a list of 36 arrows add the arrow to the list
return the list
animate 24 times per sec
move the arrows a new arrow :
arrow : an arrow shape
set the (arrow) colour
arrow

an arrow shape :
a shape of (arrow-shape) with size 5

set the (arrow) colour :


arrow’s colour : random colour

10
The arrow shape

a new arrow :
arrow : an arrow shape
a list of (n) arrows : set the (arrow) colour
arrow
make a list for the arrows
repeat (n) times an arrow shape :
arrow : a new arrow a shape of (arrow-shape) with size 5
position the arrow
add the arrow to the list arrow-shape : {
{ 0, 1}
return the list {-2, 2}
{ 0,-2}
{ 2, 2}
}

11
Creating all the arrows

a new arrow :
arrow : an arrow shape
a list of (n) arrows : set the (arrow) colour
arrow
arrows : an empty list
repeat (n) times an arrow shape :
arrow : a new arrow a shape of (arrow-shape) with size 5
position the arrow
add the (arrow) to (arrows) arrow-shape : {
{ 0, 1}
arrows {-2, 2}
{ 0,-2}
{ 2, 2}
}

12
Positioning all the arrows

a list of (n) arrows :

arrows : an empty list


position the (arrow) :
repeat (n) times
x : centre-x + 200 × cosine (angle)
arrow : a new arrow
y : centre-y - 200 × sine (angle)
position the (arrow)
arrow’s position : {x, y}
add the (arrow) to (arrows)
arrow’s heading : -1 × angle
arrows

13
What about the angle?

a list of (n) arrows :


angle : 0
arrows : an empty list
position the (arrow) at (angle) :
repeat (n) times
x : centre-x + 200 × cosine (angle)
arrow : a new arrow
y : centre-y - 200 × sine (angle)
position the (arrow) at (angle)
arrow’s position : {x, y}
add the (arrow) to (arrows)
arrow’s heading : -1 × angle
angle : angle + 10
arrows

14
Animating the arrows

move the arrows :


for each arrow in arrows
animate 24 times per sec get the angle
move the arrows change the angle
position the arrow at angle

15
Animating the arrows

move the (arrows) :


for each (arrow) in (arrows)
animate 24 times per sec get the angle
move the (arrows) change the angle
position the arrow at angle

16
Animating the arrows

move the (arrows) :


for each (arrow) in (arrows)
animate 24 times per sec angle : -1 × arrow’s heading
move the (arrows) angle : angle + 2
position the arrow at angle

17
Animating the arrows

move the (arrows) :


for each (arrow) in (arrows)
animate 24 times per sec angle : -1 × arrow’s heading
move the (arrows) angle : angle + 2
position the (arrow) at (angle)

18
We need to draw it

move the (arrows) :


for each (arrow) in (arrows)
animate 24 times per sec angle : -1 × arrow’s heading
move the (arrows) angle : angle + 2
position the (arrow) at (angle)
fill (arrow)

19
Rotating Arrows.
================
on standard paper

arrows : a list of 36 arrows

animate 24 times per sec


clear layer 1
move the (arrows)

show paper
========================================

arrow-shape : {
{ 0, 1}
{-2, 2}
{ 0,-2}
{ 2, 2}
}

a list of (n) arrows :


angle : 0
arrows : an empty list
repeat (n) times
arrow : a new arrow
position the (arrow) at (angle)
add the (arrow) to (arrows)
angle : angle + 10
arrows

a new arrow :
arrow : an arrow shape
set the (arrow) colour
arrow

an arrow shape :
a shape of (arrow-shape) with size 5

set the (arrow) colour :


arrow’s colour : random colour

position the (arrow) at (angle) :


x : centre-x + 200 × cosine (angle)
y : centre-y - 200 × sine (angle)
arrow’s position : { x, y }
arrow’s heading : -1 × angle

move the (arrows) :


for each (arrow) in (arrows)
angle : -1 × arrow’s heading
angle : angle + 2
position the (arrow) at (angle)
fill (arrow)
20
A similar turtle graphics
program - with turtle objects
turtle graphics on (maroon) paper

radius : 200
step : radius × π ÷ 180

turtles : 36 turtles

animate 30 times per sec


clear layer 1
for each (turtle) in (turtles)
move the (turtle) by (2 × step)
turn the (turtle) 2 degrees
fill the (turtle)

show paper

========================================

(n) turtles:
turtles : an empty list
repeat (n) times
new-turtle: based on (turtle)
new-turtle’s colour : random colour
move the (new-turtle) by (radius)
turn the (new-turtle) 90 degrees
add the (new-turtle) to (turtles)
turn 10 degrees
turtles
21
Other examples
Factorial

The rainfall problem

Fizz-buzz

Insertion sort

22
Other examples
Factorial

The rainfall problem


The beautiful factorial program.
Fizz-buzz ================================

Insertion sort (n)! :


if (n = 0)
return 1
n × (n - 1)!

for each (n) from 1 to 10


{ n, "! = ", (n)! } ↲

23
Other examples
Factorial

The rainfall problem

Fizz-buzz

Insertion sort

The rainfall problem.


=====================
rain-fall : {-1, -3, 0, 5, -2, 1.0, 0, -1, 6, 7, 999, -2, 0, 10}

positive-rain-fall : filter (rain-fall) by (x) where [x ≥ 0]


valid-rain-fall : keep (x) from (positive-rain-fall) until [x = 999]

count : length of (valid-rain-fall)


if (count = 0)
"No valid data." ↲
… otherwise
{ "The average rainfall is ", sum (valid-rain-fall) ÷ count } ↲
24
Other examples
Factorial

The rainfall problem


Fizz-Buzz.
Fizz-buzz
==========

Insertion sort for each (n) from 1 to 100


choose from
[(n) is divisible by 15] do [show "fizz-buzz"]
[(n) is divisible by 3] do [show "fizz"]
[(n) is divisible by 5] do [show "buzz"]
show (n)
show " "

25
Other examples
Insertion Sort.
Factorial ===============

The rainfall problem array : {-1, -3, 0, 5, -2, 1.0, 0, -1, 6, 7, 999}

Fizz-buzz insertion sort (array)

insertion sort (list) :


Insertion sort for each (i) from 2 to (length of (list))
for each (j) from (i) to 2
if (list [j - 1] > list [j])
swap (list) element (j) with (j - 1)
… otherwise
return

swap (array) element (first) with (second) :


temp : array [first]
array [first] : array [second]
array [second] : temp

26
Another just for fun
Find the middle number.
=======================
n1 : get value
n2 : get value
n3 : get value

middle : choose from


(n1) if it's between (n2) and (n3)
(n2) if it's between (n1) and (n3)
(n3) if it's between (n1) and (n2) ; or just "n3"

{ "The middle value is ", middle } ↲


=================================================

get value :
ask for integer "Enter an integer: "

(n1) if it's between (n2) and (n3) ::


((n2 ≤ n1) and (n1 ≤ n3)) produces (n1)
((n3 ≤ n1) and (n1 ≤ n2)) produces (n1)

27
The Remix Language -
function names only 4 keywords:
true, false,
A collection of words and parameters return, redo
A word is a sequence of Unicode characters surrounded by
whitespace (a few characters not allowed)
e.g. ! * what a* and #item a-tag list

A parameter is a word surrounded by parentheses

Any sequence of words and parameters is a function name

Must be on one line (when de ning)

Ends with : (or sometimes ::)

28
fi
Example function names
↲ :
(what) ↲ :
(value1) and (value2) :
not at end of (list) :
repeat (block) until (condition) :
for each (#item) from (start) to (finish) (block) :
filter (list) by (#item) where (condition-block) :
show (list) as list :
draw (colour) circle of (size) at (centre) :
(number) is not divisible by any in (list) :
(n)! :

29
The Remix Language -
function calls
Lisp problem of too many parentheses

Don’t require parentheses if unambiguous

separated by

operator, brace, bracket, comma, colon

but also string, numeric or logic values

Can access top-level variables but can only modify internals if


composite type (list, string, map, object)

all assignments to a variable are local to the function

30
Assignment inside functions
What is the output here?

what is a? :
(a) ↲ 4

what about now? :


a : 3
what is a? 4
(a) ↲ 3

and with (a)? :


what about now? 4
3
(a) ↲
4 - what if "a" wasn’t a
a : 7
parameter?
(a) ↲ 7
a : 4
what is a?
what about now?
and with (a)?
(a) ↲ 4
31
The Remix Language -
syntax
Line based

Every line is one statement

This makes the exible function naming scheme possible

But a line continues to following lines if brackets/braces/


parentheses are not closed

Also an ellipsis … at the start of a line indicates it is a


continuation

Indentation is meaningful (even more than in Python)

32
fl
The Remix Language -
indentation
Code starting at the left side margin is top-level code

function de nitions always start in the top-level

all function de nitions are gathered (possibly compiled)


before any other statements are run

you can put your functions at the top or bottom of


your source le (or anywhere in-between)

all other statements at the top-level are executed in order

33
fi
fi
fi
turtle graphics on (maroon) paper

radius : 200
step : radius × π ÷ 180

turtles : 36 turtles

animate 30 times per sec


clear layer 1
for each (turtle) in (turtles)
move the (turtle) by (2 × step)
turn the (turtle) 2 degrees ; keep as 2 if you want a nice circle
fill the (turtle)

show paper

========================================

(n) turtles:
turtles : {}
repeat (n) times
new-turtle: based on (turtle)
new-turtle’s colour : random colour
move the (new-turtle) by (radius)
turn the (new-turtle) 90 degrees
add the (new-turtle) to (turtles)
turn 10 degrees
turtles

34
The Remix Language -
indentation and deferred blocks
Tabs are used to indent code (pause for general
moaning about this e.g. make les)

All indented areas of code are deferred blocks. They


contain statements which will be evaluated at some
later time.

Blank lines are ignored (the indentation of following


lines is still important).

35
fi
This is really important

We can pass blocks of code as parameters to


functions in a syntactically trivial manner

e.g. in the following code there are two blocks passed


as parameters, one explicit, one implicit
starting with [size : 100] repeat 4 times
plot (arrow) around centre with (size)
size : size + 100

36
Comments
Comments are very exible

A line starting or ending


with a "." This is a comment.
=== so is this ===

A line starting with "=" or - lists can be comments


- like this
"- "
a : 5 ; this is a comment too
Any ";" outside a string
continuing to the end of
the line

37
fl
Statements
assignment statement

my-name : "Robert"

return statement - can return nothing. Functions without explicit "return" will return last value evaluated.

return something

redo statement

redo

expression

there are two pseudo statements - syntactic sugar

setter call to object elds

person’s name : "Robert"

assignment to a list/map item

rain-fall [2] : 13

38
fi
Expressions
Arithmetic - all operators currently have the same precedence - right associative

function calls - always have a value

if no explicit return, use the result of the last statement, some statements
have the value "none"

accessing a list or map item - syntactic sugar


rain-fall [2]

accessing an object eld - syntactic sugar


person’s name

a variable, or literal number, string, logic or list/map/object value

39
fi
Lists
rain-fall : {-1, -3, 0, 5, -2, 1.0, 0, 6, 7, 999, -2, 0, 10}

Lists hold any collection of types - even blocks


if any of { [ball[x] < size], [ball[x] > std-width - size] }
ball[vx] : -1 × ball[vx]

They expand as required

Accessed with [index] - from 1


rain-fall[3] or rain-fall[n]

Literal lists separate items either with commas or newlines

40
Ranges
1 to 10, 5 to -5

direction implicit

ranges (and lists) have built-in iterators

only one per range/list currently

iterator functions

start (range)

next (range)

end of (range)

41
Maps
A map/hash/dictionary - with keys
and values a-shape : {
position : {0, 0}
Can add extra elds after copying heading : 0 ; straight up
size : 5
The { } are used for lists and maps. colour : black
points : {}
}
If the elements are
make shape of (points) :
key : value new-shape : based on (a-shape)
new-shape [points] : points
new-shape
then Remix creates a map,
otherwise it creates a list.

42
fi
Objects
Objectives

make the creation and use of objects as simple as possible

keep it consistent with the rest of the language

the solution is the inverse of Uniform Function Call Syntax

instead of allowing function calls to use the method calling syntax,


making method calls use the function call syntax

at one level this is as simple as passing the object explicitly as a


parameter, but this parameter can be anywhere in the parameter list

43
Objects
The create function makes objects with elds
and methods.

A constructor is an ordinary function call that


returns an object. a person called (a-name) :
create
In a public method signature the (me) or (my) name : a-name
parameter represents the object reference.
getter
name
Can call methods on the same object
from a method with (me) or (my) referring say hello to (me) :
to this object. {"Hi ", name} ↲

Methods without (me) or (my) are private welcome (me) and bye :
to the object. say hello to (me)
{"bye now."} ↲
Field names must be different from
parameters. (No "this" or "self".) person : a person called "Mary"
showline (person’s name)
Syntactic sugar - object’s (the possessive) is say hello to (person)
equivalent to (object) - with parentheses. This welcome (person) and bye
means elds can be referred to in a readable
way. e.g. person’s name
44
fi
fi
Getters and Setters
object elds are private

automatic getters and setters can be speci ed using getter, setter or


getter/setter blocks

a getter is called by:


(the-object) field or the-object’s field

a setter is called by:


(the-object) field : new value or the-object’s field : new value

both can be written manually if access control is desired

45
fi
fi
Extending
Remix does not have inheritance

but you can extend existing objects using a similar syntax to object
creation

extend (existing-object) instead of create

this creates a new object based on the existing one

extend can add new elds and methods

it can also replace existing method de nitions

in this way you can specialise objects in a way similar to


inheritance
46
fi
fi
a person called (a-name) :
create
name : a-name

(my) introduction :
{"My name is ", name, "."} ↲

say hello to (me) :


{"Hi ", name, ", pleased to meet you."} ↲

a person called (name) age (years):


person : a person called (name)

extend (person)
age : years

say hello to (me) :


{"Hi ", name, ", you are ", age, " years old."} ↲

mary : a person called "Mary"


mary’s introduction Output
say hello to (mary)
My name is Mary.
jack : a person called "Jack" age 30 Hi Mary, pleased to meet you.
jack’s introduction My name is Jack.
say hello to (jack) Hi Jack, you are 30 years old.
Hi Stu, you are 56 years old.
say hello to (a person called "Stu" age 56)

47
Blocks and Sequences
Blocks hold code for later execution - deferred

the basis for control structures

Both explicit [ … ] and implicit with indentation

A sequence is a block with one or more statements (or lines)

the main program and all function/method bodies are sequences

Looping is done by calling redo - this takes control back to the start of
the sequence

this is pretty low-level and not intended for beginner use

48
Example control structure
While a "condition" is true keep doing "block"...
while (condition) repeat (block) :
if not (condition)
return last
last : do (block)
redo

... and using it


start (list)
while
not at end of (list)
… repeat
{ end of (list), ": ", next (list) } ↲

49
Transparent functions
Because a block is effectively an anonymous function return inside
a block doesn’t always mean a return from its enclosing function.

We can make this happen using "transparent" functions

this is only used 8 times in the current standard library and


should never be necessary in novice code

we declare a transparent function by ending with "::"

if you call a transparent function from the top-level this returns


back to the level above the top-level which terminates the
program

50
Reference functions
or reference parameters - the
parameter name starts with "#"

Another consequence of creating


for each (#item) in (list)(block) :
control structures in Remix itself start (list)
do
e.g. in a for loop function we need to if (end of (list))
pass a reference to the index or return last
current value so that it can be #item : next (list)
accessed in the body of the for loop last : do (block)
- which is just a block parameter redo

Currently reference functions must


be transpiled before functions which
use them. All reference functions are
transpiled before ordinary functions.
51
Another control structure
With a reference parameter

Create a new list by applying a block to each element of a list.


Uses the variable name passed as #item.
apply (block) to each (#item) from (list):
result : {}
for each (#item) in (list)
append (do (block)) to (result)
result

... and using it


result : apply [a × a] to each (a) from (1 to 10)
(result) as list ↲

{1, 4, 9, 16, 25, 36, 49, 64, 81, 100}

52
The Remix Language -
built-in functions and standard lib functions
Built-in functions include

mathematical operations

if/do/type/input/output functions

list/range/map functions

Graphics and turtle graphics

underlying graphics built-in

turtle graphics implemented solely in Remix itself

Control structures (except if/otherwise)

are all written in Remix (can be inspected and changed by the user)

53
Current implementation
Built in Red (a variant of Rebol)

mostly because it has a built-in parsing language

Originally an interpreter

Now transpiled to Red code

"Easy" to create a compiled version except for some of


the more dynamic aspects of Red

54
Some research questions
Is it helpful or just a bother?

Assuming it is helpful:

Does Remix encourage students to write pseudocode or plan their code?

What is the best level to transition natural language statements into "code" like statements?

What is the best way to convey information about parameter requirements in the function name?

What style of function name is most readable - e.g. should prepositions and articles be
included? (Code in Remix written by a speaker of English contains lots of prepositions in function
names.)

How applicable is this approach to programmers from different languages, Te Reo?

function call structure is completely exible

Does Remix make plagiarism checking simpler/more reliable?

55
fl

You might also like