forked from caiorss/Functional-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.org
More file actions
6907 lines (4555 loc) · 142 KB
/
Copy pathREADME.org
File metadata and controls
6907 lines (4555 loc) · 142 KB
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
#+INCLUDE: ../theme/style.org
#+TITLE: Scheme
* Scheme
** Overview
*** Why Lisp
- Lisp is used as embedded script language in many products
* Autocad -> Autolisp
* GNUCash -> GNU Guile Scheme
* GIMP Editor -> GNU Guile Scheme
Products that use lisp:
- Emacs -> Emacs Lisp, A lisp dialect based on Common Lisp
- GNU Maxima -> Cumputer Algebra System (CAS), Common Lisp
*** Scheme Features
- Learn the principles of programming languages
- Meta programming
- Lisp dialect that endorses Functional Programming
- Lightweight extension language or embedded language
- Scheme is used as extension language of softwares like GIMP and GnuCash
- Scheme can be used to test JVM and .NET API in the REPL.
* Google App Engine Uses Kawa scheme which is a implementations for the JVM.
*** Scheme Implementations
| Implementation | Feature |
|--------------------------+------------------------------------------------------------------------------|
| [[http://www.gnu.org/software/mit-scheme][MIT Scheme]] | Classical Scheme Implementation used by [[https://en.wikipedia.org/wiki/Structure_and_Interpretation_of_Computer_Programs][SCIP]] |
| [[http://www.gnu.org/software/kawa/][Kawa]] | Scheme for the JVM - Java API access. Compiles to the JVM |
| [[https://ironscheme.codeplex.com/][Iron Scheme]] | Scheme for .NET platform - .NET API Acess |
| [[http://www.gnu.org/software/guile/gnu-guile-projects.html#Applications][GNU Guile]] | Used as embedded extension language for many apps like GIMP, [[http://wiki.gnucash.org/wiki/Custom_Reports][GNUCash]], GEDA |
| [[http://www.call-cc.org/][Chicken]] | Compiles to Native Code, produces portable C code, package manager |
| [[http://www.lambdanative.org/][Gambit-C + Lambda Native]] | Compiles to IOS, Android, Linux, Windows ... |
| [[https://github.com/ashinn/chibi-scheme][Chibi Scheme]] | Minimal Scheme Implementation for use as an Extension Language |
| [[http://www.biwascheme.org/][BiwaScheme]] | Scheme implemented in Javascript. It can run in the browser, client side. |
| [[http://racket-lang.org/][Racket]] | IDE Dr Scheme. and Debugger. Superset of scheme, not fully compatible. |
| | |
| [[http://scsh.net/][Scsh]] | Scsh is an open-source Unix shell embedded within Scheme, running on all |
| | major Unix platforms including AIX, Cygwin, Linux, FreeBSD, GNU Hurd, HP-UX, |
| | Irix, Mac OS X, Solaris, and some others |
See also:
- [[http://community.schemewiki.org/?scheme-faq-standards#implementations][What Scheme implementations are there? ]]
- [[https://wingolog.org/archives/2013/01/07/an-opinionated-guide-to-scheme-implementations][An opinionated guide to scheme implementations]]
- [[http://www.phyast.pitt.edu/~micheles/scheme/scheme2.html][About Scheme implementations - The Adventures of a Pythonista in Schemeland]]
** Basic Syntax
*** Name Conventions
| Terminology | Description | Name Convention | Example |
|----------------|----------------------------------------|------------------|----------------|
| Procedure | functions | | |
| Predicate | Function that returns a boolean, true (#t) or false (#f) | Ends with ? | zero? null? |
| Getter | Function that returns a value from a Lisp object | Ends with -ref | list-ref |
| Setter | Function that sets a value in a Lisp object | Ends with ! | set! |
| Constructor | A constructor is a function that creates a Lisp object. | |
| Converter | Function that converts one type of Lisp object into another type | (from type)->(to-type) | symbol->string, number->string |
| Iteration (SCIP book) | Tail Recursion | |
*** Syntax
Scheme uses prefix notation, or polish notation. Every operator like +,-,* is a function.
*Function Application*
#+BEGIN_SRC
(<function> <arg1> <arg2> <arg3> ...)
;; 1 + 2 + 3 + 4 + 5
> (+ 1 2 3 4 5)
$1 = 15
;; 1 * 2 * 3 * 4 * 5
> (* 1 2 3 4 5)
$2 = 120
> (/ 1 2 3 4 5)
$3 = 1/120
> (sqrt 100)
$8 = 10
#+END_SRC
*Special Forms*
Special forms are statements that are not functions like if, then, else, begin, define ...
#+BEGIN_SRC
(<special form> <arg1> <arg2> <arg3> ...)
#+END_SRC
- define
Bind a name to a value or function (procedure).
#+BEGIN_SRC
(define <name> <value>)
(define (<function-name> arg1 arg2 ...) (<body>))
> (define x 10)
> x
$9 = 10
> (define sqrt2 (sqrt 2))
> sqrt2
$10 = 1.4142135623730951
> (define (f x y) (+ x y))
> f
$12 = #<procedure f (x y)>
> (f 3 4)
$11 = 7
> (define f (lambda (x y) (+ x y)))
> (f 3 4)
$54 = 7
#+END_SRC
- lambda
#+BEGIN_SRC scheme
(lambda (arg1 arg2 ...) (<body>))
> (lambda (x y) (+ x y))
$52 = #<procedure 9077370 at <current input>:463:0 (x y)>
> ((lambda (x y) (+ x y)) 3 4)
$53 = 7
#+END_SRC
- if then else
#+BEGIN_SRC
(if <condition> <then statement>)
(if <condition> <then statement> <else statement>)
> (if (zero? 4) "zero" "not zero")
$5 = "not zero"
> (if (zero? 4) "zero")
> (if (zero? 0) "zero")
$7 = "zero"
#+END_SRC
- cond
#+BEGIN_SRC
(cond
(<condition 1> <action1>)
(<condition 2> <action12)
...
(else <action else>)) ;; Optional
> (define x 10)
> x
$15 = 10
> (cond
((zero? x) "is zero")
((< x 3) "Less than 3")
((< x 5) "Less than 5")
((< x 9) "Less than 9")
(else "Greater or equal 9"))
$16 = "Greater or equal 9"
;;; Returns nothing
;;
(define x 100)
> (cond
((zero? x) "is zero")
((< x 3) "Less than 3")
((< x 5) "Less than 5")
((< x 9) "Less than 9"))
>
#+END_SRC
- begin
The begin statement is used to execute multiple s-expressions.
#+BEGIN_SRC
(begin <action1> <action2> ...)
> (begin
(display 10)
(display "dummy")
(newline))
10dummy
#+END_SRC
- quote or (') tick.
Don't evaluate a lisp expression, returns the list of symbols, numbers and atoms that forms the list, or in other words, any lisp expression is just a list.
#+BEGIN_SRC scheme
(quote <expression>) Or '(<epxression>)
> '(exp 3.0)
$11 = (exp 3.0)
> (quote (exp 3.0))
$12 = (exp 3.0)
;;; GNU Guile
> (eval '(exp 3.0) (interaction-environment))
$15 = 20.08553692318766
;;; (eval (quote (exp 3.0)) (interaction-environment))
$16 = 20.085536923187668
> '(1 2 3 4 5)
$13 = (1 2 3 4 5)
> (quote (1 2 3 4 5))
$14 = (1 2 3 4 5)
;;; However not all list can be evaluated
> (eval '(1 2 3 4 5) (interaction-environment))
ERROR: In procedure 1:
ERROR: Wrong type to apply: 1
#+END_SRC
*Important Functions*
- load
Load a scheme source code in the REPL.
#+BEGIN_SRC
(load "<filename.scm>")
(load "tools.scm")
#+END_SRC
- eval
Eval evaluates a quoted lis expression, its arguments depends on the scheme implementation. It is better to use macros rather than eval. It is necessary to remember that "eval is evil" since it can allow untrusted code execute arbitrary commands so it must be used with care.
Note: In some scheme implementations [] square brackets can be used instead of parenthesis to make the code more readable.
#+BEGIN_SRC scheme
;; (eval <s-expression> <argument>)
;;;; MIT - Scheme
;;
;; $ rlwrap -c --remember scheme
1 ]=> (define s '((lambda (x y) (+ (* 2 x) (* 3 y))) 3 4))
;Value: s
1 ]=> s
;Value 17: ((lambda (x y) (+ (* 2 x) (* 3 y))) 3 4
1 ]=> (eval s (the-environment))
;Value: 18
;;;; GNU Guile
;;
;; $ rlwrap -c --remember guile
> [define s '([lambda (x y) (+ (* 2 x) (* 3 y))] 3 4)]
scheme@(guile-user)>
> s
$1 = ((lambda (x y) (+ (* 2 x) (* 3 y))) 3 4)
> (eval s (interaction-environment))
$2 = 18
;;;; Chicken Scheme
;;
;; $ rlwrap -c --remember csi
> [define s '([lambda (x y) (+ (* 2 x) (* 3 y))] 3 4)]
> s
((lambda (x y) (+ (* 2 x) (* 3 y))) 3 4)
>
> (eval s)
18
;;;; Kawa Scheme
;;
;; $ rlwrap -c --remember java -jar kawa-2.0.jar
#|kawa:6|# (define s '((lambda (x y) (+ (* 2 x) (* 3 y))) 3 4))
#|kawa:7|# s
((lambda (x y) (+ (* 2 x) (* 3 y))) 3 4)
#|kawa:8|#
#|kawa:10|# (eval s)
18
#+END_SRC
*** Data Types
**** Basic Data Types
*Booleans*
#+BEGIN_SRC scheme
;;; Boolean
;;
;;---------------------------------
1 ]=> #f
;Value: #f
1 ]=> #t
;Value: #t
#+END_SRC
*Number*
#+BEGIN_SRC scheme
1 ]=> 12323
;Value: 12323
1 ]=> 123.232
;Value: 123.232
;;;;; Complex Number
1 ]=> 10+4i
;Value: 10+4i
1 ]=> (+ 10+4i 5-3i)
;Value: 15+i
1 ]=> (+ 10+4i 100)
;Value: 110+4i
1 ]=> (sqrt 10+4i)
;Value: 3.2226021794715067+.6206164734636876i
#+END_SRC
*String*
#+BEGIN_SRC scheme
1 ]=> "hello world scheme"
;Value 15: "hello world scheme"
#+END_SRC
*Characters*
#+BEGIN_SRC scheme
> #\a
$93 = #\a
> #\A
$94 = #\A
scheme@(guile-user) [20]>
> #\(
$95 = #\(
scheme@(guile-user) [20]>
> #\)
$96 = #\)
scheme@(guile-user) [20]>
> #\space
$97 = #\space
scheme@(guile-user) [20]>
> #\tab
$98 = #\tab
scheme@(guile-user) [20]>
> #\return
$99 = #\return
scheme@(guile-user) [20]>
#+END_SRC
*Symbol*
#+BEGIN_SRC
1 ]=> 'mysymbol
;Value: mysymbol
1 ]=> (quote mysymbol)
;Value: mysymbol
#+END_SRC
*List*
Scheme lists are linked lists with sequential access to any element.
#+BEGIN_SRC scheme
1 ]=> '(23.23 1000 40 50 102)
;Value 16: (23.23 1000 40 50 102
;; List of Strings
;;
;;---------------------------------
1 ]=> '( "hello" "world" "scheme")
;Value 17: ("hello" "world" "scheme")
;; List of Symbols
;;
;;---------------------------------
1 ]=> '(hello world scheme)
;Value 18: (hello world scheme)
1 ]=> (quote (hello world symbols))
;Value 31: (hello world symbols)
;; S-expression
;;---------------------------------
1 ]=> '(+ 10 2)
;Value 19: (+ 10 2)
1 ]=> (quote (+ (sin 0.4) (cos 0.01)))
;Value 32: (+ (sin .4) (cos .01))
1 ]=> (quote (1 2 3 4 5 6))
;Value 33: (1 2 3 4 5 6)
#+END_SRC
*Vector*
Vectors are equivalent to C-arrays, are linear data structures of fixed size with random access to any element.
#+BEGIN_SRC scheme
1 ]=> #(a b c d e)
;Value 11: #(a b c d e)
1 ]=> (vector-ref #(a b c d e) 0)
;Value: a
1 ]=> (vector-ref #(a b c d e) 4)
;Value: e
#+END_SRC
**** Type Predicates
Scheme is dynamic typed language therefore there is not guarantee about the variable type or the function type signature. The types can be checked with the following predicates.
| Predicate | Returns true for |
|------------|----------------------------------------------|
| boolean? | Boolean |
| string? | Strings |
| number? | Number, integer, real or complex numbers |
| integer? | Integer numbers |
| real? | Real numbers 2.232 1e3 100 |
| complex? | Complex numbers 100+45i |
| symbol? | Symbols |
| list? | Lists |
| vector? | Vectors |
| procedure? | Procedure or function |
#+BEGIN_SRC scheme
(boolean? #f)
;Value: #t
1 ]=> (boolean? 100)
;Value: #f
1 ]=> (symbol? "x")
;Value: #f
1 ]=> (symbol? 'x)
;Value: #t
1 ]=> (integer? 100)
;Value: #t
1 ]=> (integer? 3.232)
;Value: #f
1 ]=> (real? 3232)
;Value: #t
1 ]=> (real? 3232.232)
;Value: #t
1 ]=> (string? "hello world Scheme Lisp")
;Value: #t
1 ]=> (string? 100232)
;Value: #f
1 ]=> (list? '(1 2 3 5 6))
;Value: #t
1 ]=> (list? 2323)
;Value: #f
1 ]=> (procedure? sin)
;Value: #t
1 ]=> (procedure? 2323)
;Value: #f
(define atom?
(lambda (x)
(and (not (pair? x)) (not (null? x)))))
#+END_SRC
**** Type Conversion
#+BEGIN_SRC scheme
;;-------------------------------------;;
1 ]=> (string->number "20e3")
;Value: 20000.
1 ]=> (string->number "10.23")
;Value: 10.23
1 ]=>
1 ]=> (number->string 100)
;Value 11: "100"
1 ]=> (number->string -100.23e3)
;Value 12: "-100230."
1 ]=>
;;-------------------------------------;;
1 ]=> (symbol->string 'sin)
;Value 13: "sin"
1 ]=> (string->symbol "my-symbol")
;Value: my-symbol
1 ]=> (symbol->string 'some-symbol)
;Value 14: "some-symbol"
;;-------------------------------------;;
#+END_SRC
*** Variables
**** Global Variable
#+BEGIN_SRC scheme
1 ]=> (define g 9.81)
;Value: g
1 ]=> g
;Value: 9.81
1 ]=> (define (speed v0 t) (+ (* g t) v0))
;Value: speed
1 ]=> (speed 3 2.4)
;Value: 26.544
#+END_SRC
**** Local Variable
***** let
#+BEGIN_SRC scheme
(let
(
(x 10)
(y 20)
(f (lambda (a) (* a 10)))
)
(f (+ x y))
)
;Value: 300
1 ]=> x
;Unbound variable: x
1 ]=> y
;Unbound variable: y
1 ]=> f
;Unbound variable: f
#+END_SRC
***** let*
The keyword let* is equivalent to a nested let.
#+BEGIN_SRC scheme
$ rlwrap -c --remember csi
CHICKEN
(c) 2008-2015, The CHICKEN Team
(c) 2000-2007, Felix L. Winkelmann
Version 4.10.0 (rev b259631)
linux-unix-gnu-x86 [ manyargs dload ptables ]
compiled 2015-08-04 on yves.more-magic.net (Linux)
;;;; This code won't work.
>
(let
(
(x 10)
(y (+ x 40))
(z (* y x))
(f (lambda (a) (* a 3)))
)
(f (+ x y z))
)
Error: unbound variable: x
;;; This code will, however there is a lot of boilerplate nesteds let
> (let ((x 10))
(let ((y (+ x 40)))
(let (
(z (* y x))
(f (lambda (a) (* a 3))))
(f (+ x y z)))))
1680
>
;;;; let*
> (let*
(
(x 10)
(y (+ x 40))
(z (* y x))
(f (lambda (a) (* a 3)))
)
(f (+ x y z))
)
1680
#+END_SRC
***** letrec
Letrec allows to implement loops or loops with recursive anonymous functions.
Example1:
#+BEGIN_SRC scheme
(define (displayln x)
(begin
(display x)
(display "\n")))
(letrec
[(loop (lambda (i)
(if (< i 10)
(begin
(displayln i)
(loop (+ i 1))))))]
(loop 0))
0
1
2
3
4
5
6
7
8
9
> loop
Error: unbound variable: loop
#+END_SRC
Example2: Loop over a list and find the sum of all elements:
#+BEGIN_SRC scheme
(define alist '(1 2 3 4 5 6))
(letrec
[(loop (lambda (xs)
(if (null? xs)
0
(+ (car xs) (loop (cdr xs))))))]
(loop alist))
21
#+END_SRC
*** Functions
**** Defining and applying functions
In Scheme functions are first class, they can be passed as arguments
to other functions and be returned from another functions, in other
words, functions are data.
#+BEGIN_SRC scheme
]=> (define (f x) (* x 10))
;Value: f
]=> f
;Value 11: #[compound-procedure 11 f]
]=> (f 10)
;Value: 100
;; Map a function over a list
;;;;
]=> (map f '(1 2 3 4 5 6))
;Value 12: (10 20 30 40 50 60)
;; Define a function of multiple variables
;;;;;
]=> (define (fxy x y) (+ (* 4 x) (* 3 y)))
;Value: fxy
]=> (fxy 3 5)
;Value: 27
;; Map a fucntion multiple variables over alist
;;;;
]=> (map (lambda (y) (fxy 3 y)) '(1 2 3 4 5 6))
;Value 14: (15 18 21 24 27 30)
;; Apply a list as function argument
;;;;;
1 ]=> (apply fxy '( 5 6))
;Value: 38
;; Transforms a function f into a new function that accepts
;; a list of arguments
;;
;;;;;;;
1 ]=> (define (currify f) (lambda (x) (apply f x)))
;Value: currify
1 ]=> (define fxy_c (currify fxy))
;Value: fxy_c
1 ]=> (fxy_c '( 3 4))
;Value: 24
1 ]=> (fxy_c '( 5 6))
;Value: 38
1 ]=>
1 ]=> (map fxy_c (list '(5 6) '(3 7) '(8 9) '( 1 5)))
;Value 29: (38 33 59 19)
;;; OR
1 ]=> (map (currify fxy) (list '(5 6) '(3 7) '(8 9) '( 1 5)))
;Value 30: (38 33 59 19)
#+END_SRC
**** Anonymous Functions/ Lambda Functions
Anonymous functions are useful to pass functions as arguments to other
functions, callbacks and connect one function to another.
#+BEGIN_SRC scheme
1 ]=> (lambda (x) (+ (* x 4) 10))
;Value 31: #[compound-procedure 31]
1 ]=> ((lambda (x) (+ (* x 4) 10)) 10)
;Value: 50
1 ]=> (map (lambda (x) (+ (* x 4) 10)) '(10 20 30 40 50))
;Value 32: (50 90 130 170 210)
1 ]=> (define f (lambda (x) (+ (* x 4) 10)))
;Value: f
1 ]=> f
;Value 33: #[compound-procedure 33 f]
1 ]=> (map f '(10 20 30 40 50))
;Value 34: (50 90 130 170 210)
;; Scheme is a Functional Programming Language,
;; so it can return functions from functions that
;; can be used to define curried functions
;;
1 ]=> (define (addxy x y) (lambda (x) (lambda (y) (+ x y))))
;Value: addxy
1 ]=> ((addxy 10) 20)
;Value: 30
1 ]=> (define add10 (addxy 10))
;Value: add10
1 ]=> (add10 20)
;Value: 30
1 ]=>
1 ]=> (map (addxy 10) '(10 20 30 40 50 60))
;Value 37: (20 30 40 50 60 70)
#+END_SRC
**** Functions with Control Structure
#+BEGIN_SRC scheme
(define (sign x)
(cond
((> x 0) 1)
((= x 0) 0)
((< x 0) -1)
))
1 ]=> (sign -10)
$49 = -1
1 ]=> (sign 100)
$50 = 1
1 ]=> (sign 0)
$51 = 0
(define (absolute x)
(cond
((>= x 0) x)
((< x 0) (- x))
))
1 ]=> (absolute -10)
$52 = 10
1 ]=> (absolute 10)
$53 = 10
1 ]=> (absolute 0)
$54 = 0
(define (absolute2 x)
(cond
((> x 0) x )
(else (- x))
))
1 ]=> (map absolute2 '(-10 -9 0 1 2 3))
$57 = (10 9 0 1 2 3)
#+END_SRC
**** Variadic Function
Function of many arguments
#+BEGIN_SRC scheme
1 ]=> (define (variadic-fun . args) args)
1 ]=> (variadic-fun 10 20 30 40 50 100)
$55 = (10 20 30 40 50 100)
(define (variadic2 . args)
(- (apply * args) (apply + args)))
;; (- (* 10 20 30) (+ 10 20 30))
;; (- 6000 60)
;; 5940
;;
1 ]=> (variadic2 10 20 30)
$56 = 5940
#+END_SRC
**** Recursive Functions