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
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...
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 ()
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] """   
In Java programming language 11.Create the code for a for loop to print the numbers 1  through...
In Java programming language 11.Create the code for a for loop to print the numbers 1  through 5 inclusive vertically so the output is 1 2 3 4 5 11b What is the output of the following               OUTPUT int i=3; while(int i >0) { System.out.println(i); i--; } 11c What is the output of the following               OUTPUT for(int i=3;i<10;i++) System.out.println(3*i); 11d Create the line of code to print the numbers 10 through 1 decreasing by 1 each time 11e Create the line of code...
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...
1. Create a simple 68K program called ADDER. Your program should add together the numbers: 6,...
1. Create a simple 68K program called ADDER. Your program should add together the numbers: 6, 4, 12, and 5. Store two of those values in variables and rest two are immediate addressing.
In racket, implement a tail-recursive function called sum-pairs that creates a new list by adding the...
In racket, implement a tail-recursive function called sum-pairs that creates a new list by adding the elements of an input list in pairs. That is the first element of the resulting list is the sum of the first two elements of the input, the second element of the resulting list is the sum of the 3rd and 4th elements of the input, and so on. If there is an odd number of elements, then the last element remains unchanged. As...
Scheme Programming Write a polynomial time scheme program to find the longest non-decreasing subsequent of a...
Scheme Programming Write a polynomial time scheme program to find the longest non-decreasing subsequent of a list of numbers. For example: (longest ''(2 4 3 1 2 1 3 6 1 3 2 1 2 33 4 2 4 10 11 7)) --> (1 1 1 1 2 4 4 10 11). Do not use side-effecting functions (i.e., functions with exclamation point in their names such as set!), sequencing, iteration (e.g., do, for-each) or vectors. Do not use input/output mechanisms...
9-Define a scheme procedure that returns the total number of leaves of a tree For example,...
9-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.