func1.scm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. (define (hide x) (cdr (cons x x)))
  2. (define (f1 x y #!optional a b (c 8 c-supplied))
  3. (format #t "f1 x:~w y:~w a:~w b:~w c:~w c-supplied:~w~%"
  4. x y a b c c-supplied))
  5. (f1 3 4)
  6. ((hide f1) 3 4)
  7. ;; Output: f1 x:3 y:4 a:#f b:#f c:8 c-supplied:#f
  8. ;; Output: f1 x:3 y:4 a:#f b:#f c:8 c-supplied:#f
  9. (f1 8 7 6 5 4)
  10. ((hide f1) 8 7 6 5 4)
  11. ;; Output: f1 x:8 y:7 a:6 b:5 c:4 c-supplied:#t
  12. ;; Output: f1 x:8 y:7 a:6 b:5 c:4 c-supplied:#t
  13. (define (f2 x y #!optional a b (c -1 c-supplied) #!rest r)
  14. (format #t "f2 x:~w y:~w a:~w b:~w c:~w c-supplied:~w rr:~w~%"
  15. x y a b c c-supplied r))
  16. (f2 3 4)
  17. ((hide f2) 3 4)
  18. ;; Output: f2 x:3 y:4 a:#f b:#f c:-1 c-supplied:#f rr:()
  19. ;; Output: f2 x:3 y:4 a:#f b:#f c:-1 c-supplied:#f rr:()
  20. (f2 8 7 6 5 4 3 2 1)
  21. ((hide f2) 8 7 6 5 4 3 2 1)
  22. ;; Output: f2 x:8 y:7 a:6 b:5 c:4 c-supplied:#t rr:(3 2 1)
  23. ;; Output: f2 x:8 y:7 a:6 b:5 c:4 c-supplied:#t rr:(3 2 1)
  24. (define (f3 x y #!optional a b #!key k1 k2)
  25. (format #t "f3 x:~w y:~w a:~w b:~w k1:~w k2:~w~%"
  26. x y a b k1 k2))
  27. (f3 10 12 13)
  28. ;; Output: f3 x:10 y:12 a:13 b:#f k1:#f k2:#f
  29. (f3 10 12 13 14)
  30. ;; Output: f3 x:10 y:12 a:13 b:14 k1:#f k2:#f
  31. (f3 10 12 13 14 k2: 100)
  32. ;; Output: f3 x:10 y:12 a:13 b:14 k1:#f k2:100
  33. (define (f4 x y #!optional a b #!key k1 k2 #!rest r)
  34. (format #t "f4 x:~w y:~w a:~w b:~w k1:~w k2:~w r:~w~%"
  35. x y a b k1 k2 r))
  36. (f4 10 12 13)
  37. ;; Output: f4 x:10 y:12 a:13 b:#f k1:#f k2:#f r:()
  38. (f4 10 12 13 14 k1: 99)
  39. ;; Output: f4 x:10 y:12 a:13 b:14 k1:99 k2:#f r:()
  40. (f4 10 12 13 14 k2: 100 'a 'b 'c)
  41. ;; Output: f4 x:10 y:12 a:13 b:14 k1:#f k2:100 r:(a b c)
  42. (define (f5 x y #!optional a b #!rest r #!key k1 k2)
  43. (format #t "f5 x:~w y:~w a:~w b:~w k1:~w k2:~w r:~w~%"
  44. x y a b k1 k2 r))
  45. (f5 10 12 13)
  46. ;; Output: f5 x:10 y:12 a:13 b:#f k1:#f k2:#f r:()
  47. (f5 10 12 13 14 k1: 99)
  48. ;; Output: f5 x:10 y:12 a:13 b:14 k1:99 k2:#f r:(k1: 99)
  49. (f5 10 12 13 14 k2: 100 'a 'b 'c)
  50. ;; Output: f5 x:10 y:12 a:13 b:14 k1:#f k2:100 r:(k2: 100 a b c)
  51. (define (f6 x y #!optional a (b 100 b-supplied) #!rest r #!key k1 (k2 "2" k2-supplied))
  52. (format #t "f6 x:~w y:~w a:~w b:~w (supplied:~w) k1:~w k2:~w(supplied:~w) r:~w~%"
  53. x y a b b-supplied k1 k2 k2-supplied r))
  54. (f6 10 12 13)
  55. ;; Output: f6 x:10 y:12 a:13 b:100 (supplied:#f) k1:#f k2:"2"(supplied:#f) r:()
  56. (f6 10 12 13 14 k2: 100 'a 'b)
  57. ;; Output: f6 x:10 y:12 a:13 b:14 (supplied:#t) k1:#f k2:100(supplied:#t) r:(k2: 100 a b)
  58. (define (f7 #!optional (x 'a) (y 'b y-p) (z 'c z-p))
  59. (format #t "f7 x:~w y:~w y-p:~w z:~w z-p:~w~%" x y y-p z z-p))
  60. (f7 1 2)
  61. ;; Output: f7 x:1 y:2 y-p:#t z:c z-p:#f
  62. (f7)
  63. ;; Output: f7 x:a y:b y-p:#f z:c z-p:#f
  64. (define (f8 #!optional
  65. (x (begin (format #t "f8-default-x~%") 'a))
  66. (y (begin (format #t "f8-default-y~%") 'b))
  67. (z (begin (format #t "f8-default-z~%") 'c) z-p))
  68. (format #t "f8 x:~w y:~w z:~w z-p:~w~%" x y z z-p))
  69. (f8)
  70. ;; Output: f8-default-x
  71. ;; Output: f8-default-y
  72. ;; Output: f8-default-z
  73. ;; Output: f8 x:a y:b z:c z-p:#f
  74. (f8 1 2)
  75. ;; Output: f8-default-z
  76. ;; Output: f8 x:1 y:2 z:c z-p:#f
  77. ;; GitLab issue #30: Keyword arg with type
  78. (define (f11 #!key k ::boolean)
  79. (lambda () k))
  80. (format #t "f11 ~w~%" ((f11 k: #t)))
  81. ;; Output: f11 #t
  82. (define f12 (lambda (x ::boolean #!key y) (lambda () (list x y))))
  83. (format #t "f12 ~w~%" ((f12 #t y: 2)))
  84. ;; Output: f12 (#t 2)
  85. (define (f13 #!key k)
  86. (lambda () k))
  87. (format #t "f13 ~w~%" ((f13 k: 13)))
  88. ;; Output: f13 13
  89. (define (f14 x #!key (k ::boolean (not x)))
  90. (lambda () (list x k)))
  91. (format #t "f14 ~w~%" ((f14 3)))
  92. ;; Output: f14 (3 #f)