-
Notifications
You must be signed in to change notification settings - Fork 6
/
DevNotes.txt
2480 lines (1724 loc) · 79.1 KB
/
DevNotes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////
// Dev Notes
//////////////////////////////
//////////////////////////////
// Known Bugs
//////////////////////////////
//////////////////////////////
// Multiple let declarations
// Dec 14, 2013
Multi-line compiling with let declarations doesn't parse. For example compiling these two together:
let one = 1
let two = 2
-- Recursion tests. These all exhaust memory with the current setup
let fib n = if n == 0 then 0 else if n == 1 then 1 else (fib (n - 1)) + (fib (n - 2))
fib 9
let fac n = if n == 0 then 1 else n * fac (n - 1)
fac 100
let sums x y = if y > 0 then (sums (x + 1) (y - 1)) else if y < 0 then (sums (x - 1) (y + 1)) else x
sums 20 100000
//////////////////////
// Patterns
//////////////////////
"p" functions are pattern functions. These functions return lambdas that when given a beat will return a value.
let s = pseq [pseq [0,pseq [0.5, 666]], pseq [1,10], pseq [2,20]]
foldl (\acc t -> print $ s::p t) 0 [0..12]
let sh = prand [pseq [555,666,777], 1, 2]
foldl (\acc t -> print $ sh::p t) 0 [0..12]
let ws = pwrand [0.5, 0.25, 0.25] [pseq [555,666,777], 1, 2]
foldl (\acc t -> print $ ws::p t) 0 [0..12]
let ps = pshuf [0..4]
foldl (\acc t -> print $ ps::p t) 0 [0..12]
let st = pstutter 3 $ pseq [0..5]
foldl (\acc t -> print $ st::p t) 0 [0..12]
let pw = pwrap 3 6 (pseq [0..7])
foldl (\acc t -> print $ pw::p t) 0 [0..12]
let ps = pseries 2 5
foldl (\acc t -> print $ ps::p t) 0 [0..12]
let ps = pwrap 0 13 $ pseries 0 2
foldl (\acc t -> print $ ps::p t) 0 [0..12]
let pg = pgeom 1 2
foldl (\acc t -> print $ pg::p t) 0 [0..12]
let pg = pwrap 0 13 $ pgeom 1 2
foldl (\acc t -> print $ pg::p t) 0 [0..12]
let pr = preverse $ pseq [0..4]
foldl (\acc t -> print $ pr::p t) 0 [0..12]
treverse pr
-- nested patterns
let pgs = pseq [pgeom 1 2, 0.666]
foldl (\acc t -> print $ pgs t) 0 [0..12]
-- pwarp and bwarp
let si freq => sin freq >> perc 0 0.3 0.15
siner ~> si (pwarp -1 $ pseq [444,555,666,777]) -- acts like reverse
stop siner
let si freq => sin freq >> perc 0 0.3 0.15
siner ~> si (pwarp (1/3) $ pseq [444,555,666,777]) -- acts like pstutter 3
stop siner
let si freq => sin freq >> perc 0 0.3 0.15
siner ~> si (pwarp (pseq [13.5,pseq [-5.3, 1]]) $ pseq [444,555,666,777]) -- you can use patterns for the warp amount
stop siner
let si2 => sin (twarp (1/3) (pseq [444,555,666,777])) >> perc 0 0.3 0.2 -- used as a "t" pattern in a synthdef
siner2 +> si2 si2 [si2 si2] si2 -- interlocking pattern between rhythmand t pattern
let s => sin 440 >> perc 0 0.3 0.3
let s2 => sin 666 >> perc 0 0.3 0.3
let s3 => sin 777 >> perc 0 0.3 0.3
siner2 +> (pseq [s2,s3,s]) (pwarp (pseq [(1/3),(1/4)]) $ pseq [s,s3,s2]) | (/2)
stop siner2
context::currentTime -- get the current time of the audio context
currentBeat 0 -- get the current beat, given the global tempo
-- "b" functions are mirrored after their "p" pattern counter parts, but are based on the current global beat
bseq [pseq [666,777,888],1,2] -- evaluate multiple times to see the different values
bshuf [0..5]
bstutter 3 (pseq [0..4])
bwrap 1 7 $ pseries 1 5
-- This is really useful for synths, especially if you want to use a synth in an impulse stream
sin (bstutter 2 $ pshuf [555, 666, 777]) >> perc 0 0.3 0.3 >> play
sin (twrap 440 777 $ pseries 440 88) >> perc 0 0.3 0.3 >> play
-- we want to declare the shuffled pattern ahead of time otherwise it will get generated anew each time. You can re-evaluate to generate a new seq.
let shuffled = pseq [preverse ps, ps] where ps = pshuf [3..8]
let si => degree2Freq slendro (mul (bseq [1,2,3,2]) $ bstutter 2 shuffled) >> sin >> perc 0 0.2 0.25
siner +> si
siner2 +> si [_ si] [_ si] si | (*3)
stop siner
stop siner2
let si freq => sin freq >> perc 0 0.3 0.3
siner ~> si (pseq [444,555,666,777]) 999
stop siner
let s => sin 440 >> perc 0 0.3 0.3
let s2 => sin 666 >> perc 0 0.3 0.3
let s3 => sin 777 >> perc 0 0.3 0.3
siner2 +> (pseq [s2,s3,s]) (pseq [s,s3,s2,s]) | (/2)
stop siner2
-- pbind is similar to the SuperCollider Pbind.
-- It takes a pattern name (used to store in a global dict), a func (can be a pattern), an array of arguments (each can be a pattern), and a duration (can be pattern)
-- pbind name func arguments duration
let t f q => sin (f / q) >> pan -1 >> perc 0 0.5 0.3
let u f q => sin (f * q) >> pan 1 >> perc 0 0.5 0.3
let testp = pbind "test" (pseq [t,u,t,t,u,u,u]) [pshuf [666,777,888,999], pseq [1,1/2,2]] (pshuf [1/2,1/2,1,1/2,1/2])
-- You can get the current value of a pbind pattern using myPbind::value.
testp::value -- updates with the pattern. Be careful, this is a mutating value!! IMPURE!
-- Solo Streams can now have modifiers for rhythm.
let sa freq => saw (freq + (sin 3 >> gain 5)) >> add (saw (freq/2)) >> lowpass (freq * 2) 5 >> perc 0.0 0.2 (tseq [0.3,1])
sawer ~> sa 1 [2 2] 3 4 [3 3] | (*444) (*555) (*333) | _ _ (*2) (*2)
-- | UGen like patterns!
-- EXECUTE THIS FIRST
let s freq => sin freq >> perc 0 0.3 0.3
-- psin
siner ~> s (psin 0.05) | (\x -> degree2Freq bartok (x * 5 + 10))
let s freq => sin freq >> perc 0 0.3 0.3
siner ~> s (psin $ pseq [0.2,0.05]) | (\x -> degree2Freq bartok (x * 5 + 10))
-- psaw
siner ~> s (psaw 0.2) | (\x -> degree2Freq bartok (x * 5 + 10))
-- ptri
siner ~> s (ptri 0.2) | (\x -> degree2Freq bartok (x * 5 + 10))
-- psquare
siner ~> s (psquare 0.2) | (\x -> degree2Freq bartok (x * 5 + 10))
-- pselect
siner ~> s (pselect (prand [0,1]) [pseq [444,555,666],pseq [888,777,666]])
-- pclip
siner ~> s (pclip 500 600 (pseq [300,325..800]))
-- pfold
siner ~> s (pfold 500 600 (pseq [300,325..800]))
-- pmul
siner ~> s (pmul (pseq [440,666,777]) (pseq [1..4]))
-- pdiv
siner ~> s (pdiv (pseq [440,666,777]) (pseq [1..4]))
-- padd
siner ~> s (padd (pseq [440,666,777]) (pseq [0,111]))
-- pminus
siner ~> s (pminus (pseq [440,666,777]) (pseq [0,111]))
-- pmod
siner ~> s (pmod (pseq [3440,3666,3777]) (pseq [777,888]))
-- ppow
siner ~> s (ppow (pseq [2,3]) (pseq [5..9]))
-- pdelay, delay times can be negative!
siner ~> s (pdelay (pmul 50 $ psin (1/6)) $ pseq [444,555,666,777])
-- pcomb, delay times can be negative!
siner ~> s (pcomb (pmul 50 $ psin (1/6)) $ pseq [444,555,666,777])
-- pcrush, bit crush a pattern
siner ~> s (pcrush (psaw (1/3)) $ psin (1/5)) | (\x -> degree2Freq bartok (x * 5 + 10))
-- ptcrush, bit crush the time sequence of a pattern
siner ~> s (ptcrush 0.05 $ psin (1/5)) | (\x -> degree2Freq bartok (x * 5 + 10))
-- pdust
siner ~> s (pdust 0.5) | (\x -> degree2Freq bartok (x * 5 + 10))
-- pfir
siner ~> s (pfir [1,-0.651,1.7] $ psaw 0.2) | (\x -> degree2Freq bartok (x * 5 + 10))
-- prange, to be used with -1 to 1 patterns, namely psin, psaw, ptri, psquare, and pdust
siner ~> s (psaw 0.1 >> prange 400 4000)
-- pexprange, to be used with -1 to 1 patterns, namely psin, psaw, ptri, psquare, and pdust
siner ~> s (psaw 0.1 >> pexprange 400 4000)
-- Binary operators and patterns
let si freq => sin freq >> perc 0 0.3 0.15
siner ~> si (pseq [444,555,666,777] + pseq [111,222,333])
siner ~> si (pseq [444,555,666,777] - pseq [111,222,333])
siner ~> si (pseq [444,555,666,777] * pseq [1,2,3])
siner ~> si (pseq [444,555,666,777] / pseq [1,2,3])
siner ~> si (pseq [444,555,666,777] % pseq [555,666,777])
siner ~> si (pseq [15,16,17] ^ pseq [2,3])
siner ~> si (pseq [444,555,666,777] .>> pseq [1,2,3])
siner ~> si (pseq [444,555,666,777] .<< pseq [1,2,3])
siner ~> si (pseq [444,555,666,777] .| pseq [200,500,1000])
siner ~> si (pseq [444,555,666,777] .& pseq [11111,22222,33333])
siner ~> si (pseq [444,555,666,777] .^ pseq [200,512,1000])
-- Patterns as modifiers (Play these together as a mini piece) ---------------------------------------------------------------
let bd => square 78.487 >> perc 0 0.3 0.1
let sn => white 1 >> perc 0 0.3 0.1
let hh => violet 1 >> bandpass 1569.75339 100 >> perc 0 15 0.125
hat +> hh | (*2)
drums +> bd sn bd [sn sn] | (pwarp (ptri 0.1) $ pseq [(*1),(*1),(/2),(/2)])
let m freq => saw freq + saw (freq/(choose [2,4,8])) >> perc 0 0.2 (exprandom 0.075 1)
mel ~> m 1 2 3 4 5 6 | (degree2Freq kumoi) | (pwarp (ptri 0.1) $ pseq [(*1),(*1),(/2),(/2)])
let m2 freq => saw (freq/2) + saw (freq/(choose [1,8])) >> perc 0 0.2 (exprandom 0.1 0.5)
mel2 ~> m2 (pseq [7,7,5,6,6] >> pstutter 14) | (degree2Freq kumoi) | (pwarp (ptri 0.1) $ pseq [(*1),(*1),(*1.5),(/2),(/2)])
------------------------------------------------------------------------------------------------------------------------------
/////////////////////
// State Monad
/////////////////////
let testdo = do
x <- get
mprint x -- mprint is a monadic print that allows for easy printing with do syntax
put (x + 1)
y <- get
mprint y
mprint "Hello, world."
result (y + 1)
runState testdo 0
evalState testdo 8
execState testdo 0
runState (result 5 >>= \x -> mprint x >>= \_ -> mprint "Hello, world.") 0
let m = (result 5 >>= \x -> mprint x >>= \x -> result x)
runState m 99
runState (m >>= \_ -> result 10) 0
runState (mprint 5 >>= \x -> result x) 0
-- lift
let testdo = do
x <- get
lift play (sin x >> perc 0 0.7 2)
put (x + 13)
y <- get
lift play (sin y >> perc 0 0.7 2)
result (y + 1)
let m = 440
let m = evalState testdo m -- Continually rises on execution
-- Stack example (borrowed from Learn You a Haskell)
let pop = State (\(x:xs) -> Result x xs)
let push a = State (\xs -> Result Nothing (a:xs))
let stackManip = do
push 3
pop
pop
runState stackManip [5,8,2,1]
let stackStuff = do
a <- pop
if a == 5
then push 5
else do
push 3
push 8
runState stackStuff [9,0,2,1,0]
//////////////////////////////////////////
// Pattern matching
data Monster { says = "howl" }
data Species = Hydra | Gorgon | Unspeakable | MindFlay
let m = Monster
let n = Monster "slither"
let h = Hydra
let g = Gorgon
let pattern p = case p of
Nothing -> "Nothing to see here."
1 -> "One is the loneliest number."
666 -> "Hail satan."
3.14 -> "Dessert."
(Monster says) -> "Monster says " + says
"string" -> "This is not."
["String",66,(\_->)] -> "That's a really weird list."
[x,y,z] -> "x: " + x + ", y: " + y + ", z: " + z
(x:xs) -> "x: " + x + ", xs: " + (show xs)
Hydra -> "Cut off all the heads!"
Gorgon -> "Shield your eyes!"
(\_ _ _ ->) -> "lambda x y x -> " + (show $ p 6 6 6)
_ -> "Anything goes."
pattern n
pattern (\y r p -> y + r + p)
let threeD x y z = [x,y,z]
pattern threeD
pattern ["String",66,(\x -> x + 1)]
data Coordinate = North | East | South | West
let c = East
case c of
North -> "North!"
East -> "East!"
South -> "South!"
West -> "West!"
let whereFunc x = z x
where
z (x:xs) = replicate (length xs) x
whereFunc (2:[1..4])
Pattern matching function arguments
let myFunc (x:xs) [] Nothing _ = x * 2
myFunc [1] -- Curries to (\[] Nothing _ ->)
myFunc [1] [] -- (\Nothing _ ->)
myFunc [1] [] Nothing -- (\_ ->)
myFunc [1] [] Nothing "Anything" -- x * 2 = 1
myFunc [1] [1] Nothing "Anything" -- Won't match, second arg needs to be []
myFunc [] [] Nothing "Anything" -- Won't match first arg needs at least a head for x:xs matching
let myFunc 1 = "ONE"
myFunc 1
myFunc 2 -- error
let myFunc (x:xs) = replicate (length xs) x
myFunc [7..1] -- [7,7,7,7,7,7]
myFunc [7] -- [], xs has a zero length, so no replication
myFunc [] -- error, requires at least a head
-- lambdas can match numbers of arguments
let myFunc (\_ _ ->) = print "Lambda with 2 arguments"
myFunc (\x y -> x + y)
myFunc (\x -> x) -- Error, requires a function with 2 arguments
let myFunc [] = Nothing
myFunc [] -- Nothing
myFunc 1 -- Error
data Monster { mash = "The monster mash." }
let m = Monster
m :: mash
let myFunc (Monster ms) = "What are we going to do? " + ms
myFunc m
let n = Monster "Kill all reptilians."
myFunc n
myFunc 1 -- Error, requires a monster
-- Won't work, requires TCO.
let sums x y = if y > 0 then (sums (x + 1) (y - 1)) else if y < 0 then (sums (x - 1) (y + 1)) else x
sums 20 100000
// List comprehensions work! They're a little bit different than haskell in that the filters are collected up front and called on each item. Examples:
[x | x <- [1..9]] -- returns [1,2,3,4,5,6,7,8,9]
[[x,y] | x <- [1..9], y <- [1,2]] -- returns [[1,1],[1,2],[2,1],[2,2],[3,1],[3,2],[4,1],[4,2],[5,1],[5,2],[6,1],[6,2],[7,1],[7,2],[8,1],[8,2],[9,1],[9,2]]
[x * y| x <- [1..9], y <- [2..5]] -- returns [2,3,4,5,4,6,8,10,6,9,12,15,8,12,16,20,10,15,20,25,12,18,24,30,14,21,28,35,16,24,32,40,18,27,36,45]
[x * y | x <- [1..30], y <- [88,77,44], (x % 5) == 0, odd y] -- returns [385,770,1155,1540,1925,2310]
let z = let x = 10 in x + 1 -- returns 11
let q = x / pi -- returns 10.822536130248883
where
x = let y = 33 in y + 1
cave = bear ? otherBear -- Checks if Bear is nothing, returns if not, otherwise returns otherBear
// More prelude functions!
sum, take, drop, length, null,maximum, minimum, product, elem, replicate, slice
// $ function application and composition works!
mul 3 $ mul 3 $ div 2 $ add 3 $ 1 -- returns 4.5
(+) 2 . (/3) $ 3 + 4 -- returns 4.333333333333334
(+) 2 . (/3) . (^) 3 $ 3 + 4 -- returns 731
map ((+) 2 . (/3) . (^) 3) [1..9] -- returns 3,5,11,29,83,245,731,2189,6563
map ((+) 2 . (/) 3) [1..9] -- returns 5,3.5,3,2.75,2.6,2.5,2.4285714285714284,2.375,2.3333333333333335
[5.0,3.5,3.0,2.75,2.6,2.5,2.4285714285714284,2.375,2.3333333333333335]
[5.0,3.5,3.0,2.75,2.6,2.5,2.4285714285714284,2.375,2.3333333333333335]
// These now work!
foldl, foldr, zip, zipWith, filter, head, tail, init, and last
// List creation syntax
[1,2,3,4] -- 1,2,3,4
[1..7] -- 1,2,3,4,5,6,7
[2,5..99] -- 2,5,8,11,14,17,20,23,26,29,32,35,38,41,44,47,50,53,56,59,62,65,68,71,74,77,80,83,86,89,92,95,98
// Dictionaries have been implemented as such
// Dictionary creation
("fives" = 555)
(someVar = 555, "A String" = [1,2,3], 1.5 = "Three")
let doom = ("Doom" = 3)
// Dictionary look up uses the !! operator or the lookup function.
doom !! "Doom"
lookup "Doom" doom -- Note that the dictionary is the last argument for the lookup function
// Insertion uses cons, and essentially merges two dictionaries returning a new one, with the left hand overwriting the right.
let gloom = ("Gloom" = "doom") : doom
let doom2 = ("Doom3" = 2, "Liar" = "Face") : doom
// You can also use the insert function
insert ("frogs" = [0,1,2]) doom
doom2 !! "Doom" -- returns 3
lookup "Doom3" doom2 -- returns 2
doom2 !! "Liar" -- returns "Face"
//////////////////////////////////////////
// Case Statements
examples:
-- Returns (\x y -> x + y)
case False of
"String" -> True
20.1 -> "Good jarb"
30 -> one
False -> (\x y -> x + y)
-- Returns one
case 30 of
"String" -> True
20.1 -> "Good jarb"
30 -> one
False -> (\x y -> x + y)
-- Returns Nothing
case 0.5 of
"String" -> True
20.1 -> "Good jarb"
30 -> one
False -> (\x y -> x + y)
-- Returns "YOYOYO"
case "Zombie" of
"String" -> True
20.1 -> "Good jarb"
30 -> one
False -> (\x y -> x + y)
_ -> "YOYOYO"
//////////////////////
// Data
//////////////////////
-- data declaration using record like syntax
data MyData {
thisThing = 1,
two = 2,
three = 44
}
-- data instantiation, less than the total number of members in the constructor will return with default value
MyData 0.1 0.5 0.2 -- returns MyData 1 2 44
MyData 0.1 0.5 -- returns MyData 0.1 0.5 44
MyData 666 -- returns MyData 666 2 44
let myCoolObject = MyData -- returns MyData 1 2 44
-- lookup is similar to namespace resolution in C++.
myCoolObject :: thisThing -- returns 1
myCoolObject :: two -- returns 2
myCoolObject :: three -- returns 44
data MyData { one = 1, two = 2, three = 3 }
let m = MyData 0.1 0.2 0.3
let m2 = m { two = 66 } -- returns 0.1 666 0.3
//////////////////
// Guard Patterns
// Dec 14, 2013
Is this any useful with Case statements the way they are?
Need to add syntax for guard patterns, for example:
myGuard x
| x > 4 = 99
| otherwise = 100
-- Test code for pattern matching with prelude functions
let actors = mapSpawn (add 2) [1..9]
let zipActors = zipSpawn [(add 2), (div 3), (sqrt)] [1..3]
zip [(add 2), (div 3), (sqrt)] [1..3]
map (\[x,y] -> show [x,y]) $ zip [(add 2), (div 3), (sqrt)] [1..3]
(\[x,y] -> x + y) [1,2]
filter (\[x,y] -> y > 5) $ zip [1..5] [3,7,9,5,2]
map (\[x,y] -> show [x * 2,y]) ("Doom" = [1,2], "Satan" = [3,"Sandwich"])
foldl (\acc [x,y] -> acc ++ [x,y]) [] $ zip [66..99] [1..9]
foldr (\[x,y] acc -> acc : [x,y]) [] $ zip [66..99] [1..9]
foldl (\acc x -> x : acc) [] [1..9]
foldr (\x acc -> acc ++ x) [] [1..9]
zipFlat [1..99] [33,-16..-100]
zipWith (\x [y,z] -> [x ^ y, x ^ z]) [1..9] $ zip [33,-22..-1000] [1..9]
zipWith (add) [1..9] [3..5]
filter (\[x,y] -> y /= Nothing) $ zip [1..9] (zipFlat [33..1] $ replicate 5 Nothing)
filter (\[x,y] -> x < 3) ("Doom" => [1,2], "Satan" => [3,"Sandwich"])
let addOneSwitch = (\[x,y] -> [y,x]) . (\x -> [x + 1, x])
addOneSwitch
data MyType { d = 4 }
let makeData x = MyType x
makeData 1
let myLoop m =
receive
"test" -> self $ m{d = m::d + 1}
"finished" -> m::d
let newLoop = spawn myLoop [MyType 5]
"test" :>> newLoop
"finished" :>> newLoop
let myLoopTest m = case m of
MyType -> m{d = m::d + 1}
"finished" -> m::d
let m = MyType
m { d = m::d + 1 }
myLoopTest m
map (/=3) ("Doom" = "kj", "Doom2" = 2, "Doom3" = 3)
let hydra 666 = 6666
hydra 1
let hydra [x,y,z] = x + y + z
let hydra (x:xs) = x * 3
let hydra Nothing = 666
let res = hydra Nothing
-- Lexical Scoping Tests
let pi = 3.141592654
let pi_holder = 5 -- should be overwritten inside createArea
let createArea = area
where
pi_holder = pi -- The value we want to hold true in the createArea closure scope
area r = pi_holder * r * r
let area = createArea -- redefinition of area but at global scope. Should not effect area from inside createArea
area 10 -- 314.1592653589793
pi_holder -- Should still be 5, because pi_holder in createArea doesn't change the global scope pi_holder
let pi_holder = 3 -- This should have no effect on createArea's evaluation
area 10 -- Succes! Still 314.1592653589793
-- Pattern matching tests
let headTail (x:xs) = xs
headTail [1,5..99]
let listMatch list@[1,Nothing,z] = list
listMatch [1,Nothing,3]
let lambdaMatch lamb@(\_ _ _ ->) = lamb 6 6 6
lambdaMatch (\x y z -> x + y + z)
lambdaMatch 1
(\(x:xs) y z -> x + y + z) [1,2] 2 3
let add3 x y z = x + y + z
add3
add3 1
add3 1 2 3
let add31 = add3 1
lambdaMatch (add3)
add31 2 3
let one = 1
-- Higher order function tests
let flip f a b = f b a
flip div 2 3
flip subtract 2 3
let combo = add 3 . div 4
combo 6
add 3 . div 4 $ 6
let one 1 = "ONEONEONE"
one 1
let one1 = 1
one one1
one (0.5 + 0.5)
let double x = x * 2
map (double) [1..9]
let listf [(x:xs),y,z] = x + y + z
listf [[3,7,88],7,9]
let whereFunc x = z [x, x * 2]
where
z (x:_) = x * 6
[1..9]
whereFunc 6
let adds = map (add 1) [1..9]
(adds !! 2)
1 + 2
let three = add 1 2
three + 1
let addList = [add 1 2, add 2, add 3, add 4]
([add 1 2, add 666] !! 1) 3
let add2 = (addList !! 1)
add2 1
let three = (addList !! 3) 0
three
let addDict = ("ADD3" = add 3, "ADD12" = add 1 2)
(addDict !! "ADD3") 1
add 1 2
let six = add3 1 2 3
six + 1
let add3 x y z = x + y + z
let add31 = add3 1
add31 2 3
add31
1 + 2
let addList = [add 1 2, add 2, add 3, add 4]
([add 1 2, add 666] !! 1) 2
[1..9] !! 3
-- Higher order function tests
let flip f a b = f b a
flip div 2 3
flip subtract 2 3
let one 1 = "ONEONEONE"
one 1
let one1 = 1
one one1
one (0.5 + 0.5)
let double x = x * 2
map (double) [1..9]
let listf [(x:xs),y,z] = x + y + z
listf [[3,7,88],7,9]
let whereFunc x = z [x, x * 2]
where
z (x:_) = x * 6
[1..9]
whereFunc 6
let adds = map (add) [1..9]
(adds !! 2) (-1)
let double x = x * 2
double
double 10
add
add 2
lookup 1
North /= West
zipFlat [1..9] [27,53..1000]
flip subtract 2 3
clientName
even 10
odd (-4)
[[x,y] | x <- [1..50], y <- [1..90]]
[x | x <- [1..10], even x]
[x | x <- [1..9]] -- returns [1,2,3,4,5,6,7,8,9]
[[x,y] | x <- [1..9], y <- [1,2]] -- returns [[1,1],[1,2],[2,1],[2,2],[3,1],[3,2],[4,1],[4,2],[5,1],[5,2],[6,1],[6,2],[7,1],[7,2],[8,1],[8,2],[9,1],[9,2]]
[x * y| x <- [1..9], y <- [2..25]] -- returns [2,3,4,5,4,6,8,10,6,9,12,15,8,12,16,20,10,15,20,25,12,18,24,30,14,21,28,35,16,24,32,40,18,27,36,45]
[x * y | x <- [1..30], y <- [88,77,44], (x % 5) == 0, odd y] -- returns [385,770,1155,1540,1925,2310]
(3<=)
2 - (-1)
2 : [3]
add (-1) 2
subtract
map (subtract) [1..9]
minus
minus 3 1
subtract 3 1
add x y = x + y
(+1) . (3/) . (subtract 1) . (1-) $ 5
let x = 11
let y = 22
4 << 3
add 2 3 >> div 2
3 >> add 2
add 2 $ 3
add 2 << 3
let one = 6
2 + (-one)
(5 >>) (div 3)
map (subtract 2) [1..9]
5 >> div 3 >> add 2 >> mul 6
5 >> (/3) >> (+2) >> (*6) >> (3+) >> (66-)
((3 / 5) + 2) * 6
mul 6 << add 2 << div 3 << 5
5 >> (div 3) >> (add 2) >> mul 6 >> (/3) >> (*2) >> (3/)
add 2 . div 3 $ 5
map (5 >>) $ map (div) [1..9]
map (5 >>) $ map (/) [1..9]
add 2 << (div 3 << 5)
5 >> div 3 >> add 2
(5 >>) (div 3)
5 >> div 3
spawn add [2,3]
spawn (add 2) [3]
spawn (\x y -> x / y * 666) [1,2]
data MyType{d = 4}
let myLoop m =
receive
"test" -> myLoop $ m{d = m::d + 1}
"finished" -> m::d
let newLoop = spawn #Test2 myLoop [MyType 5]
"test" :>> newLoop
"finished" :>> newLoop
let newLoop = spawn #Test myLoop [MyType 5]
"test" :>> #Test
"finished" :>> #Test
let makeData x = MyType x
makeData 1
let m = MyType
let n = m { d = m::d + 1 }
m
n
data Monster { size = 6 }
let makeMonster x = Monster x
let monsterFactory maker =
receive
[x] -> maker x
let monsterActor = spawn #Monster (monsterFactory) [makeMonster]
[666] :>> monsterActor
map (map (/3)) [[x,y] | x <- [1..9], y <- [1..9], odd x, even y]
map (map (/3)) ("Doom" => [1..9], "Three" => [1..3])
replicate 3 $ replicate 3 $ replicate 3 0
deepMap (2+) $ replicateN [2,3,1,2] [1..9]
deepMap (2+) [1,2,[3],4,[[5]]]
print 1
2 + 2
spawn #print (\x -> x myName) [print]
let double x = x * 2
let printAndDouble x = do
print x
double x
let printAndTriple x = do print x; print( x * 2); x * 3
printAndDouble 20
printAndTriple 10
-- Mountains of Madness testing
compile "MountainsOfMadness"
let actor = spawn #Madness madnessLoop [demoGame]
(Walk north "casiosk1") :>> actor
(Look north "casiosk1") :>> actor
Finish :>> actor
(Walk north "casiosk1") :>> #Madness
(Look north "casiosk1") :>> #Madness
Finish :>> #Madness
(findSomethingWithName "casiosk1") demoGame
chat ";lkwedlkwwf;lwkewe;ldkwd;lwkfwf;lkwef;lwkf"
clientName
1 + 1
random 1 7
spawn #ChatActor (chat) ["Hello, world."]
spawn #ChatActor (actorChat) ["Hello, world." + (random 0 1)]
importjs "../Pieces/MountainsOfMadnessPanel.js"
updateNarration "Yo!"
spawn #ChatActor (updateNarration) ["Hello, world."]
updateNarration
-- Guards are in
let myGuard (x:xs)
| x > 4 = 99
| _ = 100
myGuard [1..9]
let whereGuard (x:xs) = y x
where
y z
| z > 10 = "> 10 > 10 > 10"
| otherwise = " < 10 < 10 < 10"
whereGuard [1..99]
whereGuard [11..99]
let whereGuard2 = y
where
y z
| z > 10 = "> 10 > 10 > 10"
| otherwise = " < 10 < 10 < 10"
whereGuard2 1
whereGuard2 11
let fib n
| n == 0 = 0
| n == 1 = 1
| otherwise = fib (n - 1) + fib (n - 2)
fib 8
let fib n = case n of
0 -> 0
1 -> 1
_ -> fib (n - 1) + fib (n - 2)
setTempo 60
setTempo 300
let tempoSetter x = do setTempo x; sleep 0.1; tempoSetter $ random 60 1000
tempoSetter 60
{-
Sequencing Ideas!!!!!
-}
-- Percussion sequencing is similar to tidal, but _ represents a rest, space seperates identifiers. We use | like in list comprehensions, but here they're for tempo multipliers and offsets
pattern +> bass _
pattern2 +> _ snare
pattern3 +> _ hi
pattern4 +> down down down _ down [_ _ down] | (*4)
pattern +> a | (*2) (\x -> 1 / x) (*3) (/4)
-- We can predefine percussive patterns without assignment using lambda style syntax (+>). This will create a new pattern without autoplaying for storage.
(+> a b c)
(+> a b [hi hi [_ low low hi]] | (+4))
-- Melodies use a similar scheme. Spaces separate numbers. Wild cards are rests. We use | like list comprehension but for offsets/multipliers which themselves can be sequenced with _ being default
octoSynth ~> octopus 1 _ 2 _ 3 _ _ 4 5 _ 6 _ -1 _ 3 _ -66 -99 99 | (4-) (sqrt) _ _ _
-- Lambda style melody pattern
(~> synth1 1 2 3)
(~> synth2 1 2 3 44 2 4 _ _ _ 1 _ (-4) 9 1 23 | (* 3) (/4) _ (+1) )
(~> octopus 1 _ 2 _ 3 _ _ 4 5 _ 6 _ -1 _ 3 _ -66 -99 99 | (4-))
-- We can pass any function expecting one argument, including lambdas
(~> bass 1 _ 2 _ 3 _ _ 4 5 _ 6 _ -1 _ 3 _ -66 -99 99 | (sqrt) _ (\x -> if odd x then x + 1 else x))
-- We can also use lists like in Perc Sequencing to get tuples
(~> noise 1 2 [3 4 5] 1 [[1 2] 3])
let s = (sin 440) >> gain 0.5 >> play
stop s
let s = sin 3 >> gain 33 >> sin >> gain 666 >> sin >> gain 0.3 >> play
s >> stop
let s2 = sin 0.5 >> gain 66 >> sin >> gain 600 >> sin >> gain 0.3 >> play
s2 >> stop
let s3 = square 7 >> gain 66 >> square >> gain 33 >> square >> gain 0.2 >> play
s3 >> stop
let s4 = tri 2 >> gain 4 >> tri >> gain 99 >> square >> gain 0.2 >> play
s4 >> stop
let s5 = tri 66 >> gain 3 >> tri >> gain 14 >> square >> gain 0.3 >> play
s5 >> stop
let s = play (tri 440 >> gain 0.2)
stop s