Skip to content

Instantly share code, notes, and snippets.

@soegaard
Created July 8, 2015 16:38
Show Gist options
  • Save soegaard/cdc8cfa6a57f6ce3881d to your computer and use it in GitHub Desktop.
Save soegaard/cdc8cfa6a57f6ce3881d to your computer and use it in GitHub Desktop.
Does this construct have a name?
> (define (make-map+fold f)
; f : α β -> (values α β)
; map f over xs while threading the seconding value
(define (f* xs ρ)
(match xs
['() (values '() ρ)]
[(cons x xs) (letv ((x ρ) (f x ρ))
(letv ((xs ρ) (f* xs ρ))
(values (cons x xs) ρ)))]))
f*)
> (define sqr+count (make-map+fold (λ (x n) (values (sqr x) (add1 n)))))
> (sqr+count '(3 7 2) 0)
'(9 49 4)
3
@soegaard
Copy link
Author

soegaard commented Jul 8, 2015

Forgot the definition of letv

(define-syntax (letv stx)
  ; syntax: (letv ((x ...) e) b ...)
  ;   bind the result of e to the variables x ... in the body b ...
  (syntax-parse stx
     [(_letv ((x:id ...) e:expr) b:expr ...)
     (syntax/loc stx
       (let-values ([(x ...) e]) b ...))]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment