1. Write a simple function that calculates the sum of a list of integers. e.g. (sum ' XXXXXXXXXXreturns 15. Implement a non-tail-recursive solution and a tail-recursive solution. 2. Write a Scheme max...

1 answer below »
. Write a simple function that calculates the sum of a list of integers. e.g. (sum '(5 4 6)) returns 15. Implement a non-tail-recursive solution and a tail-recursive solution


1. Write a simple function that calculates the sum of a list of integers. e.g. (sum '(5 4 6)) returns 15. Implement a non-tail-recursive solution and a tail-recursive solution. 2. Write a Scheme max function that finds the largest number. The input should be a list of integers. The logic is discussed as follows. boolean max(lst){ if (lst has only one element){ return the one element in lst } else if (car(lst) > max(cdr(lst))) { return car(lst) } else { return max(cdr(lst)) } } . Write a Scheme function that takes two atoms and a list as parameters and replaces all top-level occurrences of the first given atom in the list with the second given atom. For example, (replace 'a 'c '(a b (a b)) returns (c b (a b)), (replace 'a 'c '(a b (a b) a b c)) returns (c b (a b) c b c) 2. Arithmetic of Functions We can define a higher-order function, i.e. functional form that accepts two functions as parameters and returns their sum, As a running example, we’ll consider two functions: f(x) = x + 2 g(x) = 3x + 4 These are modeled, respectively, by the following Scheme functions: (define (f x) (+ x 2)) (define (g x) (+ (* 3 x ) 4)) The higher order function: (define (plus func1 func2) (lambda (x) (+ (func1 x) (func2 x)))) Note the use of the lambda in order to return a nameless function. In essence, we are accepting two functions, func1 and func2, and returning a function that takes one parameter x and represents their sum. We can apply this functional form as follows: ((plus f g) 10); returns 46. Or (define (plus func1 func2 x) (+ (func1 x) (func2 x)) ) (plus f g 10); returns 46. Define a function (form) that compute the difference of two functions. 3. Imagine an accounting routine used in a book shop. It works on a list with sublists, which look like this: Order NUmber Book Title and Author Quantity Price per Item 34587 Learning Python, Mark Lutz 4 40.95 98762 Programming Python, Mark Lutz 5 56.80 77226 Head First Python, Paul Barry 3 32.95 Write a Python program (not in imperative style), which returns the grand sale total in dollar amount. 1.(5 points) What does the following Scheme function do? Explain your answer using TWO EXAMPLES. (equal? can be used for comparison with strings, byte strings, numbers, pairs, mutable pairs, vectors, boxes, hash tables, and inspects table structures.) (define (y s lis) (cond ((null? lis) '()) ((equal? s (car lis)) lis) (else (y s (cdr lis))) )) 2. (5 points) Write a Scheme function numofatoms that determines the number of elements in a list. For example, (numOfSymbols '((1 3) 7 (4 (5 2) ) ) should return 6. 3. (5 points) Function composition We can define a higher order function, i.e. functional form, that accepts two functions as parameters and returns their sum, As an example, consider two functions below: f1(x) = x + 2 g1(x) = 3x + 4 These are modeled by the following Scheme functions, respectively: (define (f1 x) (+ x 2)) (define (g1 x) (+ (* 3 x ) 4)) The higher order function plus = f1 + g1: (define (plus func1 func2) (lambda (x) (+ (func1 x) (func2 x)))) Note the use of the lambda in order to return a nameless function. In essence, we are accepting two functions, func1 and func2, and returning a function that takes one parameter x and represents their sum. We can apply this functional form as follows: ((plus f1 g1) 10); returns 46. Or (define (plus func1 func2 x) (+ (func1 x) (func2 x)) ) (plus f1 g1 10); returns 46. Define a function (form) that compute the product of two functions and test your functions. 4. (5 points) Summarize Section 15. 11, A Comparison of Functional and Imperative Languages of our book with around 200 words. 5. (15 points) Pick 5 concepts to define. Your definition COULD include one small example. Use at least 30 words. Functional objects Closure Map, filter, reduce List comprehension List generator Lazy evaluations Curried functions Type inferencing Polymorphism Higher-order functions Referential Transparency monad lambda calculus **** Turn in a python notebook (either .ipynb, or .pdf, or .py format) for questions 6 and 7 **** 6. (5 points) Given employee data from some company, the semantics of each row are: employee ID, employee name, age, and salary. 142519864,Susan Martin,39,56990 242518965,James Smith,68,27099 141582651,Mary Johnson,44,94011 011564812,John Williams,35,74098 254099823,Patricia Jones,28,42783 356187925,Robert Brown,28,35431 489456522,Linda Davis,26,25971 287321212,Michael Miller,62,131072 Write a Python function that returns a new employee list with salary updated: if an employee is younger than 40, raise his or her salary by 5%; otherwise raise the salary by 10%. Refer to lecture notes or exercises and solutions from http://www.python-course.eu/python3_lambda.php for help, if needed. 7. (10 extra points). Each year the people at Stack Overflow ask the developer community about all kinds of things related to their jobs. What's your favorite language? What part of your job do you like the best? What was your major in college? How much do you make? The survey file used here had 98,855 respondents. We will work with a sample of 2000 of those responses for this homework problem. In addition we have narrowed down the questions from 129 to just 13. The columns we have included in this data set are: Respondent Country JobSatisfaction UndergradMajor ConvertedSalary Exercise Gender RaceEthnicity EducationParents HoursOutside Age LastNewJob LanguageWorkedWith Here is one question that we want you to find out: What are the top 10 most popular programming languages? For full credits, you must have one substantial 'pure' function in your solution.
Answered 4 days AfterNov 04, 2021

Answer To: 1. Write a simple function that calculates the sum of a list of integers. e.g. (sum '...

Anandkumar answered on Nov 06 2021
104 Votes
1. Write a simple function that calculates the sum of a list of integers. e.g. (sum '(5 4 6)) returns 15. Implement a non-tail-recursive solution and a tail-recursive solution.
Non Tail recursive in java:
im
port java.util.*;
class Main{
public static int sum(List list)
{
int sum = 0;
for (int i: list)
{
sum += i;
}
return sum;
}
public static void main(String[] args) {
List ints = Arrays.asList(1, 2, 3);
int sum = sum(ints);
System.out.println("Sum is: " + sum);
}
}
Output:
Tail recursive in java:
import java.util.*;
class Main{
static int tail_recurs(int []array, int len, int sum)
{
// Base Case
if (len == 0)
return sum;

// Function Call Observe sum+array[size-1]
// to maintain sum of elements
return tail_recurs(array, len - 1, sum + array[len - 1]);
}

// Driver code
public static void main(String[] args)
{
int array[] = { 5, 4, 6 };
int len = array.length;
System.out.print(tail_recurs(array, len, 0));
}
}
Output:
3. Imagine an accounting routine used in a book shop. It works on a list with sublists, which look like this:
Order NUmber     Book Title and Author     Quantity     Price per Item
34587     Learning Python, Mark Lutz         4     40.95
98762     Programming Python, Mark Lutz         5     56.80
77226     Head First Python, Paul Barry         3     32.95
Write a Python program (not in imperative style), which returns the grand sale total in dollar amount.
Answer:
list=[[34587,"Learning Python,Mark Lutz",4,40.95],
[98762,'Programming Python, Mark Lutz',5,56.80],
[77226,'Head First Python, Paul Barry',3,32.95]]
sum_a=[]
for i in list:
sum_a.append(i[3])
print(sum(sum_a))
2. (5 points) Write a Scheme function numofatoms that determines the number of elements in a list.
For example, (numOfSymbols '((1 3) 7 (4 (5 2) ) ) should return
Answer:
def flatten(list_of_lists):
if len(list_of_lists) == 0:
return list_of_lists
if isinstance(list_of_lists[0], list):
return flatten(list_of_lists[0]) +...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here