You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.1KB

  1. ;;; Module storing utility functions for common use.
  2. (module util (curry applied thunk just perhaps compose-symbols)
  3. (import scheme)
  4. (import chicken.base)
  5. (import srfi-13)
  6. ;; Partial application function for binary functions.
  7. (define (curry fn a)
  8. (lambda (b)
  9. (fn a b)))
  10. ;; Make a function applied, as per (applied +) = (lambda (ln) (+ (car ln) (cdr ln))).
  11. (define (applied fn)
  12. (curry apply fn))
  13. ;; Take a list of expressions and wrap them in a thunk.
  14. (define-syntax thunk
  15. (syntax-rules ()
  16. ((_ exp ...)
  17. (lambda () exp ...))))
  18. ;; Wrap a thunk in a function that discards all its arguments
  19. ;; and returns the result of evaluating the thunk.
  20. (define (just fn)
  21. (lambda args
  22. (fn)))
  23. ;; Haskell Maybe lifting pattern equivalent for booleans,
  24. ;; apply fn to arg if arg is not false.
  25. (define (perhaps fn arg)
  26. (if arg
  27. (fn arg)
  28. arg))
  29. ;; Compose the argument symbols into a new symbol, joined by '-'.
  30. ;; (compose-symbols 'apple 'pear 'orange) = 'apple-pear-orange
  31. (define (compose-symbols . ln)
  32. (string->symbol (string-concatenate (intersperse (map symbol->string ln) "-")))))