varargs.scm 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. ;;; GNU Mes --- Maxwell Equations of Software
  2. ;;; Copyright © 2016,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Mes.
  5. ;;;
  6. ;;; GNU Mes is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Mes is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Mes. If not, see <http://www.gnu.org/licenses/>
  18. ;; Setup output file
  19. (set-current-output-port (open-output-file "test/results/test017.answer"))
  20. ;; Flatten nested list example
  21. (define (flatten x)
  22. (cond
  23. ((null? x) '())
  24. ((not (list? x)) (list x))
  25. (#t (append (flatten (car x)) (flatten (cdr x))))))
  26. (display (flatten '((1) 2 ((3 4) 5) ((())) (((6))) 7 8 ())))
  27. (display #\newline)
  28. ;; Simple Vararg example
  29. (define (foo1 a)
  30. (cond
  31. ((null? a) 0)
  32. ((not (list? a)) (* a a))
  33. (#t (+ (foo1 (car a)) (foo1 (cdr a))))))
  34. (define (foo . a)
  35. (cond
  36. ((null? a) 0)
  37. (#t (foo1 a))))
  38. (display (foo))
  39. (display #\newline)
  40. (display (foo 4))
  41. (display #\newline)
  42. (display (foo 1 2 3 4 5))
  43. (display #\newline)
  44. (display (foo (flatten '((1) 2 ((3 4) 5) ((())) (((6))) 7 8 ()))))
  45. (display #\newline)
  46. (exit 0)