Question

LANGUAGE: SCHEME R5RS Create a procedure called preceeding that will take a list of numbers as...

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)

Homework Answers

Answer #1

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))
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
11. Define a scheme procedure that returns the total number of leaves of a tree For...
11. Define a scheme procedure that returns the total number of leaves of a tree For example, (count-leaves '((4 5 6) (1 2) () (3) )) returns 6 Then, trace the procedure with the given example. Scheme language
Define a function in Scheme which takes a list of numbers and a scalar and returns...
Define a function in Scheme which takes a list of numbers and a scalar and returns the rest of the list after the first occurrence of the scalar that was passed. An empty list should be returned if the value doesn't exist. For example, (list-past-scalar '(5 2 8 3 1 9 2 3) 3) #returns (1 9 2 3) (list-past-scalar '(37 18 38 65 90) 100) #returns ()
Program has to be written in ML language. Write functions in ML to do the following:...
Program has to be written in ML language. Write functions in ML to do the following: 1. Given a list, return that list with its first and third elements deleted. Assume the length of the list is at least 3. 2. Given a list of real pairs, return the list containing the larger element in each pair. Examples: larger_in_pair([]) = [] larger_in_pair([(1.0,2.5),(4.7,3.6),(5.5,8.8)] = [2.5,4.7,8.8] 3. Given two lists of integers, return the list of the sums of the elements in...
square the elements of a linear list of numbers (square ‘(1 -3 -5 7)) => (1...
square the elements of a linear list of numbers (square ‘(1 -3 -5 7)) => (1 9 25 49) using DrRacket The code has to be from scratch, no library functions. and kindly explain what each line of code is doing.
Needs to be done in DrRacket Write a function dotProduct which takes two lists of numbers...
Needs to be done in DrRacket Write a function dotProduct which takes two lists of numbers representing vectors, and produces their dot product (or reports their incompatibility). E.g., (dotProduct '(1 2) '(3 4) ) -> 11 (dotProduct '(1 2 3) '(4 5 6)) -> 32 (dotProduct '(1 2 3) '(4 5)) -> *incompatible*
In DrRacket, is there any way I can change (list 1 (list 2 (list 3 4...
In DrRacket, is there any way I can change (list 1 (list 2 (list 3 4 5) 6 )7) to (list 1 2 3 4 5 6 7)?
def count_evens(values: List[List[int]]) -> List[int]: """Return a list of counts of even numbers in each of...
def count_evens(values: List[List[int]]) -> List[int]: """Return a list of counts of even numbers in each of the inner lists of values.    >>> count_evens([[10, 20, 30]]) [3] >>> count_evens([[1, 2], [3], [4, 5, 6]]) [1, 0, 2] """   
def count_evens(values: List[List[int]]) -> List[int]: """Return a list of counts of even numbers in each of...
def count_evens(values: List[List[int]]) -> List[int]: """Return a list of counts of even numbers in each of the inner lists of values.    >>> count_evens([[10, 20, 30]]) [3] >>> count_evens([[1, 2], [3], [4, 5, 6]]) [1, 0, 2] """
Java please. Given a sequence of unsorted numbers, determine how badly out of order they are....
Java please. Given a sequence of unsorted numbers, determine how badly out of order they are. Write a program that, for any given sequence of unique natural numbers, will compute the 'distance' between that original ordering and the same set of numbers sorted in ascending order. The distance should be computed by calculating how far displaced each number is in the original ordering from its correct location in the sorted list and summing those results. For instance, given the list...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort algorithm to sort this list. Fill in this table with each iteration of the loop in the selection sort algorithm. Mark the place from which you are looking for the 'next smallest element'. In this display, the upper numbers are the indices, the lower numbers are in the corresponding positions. Use the several rows provided to show the sequence of steps. 0 1 2...