Deep recursion in Scheme.
Deep recursion with lists in Scheme.
Date Created:Tuesday September 23rd, 2008 10:50 AM
Date Modified:Friday October 03rd, 2008 06:16 PM
(define (rev a)
(cond
((null? a) nil)
((list? (car a))
(append
(rev (cdr a))
(list (rev (car a)))
)
)
((list? a)
(append
(cdr a)
(list (car a))
)
)
(else a)
)
)
(define a '((1 2) (3 4)))
(rev a)
((4 3) (2 1))
; DEEP RECURSIVE
(define (deep-reverse lst)
(if (atom? lst)
lst
(append
(deep-reverse (cdr lst))
(list (deep-reverse (car lst))
)
)
)
#|
This counts all elements in all lists and sub-lists
|#
(define (count lst)
(cond ((null? lst) 0)
((not (pair? lst)) 1)
(else (+ (count (car lst)) (count (cdr lst))))
)
)
#|
This takes a list of lists of lists... and returns the values within those lists squared
|#
(define (square-list lst)
(map (lambda (sl)
(if (pair? sl)
(square-list sl)
(* sl sl)
)
) lst
)
)
Downloads:
Download: deep.scm 501 B
Download: count.scm 207 B
Download: square.scm 267 B
Please login or Click Here to register for downloads
Deep Recursion by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at www.3daet.com
Permissions beyond the scope of this license may be available at http://www.3daet.com
