Loop over a list pasted by alaricsp on Tue Nov 20 12:06:04 2012
(define some-list '("hello" "world")) (let loop ((todo some-list)) (unless (null? todo) (write (string-length (car todo))) (newline) (loop (cdr todo))))
Explicit recursion pasted by alaricsp on Tue Nov 20 12:11:46 2012
(define (string-lengths some-list) (unless (null? some-list) (write (string-length (car some-list))) (newline) (string-lengths (cdr some-list)))) (string-lengths '("hello" "world"))
for-each pasted by alaricsp on Tue Nov 20 12:16:55 2012
(define some-list '("hello" "world")) (for-each (lambda (str) (write (string-length str)) (newline)) some-list)
map pasted by alaricsp on Tue Nov 20 12:18:07 2012
;; Now let's start moving away from the imperative model (define some-list '("hello" "world")) (write (map string-length some-list)) ;; Wasn't THAT simpler? :-)
Explictly explicit recursion added by andyjpb on Tue Nov 20 12:21:19 2012
(define (string-lengths some-list #!optional (lengths '()))
; some-list - the list of strings.
; lengths - the length of the strings we've calc'd so far : initially '().
(if (null? somelist)
(begin ; "base case" - we're at the end of some-list.
(display "Got empty list: ending")
(newline)
(reverse lengths))
(begin ; take the first element in the list and work out the length.
(display (string-length (car some-list)))
(newline)
(string-lengths ; pass on the rest of the list and the list of lengths.
(cdr some-list) ; the tail of some-list
(cons - place the string-length of list list at the head of the list of lengths.
(string-length (car some-list))
lengths)))))