LANGUAGE: SCHEME R5RS
Create a procedure called preceeding that will take a list of
numbers as argument and determine the elements and indices of those
elements that preceed a negative number in the given list. The
returned information should be in the form of a pair of lists:
((values) . (indices)).
E.g.
(preceeding '(1 -2 3 4 -5 6 -7 -8)) → ((1 4 6 -7).(0 3 5 6)) ;note: drracket will display this as ((1 4 6 -7) 0 3 5 6)
Here is the function:
(define preceeding2
(lambda (lst indexes where)
(if (< (length lst) 2) (append () (cons indexes ()))
(if (< (cadr lst) 0)
;(append
(cons
(car lst)
(preceeding2 (cdr lst) (append indexes (cons where ())) (+ where 1) )
)
;(append indexes (cons where ()) )
;)
(preceeding2 (cdr lst) indexes (+ where 1))
)
)
)
)
(define (preceeding lst)
(preceeding2 lst '() 0)
)
(preceeding '(1 -2 3 4 -5 6 -7 -8))
Get Answers For Free
Most questions answered within 1 hours.