[plt-scheme] How can I print the procedure?

From: Richard Cleis (rcleis at mac.com)
Date: Mon Dec 3 15:34:39 EST 2007

It's not clear what you are doing, but associating expressions with  
functions can be done without significant complication.  One way is  
to make a module and define your functions with your own syntax.  The  
example below keeps functions and their expressions in a hash table.   
The functions must be defined with a name so that the corresponding  
expression can be referenced.

Copy this to a DrScheme window and run it using at least the MzScheme  
language.  There are many more possibilities that depend on what you  
are trying to achieve.

rac

; beginning of example

(module expr-table mzscheme
   (define  table (make-hash-table))

   (define-syntax  def-express
     (syntax-rules ()
       ((def-express name (lambda args body))
        (begin
          (define name (lambda args name body))
          (hash-table-put! table name '(lambda args body))))
       ))

   (define  (get-express f)
     (hash-table-get table f))

   (provide def-express
            get-express)
   )

(require expr-table)

(def-express  sinx (lambda (x) (sin x)))
(def-express  cosx (lambda (x) (cos x)))
(def-express  sinx+cosx (lambda (x) (+ (sin x)(cos x))))
(def-express  sinx*cosx (lambda (x) (* (sin x)(cos x))))

(define my-list-of-functions (list sinx
                                    cosx
                                    sinx+cosx
                                    sinx*cosx))
(define  (do-funcs-of-x funcs x)
   (map (lambda (func)
          (format "For ~a, ~a = ~a~n"
                  x
                  (get-express func)
                  (func x)))
        funcs))

(for-each display (do-funcs-of-x my-list-of-functions 1.0))
(newline)
(for-each display (do-funcs-of-x my-list-of-functions 2.0))

; end of example






On Dec 2, 2007, at 10:53 PM, Majorinc, Kazimir wrote:

> I said:
>
> (define (min-f measure L)
>  (cond ((empty? (rest L)) (first L))
>        ((< (measure (first L))
>            (measure (min-f measure (rest L))))
>         (first L))
>        (else (min-f measure (rest L)))))
>
> (define (my-measure f)(+ (f 1) (f 10) (f 100)))
> (define my-list-of-functions (list (lambda(x)(sin x))
>                                   (lambda(x)(cos x))
>                                   (lambda(x)(+ (sin x)(cos x)))
>                                   (lambda(x)(* (sin x)(cos x)))))
>
> (min-f my-measure my-list-of-functions)
>
> And PLT said:
>
> #<procedure>
>
> Is there any way to actually print that #<procedure>? Except  
> rewriting such programs so they do not only calculate procedures,  
> but also prepare human-readable output? It can be significant  
> complication ...
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.