A question about dotted list: '(1 2 . 3)
;; => '(1 2 . 3)
That’s OK. However '(1 . 2 . 3)
;; => '(2 1 3)
produced a list. Why?
I hope '(1 . 2 . 3) ; => '(1 2 . 3) which == (cons 1 (cons 2 3))
That’s a Racket only extension. The idea is that (1 . < . x) turns into (< 1 x).
but I have a quotation
It should not be seen as an application imo.
It’s done on the reader level, so it works for everything that looks like a list.
It’s the last paragraph of the link above.
> (1 . 2 . 3) reads equal to (list 2 1 3)
Thanks.
I need to pretty print a two-dimensional list. I did not find it on the internet, so I had to write it by myself. (define (print-2-dim-list lol)
(define (transpose xss)
(apply map list xss))
(define t (transpose lol))
(define list-of-max-len (map (λ (l) (foldr (λ (x v) (max (string-length (format "~a" x)) v)) 0 l)) t))
(printf "(\n")
(for ([row lol])
(printf "(")
(for ([x row]
[max-len list-of-max-len])
(printf "~a" x)
(for ([n (+ 1 (- max-len (string-length (format "~a" x))))])
(printf "~a" #\space)))
(printf ")\n")
)
(printf ")\n"))
(print-2-dim-list '((xxx xx xxx)
(xx xxxxxxxxxxx xxxxxxxxx)
(xxx xxx xxxxx)
(xxx xxxxxxxxxxxxx xxxxxxxxxxxx)
(xxxxxxxxxxxxx xxx xxxxxxxxxx)))
;; (
;; (xxx xx xxx )
;; (xx xxxxxxxxxxx xxxxxxxxx )
;; (xxx xxx xxxxx )
;; (xxx xxxxxxxxxxxxx xxxxxxxxxxxx )
;; (xxxxxxxxxxxxx xxx xxxxxxxxxx )
;; )
I think my code may be a bit verbose. Any suggestion?
(displayln
(table->string '((xxx xx xxx)
(xx xxxxxxxxxxx xxxxxxxxx)
(xxx xxx xxxxx)
(xxx xxxxxxxxxxxxx xxxxxxxxxxxx)
(xxxxxxxxxxxxx xxx xxxxxxxxxx))
#:border-style 'space
#:framed? #t
#:row-sep? #f
#:align '(left)))
;; xxx xx xxx
;; xx xxxxxxxxxxx xxxxxxxxx
;; xxx xxx xxxxx
;; xxx xxxxxxxxxxxxx xxxxxxxxxxxx
;; xxxxxxxxxxxxx xxx xxxxxxxxxx
Nice! @sorawee Thank you for telling me this information.
Anyway it can’t preserve the parentheses. I think my procedure may still useful:smile:.
Well, thanks @laurent.orseau for creating the package!