Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pattern matching #42

Open
jcubic opened this issue Aug 10, 2021 · 1 comment
Open

pattern matching #42

jcubic opened this issue Aug 10, 2021 · 1 comment

Comments

@jcubic
Copy link
Contributor

jcubic commented Aug 10, 2021

There is SRFI-200 (Draft) for pattern matching but I have my own implementation, that I've created as an example for my Scheme implementation:

Here is the whole code, we can use this in cookbook, the code is not that long, but unfortunately, it uses lisp-macros I'm not sure if you can rewrite it with syntax-rules (or syntax-case)

;; Example pattern matching macro
;;
;; This file is part of the LIPS - Scheme implementation in JavaScript
;; Copyriht (C) 2019-2021 Jakub T. Jankiewicz <https://jcubic.pl/me>
;; Released under MIT license

(cond-expand
 (lips)
 (guile
  (define (object? x) #f)
  (define (type x)
          (cond ((string? x) "string")
                ((pair? x) "pair")
                ((null? x) "nil")
                ((number? x) "number")
                ((vector? x) "array")
                ((procedure? x) "function")
                ((char? x) "character")))))

;; ---------------------------------------------------------------------------------------
(define (compare a b)
  "(compare a b)

   Function that compare two values. it compare lists and any element of the list
   can be a function that will be called with other value. e.g.:
   (compare (list (list 'a) 'b) (list pair? 'b))"
  (cond ((and (pair? a) (pair? b))
         (and (compare (car a) (car b))
              (compare (cdr a) (cdr b))))
        ((and (vector? a) (vector? b))
         (compare (vector->list a) (vector->list b)))
        ((and (object? a) (object? b))
         (compare (vector->list (--> Object (keys a)))
                  (vector->list (--> Object (keys b)))))
        ((string=? (type a) (type b)) (eq? a b))
        ((and (procedure? a) (not (procedure? b)))
         (a b))
        ((and (not (procedure? a)) (procedure? b))
         (b a))
        (else #f)))

;; ---------------------------------------------------------------------------------------
(define-macro (auto-quote arg)
  "(auto-quote list)

   Macro that create list recursively but take symbols from scope"
  (if (pair? arg)
      `(list ,@(map (lambda (item)
                      (if (symbol? item)
                          item
                          (if (pair? item)
                              `(auto-quote ,item)
                              `,item)))
                    arg))
      arg))

;; ---------------------------------------------------------------------------------------
(define-macro (match-pattern expr . list)
  "(match-pattern ((pattern . body) ...))

   Pattern matching macro. examples:
   (match-pattern (1 (pair? pair?) 2) ((1 ((1) (1)) 2) (display \"match\")))
   ;; match
   (match-pattern (1 (pair? pair?) 2) ((1 ((1)) 2) (display \"match\")))
   (match-pattern (1 (pair? pair?) 2) ((1 ((1)) 2) (display \"match\")) (true \"rest\"))
   ;; rest"
 (if (pair? list)
     (let ((ex-name (gensym)))
       `(let ((,ex-name (auto-quote ,expr)))
          (cond ,@(map (lambda (item)
                         (if (eq? (car item) #t)
                             `(else ,(cadr item))
                             `((compare ,ex-name (auto-quote ,(car item))) ,@(cdr item))))
                       list))))))

;; ---------------------------------------------------------------------------------------
(match-pattern (1 (pair? pair?) 2) ((1 ((1) (1)) 2)
                                    (display "match")
                                    (newline)))

The license is MIT but I'm fine in releasing it for scheme cookbook in multiple licenses.

@lassik what do you think? Can we put lisp macros into cookbook or should we do only pure R7RS compatible code?

@lassik
Copy link
Member

lassik commented Aug 10, 2021

Looks great! Let's include it.

IMHO we should favor hygienic macros like syntax-rules and syntax-case, since those are one of the unique features of Scheme and the RnRS standards don't have unhygienic macro systems. But in practice, many schemers also use define-macro, so that's fine. It sounds reasonable to post define-macro code, and rewrite it hygienically later if we have time.

This example should be possible to write using syntax-rules by using Oleg's symbol?? hack, but I'm not completely sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants