PasteRack.org
Paste # 51997
2020-02-15 11:24:57

Fork as a new paste.

Paste viewed 226 times.


Embed:

N-times demonstration

#lang racket/base
 
(define (make-true-when-called-N-times n)
  (define counter 1)
  (define (predicate?)
    (begin0 (= counter n)
      (set! counter (add1 counter))))
  predicate?)

(require (for-syntax racket/base))

(define-syntax (log-with-name stx)
  (syntax-case stx ()
    [(_ id ...)
     #`(begin
         (printf "The value of '~a' is: ~a\n" (syntax->datum #'id) id)
         ...)]))

(define should-print (make-true-when-called-N-times 5))

(define (multiply n1 n2)
  (when (should-print)
    (log-with-name n1 n2))
  (* n1 n2))

(for ([n1 (in-range 10 16)]
      [n2 (in-naturals 20)]
      [n3 (in-naturals 1)])
  (printf "Iteration no: ~a\n" n3)
  (multiply n1 n2))