From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Stephen Compall Newsgroups: gmane.lisp.guile.user Subject: continuation curiosity -- for entertainment purposes only Date: Wed, 16 Jul 2003 12:50:07 -0500 Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Message-ID: <200307161750.h6GHo7H00853@csserver.evansville.edu> NNTP-Posting-Host: main.gmane.org X-Trace: main.gmane.org 1058378225 22329 80.91.224.249 (16 Jul 2003 17:57:05 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Wed, 16 Jul 2003 17:57:05 +0000 (UTC) Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Wed Jul 16 19:57:03 2003 Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 19cqUk-0005fC-00 for ; Wed, 16 Jul 2003 19:55:15 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.20) id 19cqUi-00088m-1m for guile-user@m.gmane.org; Wed, 16 Jul 2003 13:55:12 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.20) id 19cqQ6-0006js-KN for guile-user@gnu.org; Wed, 16 Jul 2003 13:50:26 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.20) id 19cqPv-0006gh-QP for guile-user@gnu.org; Wed, 16 Jul 2003 13:50:16 -0400 Original-Received: from [192.195.228.35] (helo=csserver.evansville.edu) by monty-python.gnu.org with esmtp (Exim 4.20) id 19cqPu-0006f8-HH for guile-user@gnu.org; Wed, 16 Jul 2003 13:50:14 -0400 Original-Received: (from sc87@localhost) by csserver.evansville.edu (8.11.6/8.11.6) id h6GHo7H00853; Wed, 16 Jul 2003 12:50:07 -0500 Original-To: guile-user@gnu.org X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.2 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: main.gmane.org gmane.lisp.guile.user:2088 X-Report-Spam: http://spam.gmane.org/gmane.lisp.guile.user:2088 While writing up an interesting example for using call/cc, I ran into this curiosity. Expressions at the top level are not considered a single program flow. Observe: Here is the original source for `ccgrep.scm', including lots of debugging messages: ;; begin ccgrep.scm (use-modules (ice-9 rdelim) (srfi srfi-13)) (define debug-message (lambda args (for-each display args) (newline))) (define next-match #f) (display (let lp ((line (read-line))) (debug-message "called: " line) (if (eof-object? line) (begin (set! next-match #f) "") (if (string-contains line "bash") (or (call-with-current-continuation (lambda (nm) (debug-message "captured continuation") (set! next-match nm) (string-append line "\n"))) (lp (read-line))) (lp (read-line)))))) (debug-message "value of continuation saver: " next-match) (and next-match (next-match (begin (debug-message "calling continuation") #f))) (debug-message "quitting") ;; end ccgrep.scm I piped /etc/passwd to it, and here is the output (with some unimportant lines taken out): [sirian@localhost guile]$ guile -s ccgrep.scm < /etc/passwd called: root:x:0:0:root:/root:/bin/bash captured continuation root:x:0:0:root:/root:/bin/bash value of continuation saver: # calling continuation called: bin:x:1:1:bin:/bin: called: daemon:x:2:2:daemon:/sbin: called: gopher:x:13:30:gopher:/usr/lib/gopher-data: called: postgres:x:40:41:PostgreSQL Server:/var/lib/pgsql:/bin/bash captured continuation postgres:x:40:41:PostgreSQL Server:/var/lib/pgsql:/bin/bash quitting Imagine my surprise when it failed to consider the top-level expressions that report on next-match and run it if it's there as part of the flow, and skipped to what it hadn't executed --- the "quitting" message. So I reworked it as a function created and called in one step: ;; repeat previous setup ((lambda () (define next-match #f) (display (let lp ((line (read-line))) (debug-message "called: " line) (if (eof-object? line) (begin (set! next-match #f) "") (if (string-contains line "bash") (or (call-with-current-continuation (lambda (nm) (debug-message "captured continuation") (set! next-match nm) (string-append line "\n"))) (lp (read-line))) (lp (read-line)))))) (debug-message "value of continuation saver: " next-match) (and next-match (next-match (begin (debug-message "calling continuation") #f))) (debug-message "quitting"))) ;; end of ccgrep.scm ...and it worked exactly as I expected it to, as a simple grep proof-of-concept that displays all lines containing "bash". I haven't tried using a simple let in place of my one-shot function. -- Stephen Compall or s11 or sirian A plumber is needed, the network drain is clogged _______________________________________________ Guile-user mailing list Guile-user@gnu.org http://mail.gnu.org/mailman/listinfo/guile-user