Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I output a grid of characters from a list of characters?

Tags:

scheme

racket

I have a list of characters that I want to print out as a grid of fixed column. For every line-width I want to print to a newline.

For example, I want

(#\Y #\q #\J #\u #\( #\t #\n #\m #\@ #\& #\c #\z #\x #\? #\} #\_ #\0 #\@ #\q #\r #\f #\/ #\I #\1 #\; #\B #\& #\O #\O #\O #\Y #\( #\n #\i #\0 #\B #\L #\h #\* #\# #\1 #\r #\? #\k #\& #\J #\J #\x #\# #\x #\i #\d #\Q #\o #\J #\J #\O #\o #\n #\_ #\v #\t #\r #\X #\c #\J #\X #\t #\z #\( #\) #\b #\C #\f #\x #\z #\d #\f #\Q #\t #\w)

To be displayed as:

YqJu(tnm@
&czx?}_0@
qrf/I1;B&
OOOY(ni0B
Lh*#1r?k&
JJx#xidQo
JJOon_vtr
XcJXtz()b
CfxzdfQtw

This is what I have tried:

(define (char-display char-list line-width)
  (for ([char (in-list char-list)]
        [i (in-range line-width)])
    (cond
      [(zero? i) (void)] ; to make sure modulo does not operate on zero
      [(zero? (modulo line-width i)) (display #\newline)]
      [else (display char)])))

On calling the function with the above list and line-width of 9 I get:


J
(tnm@[

What am I doing wrong? Is there a better way to do this?

like image 506
ath0 Avatar asked Nov 23 '25 13:11

ath0


1 Answers

Others have already provided good answers. I just want to point out in-slice, which can be helpful in cases like this.

(define (char-display char-list line-width)
  (for ([chars (in-slice line-width char-list)])
    (map display chars)
    (newline)))
like image 159
Michael MacLeod Avatar answered Nov 27 '25 13:11

Michael MacLeod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!