[plt-scheme] Local function definition

From: Richard Cleis (rcleis at mac.com)
Date: Mon Jan 12 18:35:35 EST 2004

What's about the following?

(define (connect-to-server-1)
   (let ((ip (read-line))
         (port (read)))
     (display "Server address: ")
     (display "Port: "))
     (tcp-connect ip port))

To me, define'itions are for the outermost scope since the language 
isn't intended to see inside functions anyway.  That's why I like it.  
Perhaps there is an ordering issue with the above.  But I doubt it, 
since let* will even let you put all of the code in the let-list:

(define (connect-to-server-2)
   (let* ((ip-read (read-line))
          (ip ip-read)
          (port (read))
          (waste-not (display "Server address: "))
          (want-not (display "Port: "))
          (have-not (tcp-connect ip port)))
   'do-nothing-body))

If the assignments weren't made in order, then ip wouldn't know where 
to get ip-read.

rac


On Jan 12, 2004, at 8:53 AM, Dor Kleiman wrote:

>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
> I think so too; for instance, the following example (or anything with
> write-read) is quite much simpler with co-mingling:
> ; with co-mingling
> (define (connect-to-server)
>   (display "Server address: ")
>   (define ip (read-line))
>   (display "Port: ")
>   (define port (read))
>   (tcp-connect ip port))
>
> ; without co-mingling
> ; example 1
> (define (connect-to-server-1)
>   (display "Server address: ")
>   (let ([ip (read-line)])
>     (display "Port: ")
>     (let ([port (read)])
>       (tcp-connect ip port))))
>
> ; example 2
> (define (prompt p f)
>   ; displays the prompt p and runs f (e.g. read or read-line)
>   (display p)
>   (f))
> (define (connect-to-server-2)
>   (define ip (prompt "Server address: " read-line))
>   (define port (prompt "Port: " read))
>   (tcp-connect ip port))
>
> ; example 3
> (define (connect-to-server-3)
>   (define ip (begin (display "Server address: ")
>                     (read-line)))
>   (define port (begin (display "Port: ")
>                       (read)))
>   (tcp-connect ip port))
>
> All examples need more input checking, etc., but they are just examples
> of a simple thing becoming so complex...
> -----Original Message-----
> From: Robby Findler [mailto:robby at cs.uchicago.edu]
> Sent: Monday, January 12, 2004 4:13 PM
> To: Dor Kleiman
> Cc: Guillaume Marceau; plt-scheme at po.cs.brown.edu
> Subject: RE: [plt-scheme] Local function definition
>
> At Mon, 12 Jan 2004 11:29:13 +0200, "Dor Kleiman" wrote:
>> Which brings me to something I've always wondered about: Why can't you
>> commingle definesand expressions?
>
> Because the language is defined that way. I'm not sure of the rationale
> of the designers on this particular point. It is certainly possible to
> allow such co-mingling (units, modules, and classes in mzscheme all
> allow it). I, for one, think that internal definitions would be better
> off if co-mingling were allowed.
>
> Robby



Posted on the users mailing list.