From 8cec548ea745ef8f6c2f69c75de189283e98f1d5 Mon Sep 17 00:00:00 2001 From: AlexKnauth Date: Thu, 25 Jan 2018 22:47:34 -0500 Subject: [PATCH 1/2] add and-let --- and-let.rkt | 13 +++++++++++++ main.rkt | 6 ++++-- scribblings/anaphoric.scrbl | 14 +++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 and-let.rkt diff --git a/and-let.rkt b/and-let.rkt new file mode 100644 index 0000000..4a07159 --- /dev/null +++ b/and-let.rkt @@ -0,0 +1,13 @@ +#lang racket/base + +(provide and-let) + +(require syntax/parse/define "if-let.rkt" (for-syntax racket/base)) + +(define-syntax and-let + (syntax-parser + [(_) #'#true] + [(_ body:expr) #'body] + [(_ [variable:id condition:expr] rest ...+) + #'(if-let [variable condition] (and-let rest ...) #false)])) + diff --git a/main.rkt b/main.rkt index 7299073..6ce9350 100644 --- a/main.rkt +++ b/main.rkt @@ -16,7 +16,8 @@ anaphoric/acond anaphoric/if-let anaphoric/when-let - anaphoric/cond-let)) + anaphoric/cond-let + anaphoric/and-let)) (require anaphoric/it anaphoric/aif @@ -24,4 +25,5 @@ anaphoric/acond anaphoric/if-let anaphoric/when-let - anaphoric/cond-let) + anaphoric/cond-let + anaphoric/and-let) diff --git a/scribblings/anaphoric.scrbl b/scribblings/anaphoric.scrbl index c1f62de..bbebf5d 100644 --- a/scribblings/anaphoric.scrbl +++ b/scribblings/anaphoric.scrbl @@ -122,4 +122,16 @@ using @racket[it]. Each @racket[conditionᵢ] is evaluated at most once (evaluation stops at the first successful - @racket[conditionᵢ]).} \ No newline at end of file + @racket[conditionᵢ]).} + +@defform[(and-let [identifier condition] ... body)]{ + Variant of @racket[and] which binds each @racket[identifier] + to the value of its @racket[condition], in scope within all + @racket[condition]s afterwards as well as in @racket[body]. + + Each @racket[condition] is evaluated at most once, and + evaluation stops at the first false condition. The + @racket[body] is only evaluated when all + @racket[conditions] are successful. +} + From 4bb11cb8f68f6307f1cbdb8578593a4733d53032 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Georges=20Dup=C3=A9ron?= Date: Sat, 27 Jan 2018 11:55:34 +0100 Subject: [PATCH 2/2] Add aand --- aand.rkt | 14 ++++++++++++++ and-let.rkt | 4 +++- main.rkt | 2 ++ scribblings/anaphoric.scrbl | 30 ++++++++++++++++++++++++------ 4 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 aand.rkt diff --git a/aand.rkt b/aand.rkt new file mode 100644 index 0000000..de3e3eb --- /dev/null +++ b/aand.rkt @@ -0,0 +1,14 @@ +#lang racket/base + +(provide aand it) +(require anaphoric/it + anaphoric/aif + syntax/parse/define + (for-syntax racket/base)) + +(define-syntax aand + (syntax-parser + [(_) #'#true] + [(_ body:expr) #'body] + [(_ [variable:id condition:expr] rest ...+) + #'(aif condition (and-let rest ...) #false)])) diff --git a/and-let.rkt b/and-let.rkt index 4a07159..28dfac8 100644 --- a/and-let.rkt +++ b/and-let.rkt @@ -2,7 +2,9 @@ (provide and-let) -(require syntax/parse/define "if-let.rkt" (for-syntax racket/base)) +(require syntax/parse/define + anaphoric/if-let + (for-syntax racket/base)) (define-syntax and-let (syntax-parser diff --git a/main.rkt b/main.rkt index 6ce9350..66dc60e 100644 --- a/main.rkt +++ b/main.rkt @@ -14,6 +14,7 @@ anaphoric/aif anaphoric/awhen anaphoric/acond + anaphoric/aand anaphoric/if-let anaphoric/when-let anaphoric/cond-let @@ -23,6 +24,7 @@ anaphoric/aif anaphoric/awhen anaphoric/acond + anaphoric/aand anaphoric/if-let anaphoric/when-let anaphoric/cond-let diff --git a/scribblings/anaphoric.scrbl b/scribblings/anaphoric.scrbl index bbebf5d..fdb847e 100644 --- a/scribblings/anaphoric.scrbl +++ b/scribblings/anaphoric.scrbl @@ -90,6 +90,24 @@ using @racket[it]. at most once (evaluation stops at the first successful @racket[conditionᵢ]).} +@defform[(aand conditionᵢ ... body)]{ + Variant of @racket[and] which binds @racket[it] + to the value of each @racket[conditionᵢ], in scope within the + next @racket[conditionᵢ] or @racket[body]. More precisely, the value + of each @racket[conditionᵢ] can be referred to as @racket[it] in + the following @racketvarfont{conditionᵢ₊₁}, and the value of the last + @racket[conditionᵢ] can be referred to as @racket[it] in the + @racket[body]. If there are no @racket[conditionᵢ], i.e. when + writing, @racket[(aand body)], then @racket[it] retains its original + binding (which means that @racket[it] could be unbound, e.g. if no + other @racketmodname[anaphoric] form wraps this one). + + Each @racket[condition] is evaluated at most once, and + evaluation stops at the first false condition. The + @racket[body] is only evaluated when every + @racket[conditionᵢ] is successful. +} + @section{The hygienic versions @racket[if-let], @racket[when-let] and @racket[cond-let]} @@ -124,14 +142,14 @@ using @racket[it]. (evaluation stops at the first successful @racket[conditionᵢ]).} -@defform[(and-let [identifier condition] ... body)]{ +@defform[(and-let [identifier conditionᵢ] ... body)]{ Variant of @racket[and] which binds each @racket[identifier] - to the value of its @racket[condition], in scope within all - @racket[condition]s afterwards as well as in @racket[body]. + to the value of its @racket[conditionᵢ], in scope within every + @racket[conditionᵢ] afterwards as well as in @racket[body]. - Each @racket[condition] is evaluated at most once, and + Each @racket[conditionᵢ] is evaluated at most once, and evaluation stops at the first false condition. The - @racket[body] is only evaluated when all - @racket[conditions] are successful. + @racket[body] is only evaluated when every + @racket[conditionᵢ] is successful. }