Question

In: Computer Science

I am writing a scheme function that pairs an element to every element in a list,...

I am writing a scheme function that pairs an element to every element in a list, and I have the following code so far:

(define (pair-element e l)   
(cond ((null? l) (list e))
(else (cons e (car l)) (pair-element (cdr l)))))
(pair-element 4 `(4 3 5))

What is my error? I'm getting a pair element mismatch when I run it. I need to do this without using built-in functions. Thanks!

Solutions

Expert Solution

1.(define (pair-element e l)   
2.(cond ((null? l) (list e))
3.(else (cons e (car l)) (pair-element (cdr l)))))

Your program is calling function (pair-element) with just one argument at line number 3. It takers two arguments "e" and "l".

Second error is that your program will always return only list of single element. Not all pairs. Cons the elements as shoen in below code.

Correct Code:

(define (pair-element e l)   
(cond ((null? l) '()) ;;if list empty should return empty list not list which contains e
(else (cons (cons e (list (car l))) (pair-element e (cdr l)))))) ;;else pair element with each member of list.

Snapshot of Code and Output:


Related Solutions

Use Scheme Language Write a Scheme function that takes a list and returns a list identical...
Use Scheme Language Write a Scheme function that takes a list and returns a list identical to the parameter except the third element has been deleted. For example, (deleteitem '(a b c d e)) returns ‘(a b d e) ; (deleteitem '(a b (c d) e)) returns ‘(a b e).
Python: I am currently a function that will return a list of lists, with the frequency...
Python: I am currently a function that will return a list of lists, with the frequency and the zip code. However, I am having a difficult time organizing the list in decreasing order of frequency. We are asked to use sort () and sorted () instead of lambda. The function will find 5 digit zip codes, and when it sees a 9-digit zip code, the function needs to truncate the last 4 digits. The following is an example of the...
Send an element from 2d array to a function using pointer to pointer. c++ I am...
Send an element from 2d array to a function using pointer to pointer. c++ I am having an issue with being able to send specific elements to the swap function. #include <iostream> using namespace std; void swap(int **a, int **b){    int temp = **a;    **a=**b;    **b=temp; } void selSort(int **arr, int d){    for(int i =0; i<d-1; i++){        int min = *(*arr+i);        int minin = i;        for(int j =i+1; j<d; j++){...
I am writing a paper on the dealiest epidemics in recorded history. I am looking for...
I am writing a paper on the dealiest epidemics in recorded history. I am looking for information on the polio virus, thypoid fever, and the black palgue. I need help identifying when these diseases started, what started them, the virus or mutation that caused them, how they spread and the preventative measures to stop them.
I am writing a paper on the dealiest epidemics in recorded history. I am looking for...
I am writing a paper on the dealiest epidemics in recorded history. I am looking for information on typhus epidemic/camp fever. I need help identifying when these diseases started, what started them, the virus or mutation that caused them, how they spread and the preventative measures to stop them.
I am writing a paper on Financial Restructuring. I am required to pick a company that...
I am writing a paper on Financial Restructuring. I am required to pick a company that is in [potential] trouble of defaulting/bankruptcy. I have chosen Toys R Us. I am required to: 1) Analyze the company's (Toys R Us) financial history and current financial situation. 2) Propose a financial restructuring proposal. It is my understanding that I need to look at all possible financial statements, income statements, cash flows, and use financial tools to "financially restructure" and propose a "fix"...
I am always found in pairs, sometimes the pair appear as short chains. I am protected...
I am always found in pairs, sometimes the pair appear as short chains. I am protected by a coat that can swell in the presence of specific antiserum. I am not exactly spherical but more lanceolate with sides facing each other being more rounded. I can't survive on simple nutrition and presence of blood boosts my growth. I can lyse blood but only partially. I grow better in a candle jar. As I grow old, I become suicidal. My colonies...
scheme: Write a recursive Scheme function (subst x y L), which returns a list identical to...
scheme: Write a recursive Scheme function (subst x y L), which returns a list identical to L except that every occurrence of x has been replaced with y. The following example illustrates the use of this function: > (subst 'c 'k '(c o c o n u t)) (k o k o n u t) Write a recursive Scheme function (all-different? L), which determines whether all elements of list L are distinct (that is, not equal?). The following example illustrates...
IN SCHEME Write a function subtract which takes a list of numbers as a parameter and...
IN SCHEME Write a function subtract which takes a list of numbers as a parameter and returns a list of the differences. So, a list containing the second minus the first, third minus the second, etc. (subtract '(3 4 10 14 5)) '(1 6 4 -9)
Suppose that I am spending $6,000 every month, but now I suppose to save $2,000 every...
Suppose that I am spending $6,000 every month, but now I suppose to save $2,000 every month and only spend $4,000. According to the classical economists, how will this decision to save affect my country’s RGDP? RGDP will increase RGDP will decrease RGDP will not change Suppose that the current wage rate in a market is $14 per hour and the equilibrium wage rate is $12. Given this information, we can assume that… The quantity of labor supplied will exceed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT