Created
April 20, 2010 03:02
-
-
Save hltbra/371972 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang scheme | |
; http://pheattarchive.emporia.edu/courses/2002/cs220f02/anotherSchemeTtutorial/excercises.htm | |
; Exercise 1: | |
; Write a recursive procedure (positions 1st e) which returns a list of numbers corresponding | |
; to the position of every occurrence of element e in the list 1st. | |
; Assume the first element of the list is element 0. For example: | |
; (positions '(a b f b a f b b) 'b) ==> (1 3 6 7) | |
; | |
; hint: use a helper function | |
(define (positions lst e) | |
(define (positions-helper xs i) | |
(if (null? xs) '() | |
(append | |
(if (equal? (car xs) e) | |
(list i) | |
'()) | |
(positions-helper (cdr xs) (+ 1 i))) | |
) | |
) | |
(positions-helper lst 0) | |
) | |
(positions '(a b f b a f b b) 'b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment