suggestions and questions

For the Compleat Fan
Locked
Fanda
Posts: 253
Joined: Tue Aug 02, 2005 6:40 am
Contact:

suggestions and questions

Post by Fanda »

Happy New Year to all!

Some suggestions and questions ;-)

Suggestions:
- flat with an optional parameter of the level of flattening - (flat lst n)

Code: Select all

(set 'lst '(((1 2) (3 4)) ((5 6) (7 8))))
(flat lst 1) => ((1 2) (3 4) (5 6) (7 8))
(flat lst 2) => (1 2 3 4 5 6 7 8)

(flat lst 1) == (apply append lst)
(flat lst 2) == (apply append (apply append lst))
- inverse functions to hyperbolic sinh, cosh, tanh => asinh, acosh, atanh

- opposite of empty? == (not (empty? x))
how to name it? - full? or not-empty?

Questions:
- how to convert an expression to a string the same way as the function 'print'? - except by not writing and reading it from a file???

Code: Select all

(define (exp-str e)
  (device (open "tmp_file" "write"))
  (print e)
  (close (device))
  (read-file "tmp_file"))

> (set 'e '(begin (set 'x 5) (+ x x)))
(begin
 (set 'x 5)
 (+ x x))

> (exp-str e)
"(begin \r\n (set 'x 5) \r\n (+ x x))"

> (println (exp-str e))
(begin
 (set 'x 5)
 (+ x x))
"(begin \r\n (set 'x 5) \r\n (+ x x))"
- is there any other way to find out the name of a script which is currently running? - except by calling (main-args)???

Fanda

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

try 'source' on symbols:

Code: Select all

(define (e) (begin (set 'x 5) (+ x x)))

(source 'e) =>

"(define (e )\n  (begin \n   (set 'x 5) \n   (+ x x)))\n\n"
Lutz

ps: on Windows you would see \r\n instead of \n

Fanda
Posts: 253
Joined: Tue Aug 02, 2005 6:40 am
Contact:

Post by Fanda »

I would like to be able to print expressions nicely - that's why I need it :-)

Improved version based on 'source':

Code: Select all

(define (exp-str e)
  (let (f nil line-break "")
    (letex (e e)
      (define (f) e))
    (write-line "" line-break)
    (join (map (fn (s) (slice s 2)) (chop (rest (parse (source 'f) line-break)))) line-break)))
Example:

Code: Select all

> (exp-str '(begin (set 'x 5) (+ x x)))
"(begin \r\n (set 'x 5) \r\n (+ x x)))\r\n"

> (print (exp-str '(begin (set 'x 5) (+ x x))))
(begin
 (set 'x 5)
 (+ x x)))
"(begin \r\n (set 'x 5) \r\n (+ x x)))\r\n"
Fanda

PS: This is my platform independent way, how to figure out the line break:

Code: Select all

(write-line "" line-break)

Fanda
Posts: 253
Joined: Tue Aug 02, 2005 6:40 am
Contact:

Post by Fanda »

Another version (just in case somebody wants to use it):

Code: Select all

(define (exp-str e)
  (let (f nil line-break "" pp (pretty-print) result "")
    (write-line "" line-break)
    (pretty-print 64 " ")
  
    ;; get expression
    (letex (e e)
      (define (f) e))
    (set 'result (join (map (fn (s) (slice s 2)) (chop (rest (parse (source 'f) line-break)))) line-break))

    ;; cut-out the last ')'
    (reverse result)
    (set 'result (select result (difference (sequence 0 (- (length result) 1)) (list (find ")" result)))))
    (reverse result)
  
    (apply pretty-print pp)
    result))
Fanda

Locked