Skip to content

Commit 659c8d2

Browse files
committed
add examples
1 parent e868945 commit 659c8d2

File tree

11 files changed

+271
-0
lines changed

11 files changed

+271
-0
lines changed

example

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
song = [ 1 2 | 3 ^7 ];

example1

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
key = #2;
2+
song = [ 1 2 3 4 | 5 6 7 ^1 ];

example2

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
tempo = 200;
2+
3+
song = [ _melody melody ^melody (^^1, 3)] ;
4+
5+
melody = [ 1 3 5 |];

example3

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
tempo = 300;
2+
song = [
3+
_5 3 2 1 | (mordent _5 4) | _5 3 2 1 | _(6, 4) |
4+
_6 4 3 2 | _(7, 3) 5 | 5 5 4 2 | (3, 4) |
5+
_5 3 2 1 | _(5, 4) | _5 3 2 1 | _(6, 4) |
6+
_6 4 3 2 | 5 5 (5, 2) | 6 5 4 2 | (1, 4) |
7+
3 3 (3, 2) | 3 3 (3, 2) | 3 5 1 2 | (3, 4) |
8+
4 4 (4, 2) | 4 3 (3, 2) | 3 2 2 1 | (2, 2) (5, 2) |
9+
3 3 (3, 2)| 3 3 (3, 2) | 3 5 1 2 | (3, 4) |
10+
4 4 (4, 2)| 4 3 (3, 2) | 5 5 4 2 | (1, 2) (chord 1 2) |
11+
] ;
12+
13+
/*
14+
The following chord got two parameters: p is a pitch variable and d is a duration variable. The four keys in this chord have the same duration d. The second pitch is 2 higher than p. For example, if p is 1, then the second pitch is 3. Likewise, the third pitch is 5 and the fourth pitch is ^1.
15+
*/
16+
17+
chord p d = {
18+
(p, d)
19+
(+ p 2, d)
20+
(+ p 4, d)
21+
(+ p 7, d) } ;
22+
23+
/*
24+
Suppose p is the middle C and d is a quarter notes. The corresponding representation for the above chord in staff notation is:
25+
26+
Similarly, the following chord got two parameters: p is a pitch variable and d is a duration variable. The keys in a mordent happen one after another. Each key could have different pitch and different duration. In the following example, the pitch for the first key and the third key are the same p while the second key has a pitch 1 higher than the others. When it comes to duration, the duration for the first key and for the second key are the same, namely a quarter of duration d. The duration for the third key is a half of duration d.
27+
*/
28+
29+
mordent p d = [ (p, / d 4) (+ p 1, / d 4) (p, / d 2) ];

example4

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
tempo = 300;
2+
song = { m1 (m2 a b c e) } ;
3+
4+
m1 = [
5+
_5 3 2 1 | (mordent _5 4 1) | _5 3 2 1 | (mordent _6 4 2) |
6+
_6 4 3 2 | (_7, 3) 5 | 5 5 4 2 | (3, 4) |
7+
_5 3 2 1 | (_5, 4) | _5 3 2 1 | (_6, 4) |
8+
_6 4 3 2 | 5 5 (5, 2) | 6 5 4 2 | (1, 4) |
9+
3 3 (3, 2) | 3 3 (3, 2) | 3 5 1 2 | (3, 4) |
10+
4 4 (4, 2) | 4 3 (3, 2) | 3 2 2 1 | (2, 2) (5, 2) |
11+
3 3 (3, 2)| 3 3 (3, 2) | 3 5 1 2 | (3, 4) |
12+
4 4 (4, 2)| 4 3 (3, 2) | 5 5 4 2 | (1, 2) (chord 1 2) |
13+
] ;
14+
15+
chord p d = {
16+
(p, d)
17+
(+ p 2, d)
18+
(+ p 4, d)
19+
(+ p 7, d) } ;
20+
21+
mordent p d v = [ (p, / d 8) (+ p v, / d 8) (p, * 3 (/ d 4)) ] ;
22+
23+
/*
24+
The following short melodies are used as parameter for the melody m2.
25+
*/
26+
27+
a = [_1 _3 __5 _3 |];
28+
b = [__6 _1 __4 _1 |];
29+
c = [ __7 _2 __5 _2 |];
30+
e = [ __7 _2 _2 _2 |];
31+
32+
/*
33+
The corresponding representation for the above short melodies in staff notation is:
34+
35+
36+
The following melody function takes in four short melodies as its parameters and uses them to build a longer melody.
37+
*/
38+
39+
m2 a b c d = [
40+
a a a b
41+
b c c a
42+
a a a b
43+
b c d 1 (1, / 1 2) (1, / 1 2) 1 1 |
44+
a a a a
45+
b a a e
46+
a a a a
47+
b a e (_1, 2) (1, 2) |
48+
];

example5

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
song = [
2+
/* every line of song is a randomly selected note and triad combination. After listening to the midi file produced, the composer could determine whether he or she likes it*/
3+
[(MajorTriad 1) (MinorTriad 3) (MinorTriad &5) (MajorTriad &7)]
4+
/*Noticing that (MajorTriad 1) is an application of function MajorTriad and it takes 1 as parameter of type note*/
5+
[(MajorTriad #1) (MajorTriad 4) (MinorTriad &6) (MinorTriad 6)]
6+
/*Noticing that # stands for sharp symbol, & stands for flat symbol*/
7+
];
8+
9+
/*The corresponding representation for the above song in staff notation is as followed: */
10+
11+
12+
/*Below implements the basic major triad and minor triad func needed*/
13+
/* raise is a built-in func that increase n semitone, which can be referred to later chapter for its implemetation*/
14+
MajorThird p = raise 4 p ; /* MajorThird returns a major third note over p*/
15+
MinorThird p = raise 3 p ; /* MajorThird returns a minor third note over p*/
16+
MajorTriad p = {
17+
p
18+
(MajorThird p)
19+
(MinorThird (MajorThird p))
20+
} ;
21+
22+
MinorTriad p = {
23+
p
24+
(MinorThird p)
25+
(MajorThird (MinorThird p))
26+
} ;
27+
AugmentedTriad p = {
28+
p
29+
(MajorThird p)
30+
(MajorThird (MajorThird p))
31+
} ;
32+
DimnishedChord p d = {
33+
p
34+
(MinorThird p)
35+
(MinorThird (MinorThird p))
36+
} ;
37+
38+
raise n p = if (< n 0)
39+
(lower (- 0 n) p)
40+
(if (== 0 n)
41+
p
42+
(raise (- n 1) #p)) ;
43+
44+
lower n p = if (< n 0)
45+
(raise (- 0 n) p)
46+
(if (== 0 n)
47+
p
48+
(lower (- n 1) &p)) ;

example6

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
song = { (timeBound 9 m1)
2+
(timeBound 9 m2) } ;
3+
4+
series = [ 1 2 3 4 5 6 7 ^1] ;
5+
6+
/*
7+
This is just a simple sequential list which we have seen before.
8+
*/
9+
10+
timeBound t xs =
11+
if (or (nil? xs)
12+
(< t (time (car xs))))
13+
[]
14+
(seq (car xs)
15+
(timeBound (- t (time (car xs))) (cdr xs))) ;
16+
17+
/*
18+
Here we define a function which take two argument, the first one t should be a number represent a duration bound, the second one is a sequencial list. The function returns a sequence which is the longest prefix of xs within the duration bound.
19+
20+
In above definition, time is a primitive function takes an arbitrary melody as argument and return its duration.
21+
*/
22+
23+
raise n p = if (< n 0)
24+
(lower (- 0 n) p)
25+
(if (== 0 n)
26+
p
27+
(raise (- n 1) #p)) ;
28+
29+
lower n p = if (< n 0)
30+
(raise (- 0 n) p)
31+
(if (== 0 n)
32+
p
33+
(lower (- n 1) &p)) ;
34+
35+
/*
36+
Here we define two function to repeatedly apply # or & operator to raise or lower the pitch of a melody.
37+
*/
38+
39+
m1 = seq (lower 2 series) #m1 ;
40+
m2 = seq (raise 2 series) &m2 ;
41+
42+
/*
43+
Here, we define m1 and m2 using function seq. seq is a function to construct a sequential list. seq would normally take 2 arguments. For example
44+
45+
seq 4 [3 4] -> [4 3 4]
46+
seq 3 [] -> [3]
47+
48+
In above definition, m1 and m1 is defined to be a sequential list where its first element is a given melody, its rest elements also form a sequential list. And this sequential list is defined to be #m1 or &m2.
49+
50+
As we can see, the length of melody is infinite. But it is ok in RYC since RYC will only compute the list elements just when they are needed. We just need to take only part of it as our final result song.
51+
*/

example7

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
song = f m [1 2];
2+
f fun arg = fun arg;
3+
m x = [1 2];

example8

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
song = car [1 a];
2+
a = [2 3];

example9

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
song = a 3 m;
2+
m = [1 2 3];
3+
a repeat melody = if (<= repeat 0)
4+
[]
5+
(seq melody (a (- repeat 1) melody));

queens

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* vim: set expandtab filetype=haskell : */
2+
3+
tempo = 400 ;
4+
5+
song = search-queen 1 ;
6+
7+
/*song = test [4 4] 5 ;*/
8+
9+
test xs x =
10+
if (nil? xs)
11+
true
12+
(let sadfsa = 1;
13+
in and (<> x (car xs))
14+
(test (cdr xs) x)) ;
15+
16+
test xs x =
17+
let go n rs = if (nil? rs)
18+
true
19+
(let asdfasd = 1 ;
20+
in and (<> x (car rs))
21+
(go (+ 1 n) (cdr rs))) ;
22+
in go 1 xs ;
23+
24+
search-queen size =
25+
let ns = genListFromToStep 1 size 1 ;
26+
go n xs = if (== n size)
27+
[xs]
28+
(concat (map (go (+ 1 n))
29+
(map (\x -> seq x xs)
30+
(filter (valid-queen? xs) ns)))) ;
31+
in go 0 [] ;
32+
33+
valid-queen? xs x =
34+
let go n rs = if (nil? rs)
35+
true
36+
(let r' = car rs ; rs' = cdr rs ;
37+
in and (<> x r')
38+
(and (<> x (+ r' n))
39+
(and (<> x (- r' n))
40+
(go (+ 1 n) rs')))) ;
41+
in go 1 xs ;
42+
43+
genListFromToStep from to step =
44+
let go n = if (> n to)
45+
[]
46+
(seq n (go (+ n step))) ;
47+
in go from ;
48+
49+
length xs = if (nil? xs)
50+
0
51+
(+ 1 (length (cdr xs))) ;
52+
53+
append = appendSeq ;
54+
appendSeq xs ys = if (nil? xs)
55+
ys
56+
(let x' = car xs ; xs' = cdr xs ;
57+
in seq x' (appendSeq xs' ys)) ;
58+
59+
concat = concatSeq ;
60+
concatSeq xs = if (nil? xs)
61+
[]
62+
(let x' = car xs ; xs' = cdr xs ;
63+
in appendSeq x' (concatSeq xs')) ;
64+
65+
map = mapSeq ;
66+
mapSeq f xs = if (nil? xs)
67+
[]
68+
(let x' = car xs ; xs' = cdr xs ;
69+
in seq (f x') (mapSeq f xs')) ;
70+
71+
filter = filterSeq ;
72+
filterSeq p xs = if (nil? xs)
73+
[]
74+
(let x' = car xs ; xs' = cdr xs ;
75+
in if (p x')
76+
(seq x' (filterSeq p xs'))
77+
(filterSeq p xs')) ;

0 commit comments

Comments
 (0)