[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#36682: Error in Guile scripting examples
From: |
Maxim Cournoyer |
Subject: |
bug#36682: Error in Guile scripting examples |
Date: |
Mon, 12 Dec 2022 16:17:55 -0500 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) |
Hello,
Arne Babenhauserheide <arne_bab@web.de> writes:
> Hello Hans-Werner Roitzsch,
>
> It looks like you’re mixing up two concepts: the fac creates a module
> and loads the fact which is not a module, so basically main and choose
> live in another namespace than fact (define-module starts a new
> namespace).
>
> And it seems that this is indeed a bug in the documentation, because
> https://www.gnu.org/software/guile/manual/html_node/Scripting-Examples.html#Scripting-Examples
> jumps to defining fac as a module but does not at the same time define
> and import fact as a module, too.
>
> Hans-Werner Roitzsch <hwroitzsch@posteo.net> writes:
>> 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))
>
> At this point you need
>
> (use-modules (fact))
>
>> ;; 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
>
> To be usable as module, this needs to be defined as module:
>
> (define-module (fact)
> #:export (fact))
>
>> (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----
> …
>> chmod +x modules.scm
>> ./modules.scm 10 3
>
> Does it work with the added module definition and import?
Thank you for the above explanations. I got confused by the this in the
documentation as well. Trying the above suggestions, I still have a
problem.
I have the following two files:
fact:
--8<---------------cut here---------------start------------->8---
#!/run/current-system/profile/bin/guile \
-e main -s
!#
(define-module (fact)
#:export (fact))
(define (fact n)
(if (zero? n) 1
(* n (fact (- n 1)))))
(define (main args)
(display (fact (string->number (cadr args))))
(newline))
--8<---------------cut here---------------end--------------->8---
fac:
--8<---------------cut here---------------start------------->8---
#!/run/current-system/profile/bin/guile \
-e (@@ (fac) main) -s
!#
(define-module (fac)
#:export (main))
(use-modules (fact))
(define (choose n m)
(/ (fact m) (* (fact (- m n)) (fact n))))
(define (main args)
(let ((n (string->number (cadr args)))
(m (string->number (caddr args))))
(display (choose n m))
(newline)))
--8<---------------cut here---------------end--------------->8---
But with Guile 3.0.8, this gives me:
--8<---------------cut here---------------start------------->8---
./fac 5 20
ice-9/read.scm:126:4: In procedure lp:
#<unknown port>:1:4: unexpected end of input while searching for: )
--8<---------------cut here---------------end--------------->8---
Which I don't understand.
--
Thanks,
Maxim
- bug#36682: Error in Guile scripting examples,
Maxim Cournoyer <=