chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Create and write to a file


From: Robert Herman
Subject: Re: [Chicken-users] Create and write to a file
Date: Sun, 4 Oct 2015 09:34:03 +0800

Matt, I tried all of your suggestions, and they all work, and the explanation of the procedure call was crystal. Thanks!
I wasn't sure how it knew which and where to write the file. Then i just (define file-name "pidigits.txt"), and all was good. Now to open a new thread on Windows deployment.

Rob

On 3 October 2015 at 15:34, Matt Gushee <address@hidden> wrote:
Hi, Robert--

On Fri, Oct 2, 2015 at 1:48 PM, Robert Herman <address@hidden> wrote:

Here's what I have so far that throws a 'lambda-list expected' error. NOTE: I tried a format without the lambda part as well:
...
(display "How many digits of Pi to compute?\n ")

If you don't care about portability to other Scheme systems, you can write

    (print "How many digits of Pi to compute?")

and save yourself having to add a newline at the end.

(define digits (read))
 
(display "Here you go: \n")
(format #t (number->string (pich digits)))

There's nothing really wrong with the above code, but I don't see the point of using FORMAT here. You could just as well write

    (display (pich digits))

or

    (print (pich digits))
 

(define (write-to-a-file "c:/users/robert/desktop/Chicken-IPU-Examples/pidigits.txt" (lambda () (format #t "~A~%" (number->string(pigud num))))))

Here's where you're having trouble. Both the syntax of the procedure definition and the usage of FORMAT are wrong. A procedure definition should look like:

    (define (PROC-NAME [PARAMS])   ; parentheses enclosing proc name and params
        BODY)

... which is syntactic sugar for:

    (define PROC-NAME                      ; no parentheses enclosing proc name
        (lambda ( [PARAMS] )
            BODY)))

Note that in the first form, which is what you seem to be trying to write, you generally don't use a lambda _expression_ (you can, but in that case your function would return another function, which I don't think is what you mean to do).

The next problem is that you have a literal string (the filename) in the parameter list, which is not done (at least not that I've ever seen, and I can't imagine how it would be useful).

Finally, your call to FORMAT cannot write to a file as written. There's more than one way you could do it, but the simplest way would be to use WITH-OUTPUT-TO-FILE. So a correct definition might look like:

    (define (write-to-a-file file-name)
        (with-output-to-file file-name
            (lambda ()
                (format #t "~A-%" (pigud num)))))

Or

    (define write-to-a-file
        (lambda (file-name)
            (with-output-to-file file-name
                (lambda ()
                    (format #t "~A-%" (pigud num)))))

I'm assuming PIGUD and NUM are defined somewhere earlier in the program. Otherwise you'll get undefined variable errors. Also, if you actually want the file name to be a constant, maybe you don't need to define the WRITE-TO-A-FILE procedure at all - just perform the actions contained in its body.

Hope that helps.

--
Matt Gushee



reply via email to

[Prev in Thread] Current Thread [Next in Thread]