suggestions and questions

Started by Fanda, January 11, 2007, 12:50:21 PM

Previous topic - Next topic

Fanda

Happy New Year to all!



Some suggestions and questions ;-)



Suggestions:

- flat with an optional parameter of the level of flattening - (flat lst n)
(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???
(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 rn (set 'x 5) rn (+ x x))"

> (println (exp-str e))
(begin
 (set 'x 5)
 (+ x x))
"(begin rn (set 'x 5) rn (+ 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

#1
try 'source' on symbols:


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

(source 'e) =>

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


Lutz



ps: on Windows you would see rn instead of n

Fanda

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



Improved version based on 'source':
(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:
> (exp-str '(begin (set 'x 5) (+ x x)))
"(begin rn (set 'x 5) rn (+ x x)))rn"

> (print (exp-str '(begin (set 'x 5) (+ x x))))
(begin
 (set 'x 5)
 (+ x x)))
"(begin rn (set 'x 5) rn (+ x x)))rn"


Fanda



PS: This is my platform independent way, how to figure out the line break:
(write-line "" line-break)

Fanda

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


(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