Skip to content

Instantly share code, notes, and snippets.

@hltbra
Created April 20, 2010 03:02
Show Gist options
  • Save hltbra/371972 to your computer and use it in GitHub Desktop.
Save hltbra/371972 to your computer and use it in GitHub Desktop.
#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