Giter Site home page Giter Site logo

match's Introduction

match

This is a pioneering work by Dan Friedman, Erik Hilsdale and Kent Dybvig that brings pattern matching to Scheme.

It runs amazingly, and I think learning how to use it is a must-have for all Scheme users.

Install:

raven install match

How to use:

First of all, you have to know (match) accept a list, and matching it with you set:

You can simply use it like this:

(match '(a 1 2)
    ((a ,x ,y) x))

(a ,x ,y) this phrase means, match the list with a symbol 'a, the car of list must be 'a not 'b or 'c.

you can write (1 ,x ,y) if you want to designation car to 1. or any number, or in place of cadr and caddr.

Just remenber if you use symbol or number or char, that fix there want has to be.

and the ,x ,y means it can be anything. you can call it as the last x in ((a ,x ,y) x), which means it back the value of x if match.

it can also accept ...

(match '(a 1 2 3)
    ((a ,x ...) `(,x ...)))

the ... means accept any number of x, the second mean return all what it got.

so it will return '(1 2 3) and '(1 2 3 4 5) for '(a 1 2 3 4 5).

and maybe you want the x must be symbol, then do this:

(match '(a 1 2)
    ((a ,x ,y) (guard (symbol? x)) y))

(guard) need a boolean, if true this phrase match, and false not. although the list is match, but (guard) have one vote veto power, like the five permanent member in the United Nations.

Notice that the atom in test of (guard) is NOT unquote. like (symbol? x), not (symbol? ,x).

The amazing thing is you can nesting the match: if we define:

(define Expl
    (lambda (x)
        (match x
            ((a ,x) x))))

then we can use it like this: ,(Expr -> e) so:

(match '(a (a 3) 2)
    ((a ,(Expl -> e) ,y)  e))

it will return 3.

,(Expr -> e) test the second element and it can call by ,e.

Notice that in ,(Expr -> e) the e is not unquote and has unquote when we call it.

For more information: Use Match

 Exp    ::= (match              Exp Clause)
         || (trace-match        Exp Clause)
         || (match+       (Id*) Exp Clause*)
         || (trace-match+ (Id*) Exp Clause*)
         || OtherSchemeExp

 Clause ::= (Pat Exp+) || (Pat (guard Exp*) Exp+)

 Pat    ::= (Pat ... . Pat)
         || (Pat . Pat)
         || ()
         || #(Pat* Pat ... Pat*)
         || #(Pat*)
         || ,Id
         || ,[Id*]
         || ,[Cata -> Id*]
         || Id

 Cata   ::= Exp

YOU'RE NOT ALLOWED TO REFER TO CATA VARS IN GUARDS. (reasonable!)

match's People

Contributors

guenchi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

match's Issues

错误使用了未定义的符号会卡死

-> 的时候要定义一个空的syntax,否则调用的时候会卡死。

我把这个符号全部改成 => 之后就不需要重新定义了,

而且还有个比较蛋疼的问题, 符号写错成其他符号例如 ==> ,又会卡死。
感觉应该要捕获这种异常才对。

(define sumsquares
    (lambda (ls)
      (define square
        (lambda (x)
          (* x x)))
      (match ls
        [(,[a*] ...) (apply + a*)]
        [,[square => n] n])))

(sumsquares '(1 2))

A question

Hi,
sorry for contacting you this way, but I had no other information how to reach you.

I am currently working on a SRFI document that compares some of the most popular pattern matchers.

I have slow Internet, so I didn't manage to download Chez Scheme on my machine, and the code doesn't seem to work with Guile or Racket.

Normally, I would try these myself, but given the circumstances I need to ask you for help.

In particular, I'd like to ask you whether there is a special meaning for the _ symbol in this pattern matcher, so for example, will the expression

(match '(a b c) (,_ _))

return the list (a b c) or would it rather return some error?

Likewise, I'd like to know if writing

(match '((a 1) (b 2) (c 3)) (((,x ,y) ...) `(,x ,y)))

would return the value ((a b c) (1 2 3)).

Thanks in advance!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.