Title: Art, Math, Music and Computer Programming Languages
1Art, Math, MusicandComputer Programming
Languages
2Programming with Functions
Everyone understands functions f(x) x 1
g(x,y) sine(x) - y abs(x) if x lt 0 then
-x else x Functional Programming uses functions
(and nothing else!) to write computer
programs. We use functions to create some simple
but interesting programs that explore things like
math or music or art
3Programming languages
- If you use languages like Java or C you have to
tell the computer a lot about how to do
something. You need to learn a lot to do a
little but you can do almost anything if you work
really hard - We are interested in languages where you dont
have to say how to do things - anyone can write
programs in these languages! Our languages deal
with specific domains - they are not meant to do
everything.
4Everything is a Function
Why functions? Because we can represent just
about everything with them Pictures are
functions from points onto colors Music is a
function from time to sets of notes Math is a
study of functions In all of these areas, we
want to capture patterns and give them names
Reflection Shapes Scales Rotations
Repetition
5Notation
- Using a computer requires precise notations.
- f x y -- function f applied to arguments x
and y - (x,y) -- A 2-dimensional point (or vector)
- f x x1 -- A function definition
- f (x,y) (y,x) -- f has one argument, a point
-
6Functional Images
- We use a language called Pan (Picture Animation)
to create pictures.
A picture is a function from a coordinate (place
on the X-Y plane) to a color. Computer geeks
write things like this a Picture has type
Point -gt Color You should know about
coordinates but ... Whats a color?
7Colors
We describe colors using 3 numbers, each between
0 and 1 Amount of Red Amount of Green
Amount of Blue Example Yellow Red Green
rgb 1 1 0 We can also add Transparency 0
clear, 1 solid rgba 1 1 0 0.5 --
semi-transparent yellow
8A Picture!
- We can define a simple picture like this
- pic (x,y) if abs x abs y lt 100
- then black else white
-
- What does this define?
- The coordinate system is in pixels and the origin
is at the center of the window.
Test1
9Another Picture!
- More colors! Lets change the black to a color
that varies with location - pic (x,y) if abs x abs y lt 100
- then rgb 0.5 (abs x/100) (abs y/100)
- else white
- rgb defines a color from the red, green, and blue
components. -
Test2
10Adding Controls
- r lt- slider red (0,1) 0
- pic (x,y) if abs x abs y lt 100
- then rgb r (abs x/100) (abs y/100)
- else white
-
- slider uses a label, a range, and an initial
value to define an interactive controller
Test3
11Transformations
A function that maps one point onto another is a
transformation A Transformation has type
Point -gt Point moveIt (x,y) (x1, y1) What
is moveIt (2,3)? twistIt (x,y) (y, -x) What
is twistIt (2,3)
12Visualizing Transformations
So how can we see a transform on points? One
way is to use it as a lens to view an
image. Heres a transformation f (x,y) (xx,
yy) f (0,0) (0,0) f (1,1) f(-1,1) f(1,-1)
f (-1,-1) (1,1) f (2,2) (4,4) So, if we
look at (2,2) well see whats at (4,4)
13Heres what happens
y
x
Test4
14Symmetry
The image symmetric about both the x and y
axis f(-x,y) f(x,y) Can you prove
this?? f(x,-y) f(x,y)
15Polar Coordinates
A different way to locate points on a plane Use
distance and angle to the origin
d
angle
Simple transformation in polar coordinates have
interesting effects
16Swirling
This transformation adds extra rotation as
points move out from the origin f (distance,
angle) (distance, angle k distance) k is a
parameter we can adjust this and watch how the
picture changes.
Test5
17Altering the Distance
f (r, theta) (g r theta, theta) What if g
r theta r k (k is a parameter) g r
theta r ktheta g r theta rrk
Test6
Test7
Test8
18Rippling
Move further or closer to the origin depending on
the angle g r theta r (1 ksine (n
theta)) The sine function generates a ripple.
Test9
Sine tells you how high up you are on the unit
circle given an angle.
19Ramps
A periodic function repeats itself. One of the
simplest periodic functions is a series of ramps
from 0 to 1, repeated over and over ramps x
x when 0 lt x and x lt 1 x ramps
(x1) when x lt 0 ramps (x-1) when x gt 1
A more useful version of this function is
parameterized by its period pramps p x p
ramps (x/p)
y ramps p x
20 Using Ramps
p lt- slider Size of square (10,300) 100 f (x,y)
(pramps p x, pramps p y) This replicates the
image in the square of size s over the entire
plane (tiling)
Test10
21A Sawtooth
These function is symmetric teeth(x) x when
0 lt x and x lt ½ 1-x when
½ lt x and x lt 1 ..... plus rest of
ramps We can also parameterize by the period
(sawtooth) Tiling the angle in polar coordinates
kaleidoscope f (distance, angle)
(distance, sawtooth(width, angle))
22Using the Sawtooth
Tiling f (x,y) (pteeth p x, pteeth p
y) Circular reflections f (r, theta)
(pteeth p r, theta) Kaleidoscope f
(t, theta) (r, pteeth p theta)
test11
test12
test13
23The Grand Finale
Define factor to take apart a number x as
follows (big, little) factor p x
where big is a multiple of p ,
-p/2 lt little lt p/2, and big
little x Now, apply a warping t such that
slicer p t (x,y) (x bigx, y bigy)
where (bigx, littlex) factor p x
(bigy, littley) factor p y (x, y) t
(littlex, littley)
24Apply this to the following transformations
f (r, theta) (rk, theta) f (r, theta)
(0, 0)
test14
test15
25Haskore
- Motivation
- Traditional music notation has many limitations
- Unable to express a composers intentions.
- Biased toward music that is humanly performable.
- Unable to express notions of algorithmic
composition.Haskore (Haskell Score) is a
library of Haskellfunctions and datatypes for
composing music.
26Defining Music
- Can we create music with functions? Of course!
- NoteName Cf C Cs Df D Ds Ef E
Es Ff F Fs - Gf G Gs Af A As Bf
B Bs - Music
- Note (NoteName, Integer) Duration Attributes
-- a note - Rest Duration -- a rest
- Music Music -- sequential
composition - Music Music -- parallel
composition - Tempo Float Music -- scale the tempo
- Trans Integer Music -- transposition
-
27C Major Scale
For convenience, c oct duration attributes Note
(C, oct) duration attributes cmaj c 3 0.2
d 3 0.2 e 3
0.2 .. c 4 0.2
28Simple Transformations
Add a parallel line 7 notes higher pfifth m
m Trans 7 m cmaj5 pfifth cmaj
againFaster m m (Tempo 2 m) cmaj5twice
againFaster cmaj5 cmaj5four againFaster
cmaj5twice
29Function Composition
Function composition notation (f . g) x f
(g x) Make a pipeline of transformers. pfifth
. Trans 5 . Tempo 2 -- Music transfomers
means double the tempo, transpose up 5, and add
a parallel part a fifth
higher Such notation makes it easier to work
with functions!
30Types
Types help us understand functions x Music
-- x is a piece of music y Music -gt Music
-- y a function that accepts music
and returns music (a music
transformer) z Integer -gt Music -gt Music --
z has two arguments, an
integer and some
music z 3 -- if passed only one argument, you
get a music transformer Using only some of the
arguments is called currying named after Haskell
Curry
31Simple Iteration
We use functions to capture music structures One
such structure is simple iteration rpt
Integer -gt Music -gt Music rpt 0 m Rest 0
--- a null piece of music rpt n m m rpt
(n-1) m Use recursion to define a function that
iterates n times cmaj4 rpt 4 cmaj What does
rpt 3 . rpt 3 do?
32More Complex Patterns
Why repeat the same thing exactly the same? We
can add a function that changes the music with
each iteration rptm Integer -gt (Music -gt
Music) -gt Music -gt Music rptm 0 f m Rest 0 rptm
n f m m rptm (n-1) f (f m) cmr rptm 4
pfifth cmaj cmr1 rptm 4 (pfifth . Tempo 1.5)
cmaj
This changes the music!
33More Examples
A function to recursively apply transformations f
(to elements in a sequence) and g (to accumulated
phrases) rep (Music -gt Music) -gt (Music -gt
Music) -gt Integer -gt Music -gt Music rep f g 0 m
Rest 0 rep f g n m m g (rep f g (n-1) (f
m)) An example using "rep" three times,
recursively, to create a "cascade" of
sounds. run rep (Trans 5) (delay
tn) 8 (c 4 tn) cascade rep (Trans 4) (delay
en) 8 run cascades rep id (delay sn) 2
cascade waterfall cascades revM cascades
34How Did That Work?
rep f g 0 m Rest 0 rep f g n m m g (rep f
g (n-1) (f m)) At each step, apply f going
down and g coming back Play m at the same time
as the rep part If n 2, we get rep f g 2 m
m g ( rep f g 1 (f m))
m g ( f m g (rep f g o (f (f m))))
m g ( f m g (Rest
0)) run rep (Trans 5) (delay tn) 8
(c 4 tn)
35The Grand Finale
Write a function that plays a piece of music with
the same music played twice as fast an octave
higher, and so on.
m
Higher Faster
m
m
m
m
m
m
36The Program
pyr 1 m m pyr n m m
(rpt 2 . Trans 12 . Tempo 2 . pyr (n-1)) m tune
c 4 0.1 g 4 0.1 e 4
0.1 g 4 0.1 alldone pyr 4 (Tempo 0.3
tune)
37Gloveby Tom Makucevichwith help from Paul
HudakComposed using Haskore, a Haskell DSL,and
rendered using csound.
38Thank You!
Join us tomorrow to get some hands on
experience with tunes and toons. Help us build a
web page. Bring your creativity! Embarrass
your friends. Compose music. Play with
pictures. We will have a camera available