[Scheme-reports] the humble pk Andy Wingo 19 Sep 2011 19:30 UTC

Hello all,

I apologize that this is not relevant to Scheme reports in general,
apart from having been motivated by the (define dummy (if #f #f)) idiom,
but this is a nice list.

In Guile we have a handy little debugging function, `pk'.  It's my
favorite.

    ;; peek takes any number of arguments, writes them to the
    ;; current ouput port, and returns the last argument.
    ;; It is handy to wrap around an expression to look at
    ;; a value each time is evaluated, e.g.:
    ;;
    ;;      (+ 10 (troublesome-fn))
    ;;      => (+ 10 (pk 'troublesome-fn-returned (troublesome-fn)))
    ;;

    (define (peek . stuff)
      (newline)
      (display ";;; ")
      (write stuff)
      (newline)
      (car (last-pair stuff)))

    (define pk peek)

The way it works is you just type `M-( p k SPC' in Emacs before your
squirrely expression, and you don't have to add dummy bindings.  It
returns the last item in its args, so adding it to the RHS of a `let' is
very natural.

If it were a macro I suppose that we could preserve the ability for the
last form to return multiple values, but oh well.  It's a stupid little
function but I use it all the time.

What little functions do you use all the time when debugging?

Regards,

Andy
--
http://wingolog.org/

_______________________________________________
Scheme-reports mailing list
Scheme-reports@scheme-reports.org
http://lists.scheme-reports.org/cgi-bin/mailman/listinfo/scheme-reports