unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Hans-Werner Roitzsch <hwroitzsch@posteo.net>
To: 36682@debbugs.gnu.org
Subject: bug#36682: Error in Guile scripting examples
Date: Tue, 16 Jul 2019 00:39:43 +0200	[thread overview]
Message-ID: <6956abda-8d34-8f3a-0816-9faacee7c7e7@posteo.net> (raw)

[-- Attachment #1: Type: text/plain, Size: 2859 bytes --]

Hello GNU Team!

I wish to report a bug in either Guile's documentation or Guile's code
with regard to running scripts.

There are some examples of that given in Guile's documentation at:

https://www.gnu.org/software/guile/manual/html_node/Scripting-Examples.html#Scripting-Examples

In the following I will describe the problem.


I have the file `modules.scm` with the following code:

----8<----start-of-code---->8----
#!/usr/bin/env sh
exec guile -l fact.scm -e '(@ (my-module) main)' -s "$0" "$@"
!#

;; Explanation:
;; -e (my-module)
;; If run as a script run the `my-module` module's `main`.
;; (Use `@@` to reference not exported procedures.)
;; -s
;; Run the script.

(define-module (my-module)
  #:export (main))

;; Create a module named `fac`.
;; Export the `main` procedure as part of `fac`.

(define (n-choose-k n k)
  (/ (fact n)
     (* (fact k)
        (fact (- n k)))))

(define (main args)
  (let ((n (string->number (cadr args)))
        (k (string->number (caddr args))))
    (display (n-choose-k n k))
    (newline)))
----8<----end-of-code---->8----

And I have the following `fact.scm`:

----8<----start-of-code---->8----
#!/usr/local/bin/guile \
-e main -s
!#

;; How to run this program?
;; Example:
;; guile -e main -s factorial-script.scm 50
;; Explanation:
;; -e specifies the procedure to run
;; -s specifies to run this as a script
;; 50 is the number we take as input to the script

(define (fact n)
  (if (zero? n) 1
      (* n (fact (- n 1)))))

(define (main args)
  (display (fact (string->number (cadr args))))
  (newline))
----8<----end-of-code---->8----

The script is made executable by doing:

chmod +x modules.scm

Then I call the script as follows:

./modules.scm 10 3

This results in the error:

----8<----start-of-code---->8----
Backtrace:
           4 (apply-smob/1 #<catch-closure 119cb80>)
In ice-9/boot-9.scm:
    705:2  3 (call-with-prompt ("prompt") #<procedure 11aa8e0 at ice-9/eval.scm:330:13 ()> #<procedure default-prom…>)
In ice-9/eval.scm:
    619:8  2 (_ #(#(#<directory (guile-user) 1233140>)))
In /home/xiaolong/development/Guile/scripting/./modules.scm:
    26:13  1 (main _)
     18:0  0 (n-choose-k _ _)

/home/xiaolong/development/Guile/scripting/./modules.scm:18:0: In procedure n-choose-k:
In procedure module-lookup: Unbound variable: fact
----8<----end-of-code---->8----

According to my understanding of the tutorial in the Guile documentation
that I linked to above this code should work.

I also described the problem some time ago at:

https://stackoverflow.com/questions/50272618/guile-scheme-scripting-tutorial-loading-scripts

My Guile version is:

guile (GNU Guile) 2.2.4

Best regards,

Hans-Werner Roitzsch


[-- Attachment #2: Type: text/html, Size: 3660 bytes --]

             reply	other threads:[~2019-07-15 22:39 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-15 22:39 Hans-Werner Roitzsch [this message]
2019-07-15 23:44 ` bug#36682: Error in Guile scripting examples Arne Babenhauserheide
2022-12-12 21:17   ` Maxim Cournoyer
2022-12-13  1:08     ` Maxim Cournoyer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6956abda-8d34-8f3a-0816-9faacee7c7e7@posteo.net \
    --to=hwroitzsch@posteo.net \
    --cc=36682@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).