Use of macros in scheme

Source: Internet
Author: User

#lang scheme

(Define-syntax My-when
(Syntax-rules ()
[(_ Pred body ...)
(if pred (begin body ...) (void)) ] ) )

(My-when
(= 2 1)
(Display 1)
(Display 2))

(Define-syntax My-let
(Syntax-rules ()
[(My-let ([var exp] ...)
Body ...)
((Lambda (var ...)
Body ...) Exp ...) ] ) )

(My-let
([a 1]
[B 2])
(Display a)
(Display B))

(Define-syntax my-let*
(Syntax-rules ()
[(my-let* ([var exp]) body ...)
(My-let ([var exp]) body ...) ]
[(my-let* ([var exp]
[Var1 EXP1] ...) Body ...)
(My-let ([var exp])
(My-let* ([var1 exp1] ...)
Body ...) ) ] ) )

(Define-syntax my-for
(Syntax-rules ()
[(My-for (var from to) body ...)
(Let loops ([var from])
(My-when
(< var to)
Body ...
(Loop (+ var 1))) ) ] ) )

(my-for
(I 0 10)
(display "a")
(display "B"))


;;; A more interesting usage.

(Define-syntax Loop
(Syntax-rules (begin end from to)
[Loop <var> from <min> to <max> start body ... end]
(Let Loop1 ([<var> <min>])
(Cond
[(< <var> <max>) body ... (LOOP1 (+ 1 <var>))]
[Else (void)]) ) ] ) )

(Loop i from 1 to 10
Begin
(Display 1)
(newline)
(Display 2)
(newline)
End)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use of macros in scheme

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.