Skip to content

Instantly share code, notes, and snippets.

@ruliana
Last active June 14, 2023 07:42
Show Gist options
  • Save ruliana/421cf4119016276718c9293188f548d2 to your computer and use it in GitHub Desktop.
Save ruliana/421cf4119016276718c9293188f548d2 to your computer and use it in GitHub Desktop.
Sprinkling some syntax sugar on Guile Scheme to make it more pratical. Let's say "better defaults" for my personal taste :)
(use-modules (srfi srfi-1) ;; list operations
(srfi srfi-11) ;; let-values
(srfi srfi-41) ;; streams
(srfi srfi-42) ;; list comprehension
(srfi srfi-88) ;; keyword as "this:", not only "#:this"
(oop goops) ;; function overloading ("OOP" is _not_ the main point)
((ice-9 format) #:prefix fmt.))
(define-syntax def (identifier-syntax define-method))
;; Sane "let"
(define-syntax var
(syntax-rules (<=)
[(_) #f]
[(_ variable <= value rest ...)
(let ([variable value]) (var rest ...))]
[(_ v1 v2 <= value rest ...)
(let-values ([(v1 v2) (destructure '(v1 v2) value)]) (var rest ...))]
[(_ body ...) (begin body ...)]))
(define-generic destructure)
(def (destructure args (lst <list>))
(let ([size (length args)])
(apply values (take lst size))))
(define-generic empty?)
(def (empty? lst)
(null? lst))
(def (empty? lst <list>)
(null? lst))
(define-generic length)
(def (length (lst <list>))
(next-method))
(def (length (str <string>))
(string-length str))
(define-generic format)
(def (format (pattern <string>) . args)
(apply fmt.format #f pattern args))
(define-generic printf)
(def (printf thing)
(printf "~a" thing))
(def (printf (pattern <string>) . args)
(var pat <= (string-append pattern "\n")
(apply fmt.format #t pat args)))
(var str <= "test"
lst <= '(t e s t)
(printf (length str))
(printf (length lst)))
(var first second <= '(1 2 9)
third <= 3
(printf "~a ~a ~a" third second first))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment