Re: [racket-users] Re: Andy Wingo's fold

2020-06-12 Thread Philip McGrath
On Fri, Jun 12, 2020 at 2:46 AM Catonano  wrote:

> the original paper Andy Wingo refers to uses Haskell to express this
> operator and I can't read Haskell and I'm not willing to learn
>

I'm confused about what you mean: in the version of "Applications of Fold
to XML Transformation", on Andy Wingo's blog
, all of the
examples are in Scheme. Here is a version of the example from the paper
that will run in Racket—most of the code is just copied and pasted from the
figures:

#lang racket

;; Source: https://wingolog.org/pub/fold-and-xml-transformation.pdf

(module+ test
  (require rackunit)
  (check-equal?
   (cartouche->svg
;; figure 16
'(cartouche (@ (line-color "red")
   (text-height 56))
(para "Warning: Smoking Kills")))
   ;; figure 17
   '(g (rect (@ (fill "none") (stroke "red")
(stroke-width "4")
(width "660") (height "120.0")
(x "0") (y "0")
(ry "20")))
   (text (@ (xml:space "preserve")
(font-size "56")
(font-family "Georgia")
(x "32")
(y "88"))
 (tspan (@ (x "32") (y "88"))
"Warning: Smoking Kills")

;;
-
;;
-

;; for Racket compatibility

(define (atom? v)
  (not (pair? v)))

(struct layout (x y)
  #:constructor-name make-layout
  #:transparent)

;; p. 7
;;   "Figure 20 uses without definition the macro let-params,
;;which binds lexical variables from the parameters list."
;; p. 6
;;   "... representing parameters as a list of association lists.
;;At each descent into a new SXML node, we cons the new parameters
;;onto the list. Lookup proceeds left-to-right in the parameters list,
;;stopping at the first alist in which a parameter is found."
(require syntax/parse/define)
(define-simple-macro (let-params params:expr (name:id ...)
 body:expr ...+)
  (let ([the-params params])
(let ([name (params-ref the-params 'name)]
  ...)
  body ...)))
(define (params-ref params name)
  (or (for*/first ([alist (in-list params)]
   [pr (in-list alist)]
   #:when (eq? name (car pr)))
(cadr pr))
  (raise-argument-error 'params-ref "no binding found for parameter"
"name" name
"params" params)))

;;
-
;;
-


;; figure 7 (part)
(define (assq-ref alist key default)
  (cond ((assq key alist) => cdr)
(else default)))

;; figure 11
(define (fold-values proc list . seeds)
  (if (null? list)
  (apply values seeds)
  (call-with-values
   (lambda ()
 (apply proc (car list) seeds))
   (lambda seeds
 (apply fold-values proc (cdr list)
seeds)

;; figure 12
(define (foldts*-values fdown fup fhere
tree . seeds)
  (if (atom? tree)
  (apply fhere tree seeds)
  (call-with-values
   (lambda () (apply fdown tree seeds))
   (lambda (tree . kseeds)
 (call-with-values
  (lambda ()
(apply fold-values
   (lambda (tree . seeds)
 (apply foldts*-values
fdown fup fhere
tree seeds))
   tree kseeds))
  (lambda kseeds
(apply fup tree
   (append seeds kseeds

;; figure 13, but with fdown replaced by figure 14
(define (post-order bindings tree)
  (define (err . args)
(error "no binding available" args))
  (define (fdown tree bindings pcont ret)
(let ((tail (assq-ref bindings (car tree)
  #f)))
  (cond
((not tail)
 (let ((default (assq-ref bindings
  '*default* err)))
   (values tree bindings default '(
((pair? tail)
 (let ((cont (cdr tail)))
   (case (car tail)
 ((*preorder*)
  (values '() bindings
  (lambda x (reverse x))
  (apply cont tree)))
 ((*macro*)
  (fdown (apply cont tree) bindings
 pcont ret))
 (else
  (let ((new-bindings (append (car tail)
  bindings)))
(values tree new-bindings cont
'()))
(else
 (values tree bindings tail '())
  (define (fup tree bindings cont ret
   kbindings kcont kret)
(values bindings cont
(cons (apply kcont (reverse kret))
  

Re: [racket-users] Re: Andy Wingo's fold

2020-06-12 Thread Catonano
Hi Stephen,

Il giorno lun 8 giu 2020 alle ore 16:34 Stephen De Gabrielle <
spdegabrie...@gmail.com> ha scritto:

> Hi Catonano
>
> Did you resolve this
>
> Kind regards
>
> Stephen
>
>
>

No, I didn't resolve this

the original paper Andy Wingo refers to uses Haskell to express this
operator and I can't read Haskell and I'm not willing to learn

My idea was to use some drawings of trees made in svg and maybe animations
of trees being processed with the original operator (the one expressed in
Haskell)

There's a scheme implementation, in my idea I would have traced it while
running and I would have prepared a graphical representation

In order to explain the operator to myself, before than to anyone else

But frankly it's a lot of work

If this thing is expressed in such a poor way, maybe its authors are not
interested in this idea being a thing

I account this to the cultural debt of the scheme community

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAJ98PDy1fUw%3Dba6FrpNukiOSMf8XFf_PEFt2hmik%3D%2Bvt9i2-mg%40mail.gmail.com.