scheme - Display elements in list with for-each -
i have 1 big list of smaller 3-element-lists like:
( ("001" "bob" 80) ("002" "sam" 85) ("003" "aaron" 94) etc . . .)
i'm trying create like:
no.1: id=001, name=’’bob’’, grade=80 no.2: id=002, name=’’sam’’, grade=85 no.3: id=003, name=’’aaron’’, grade=94
i have access display , for-each (no "for" or "printf" functions)
i've been trying create for-each function takes list and:
pseudo-code:
for-each list in list display "id=(car list)" display "name ="(cadr list)" " etc
any appreciated!
so, interpreter doesn't have printf
after all? that's shame. can desired output hand, it's bit cumbersome should work on scheme interpreters, notice procedure required keeping track of index:
(define lst '(("001" "bob" 80) ("002" "sam" 85) ("003" "aaron" 94))) (define (add-index lst) (let loop ((lst lst) (idx 1)) (if (null? lst) '() (cons (cons idx (car lst)) (loop (cdr lst) (+ idx 1)))))) (for-each (lambda (e) (display "no.") (display (car e)) (display ": id=") (display (cadr e)) (display ", name=’’") (display (caddr e)) (display "’’, grade=") (display (cadddr e)) (newline)) (add-index lst))
it prints desired result:
no.1: id=001, name=’’bob’’, grade=80 no.2: id=002, name=’’sam’’, grade=85 no.3: id=003, name=’’aaron’’, grade=94
Comments
Post a Comment