Python/Haskell Problems A value is considered \"touched\" every time that it is
ID: 3756142 • Letter: P
Question
Python/Haskell Problems
A value is considered "touched" every time that it is processed by/in a function.
double [] = [] double
(x:xs) = 2*x : double xs
The following code is in HASKELL. Write the number of times each value is touched in the following function examples (for values- 1,2 and 3). Explain the reasoning behind why the values are touched that many times and give a step by step explanation.
1. head (double (double [1, 2, 3])))
2. double (double [1, 2, 3]))
The following code is in PYTHON. Write the number of times each value is touched in the following function examples (for values- 1,2 and 3). Explain the reasoning behind why the values are touched that many times and give a step by step explanation.
def double2(l): return [2*x for x in l]
1. head (double (double ([1, 2, 3]))))
2. double (double ([1, 2, 3])))
Explanation / Answer
//Trip Mileage=miles/gallons
// Trip cost=pricePerGallons*gallons
// Trip cost per mile=Tripcost/miles.
#include <iostream>
#include <iomanip>
double const FLEET_AVG = 25.0;
//Function Prototype
double getGallons ();
double getMiles ();
double getPricePerGallon ();
double calcTripMileage (double miles, double gallons);
double calcTripCost (double pricePerGallon, double gallons);
double calcTripCostPerMile (double tripCost, double miles);
double calcOverallMPG (double totalMiles, double totalGallons);
void showOneTrip (double tripMileage, double tripCost, double tripCostPerMile);
void showTotals (double totalMiles, double totalGallons, double totalCost, double overallMPG);
void showMileageComparison (double overallMPG);
int main()
{
double gallons= 0,miles= 0,pricePerGallon = 0; //Place holder for user inputs
double tripMileage= 0,tripCost= 0,tripCostPerMile = 0; //Place holder for one trip stats double totalGallons= 0,totalMiles= 0,totalCost= 0; //Place holder for stats for all trips double overallMPG= 0; //Place holder for Overall Miles Per Gallon
char another; //Holds user's answer
cout << " Fuel Usage Analysis" << endl << endl;//Print Title
//Start do... while loop to process unknown number of trips
do {
//Get trip values
gallons = getGallons();
miles = getMiles();
pricePerGallon = getPricePerGallon();
cout << endl;
//Calculate trip costs
tripMileage = calcTripMileage(miles , gallons);
tripCost = calcTripCost(pricePerGallon , gallons);
tripCostPerMile = calcTripCostPerMile(tripCost , miles);
//Accumulate totals
totalGallons += gallons;
totalMiles += miles;
totalCost += tripCost;
overallMPG = calcOverallMPG(totalMiles, totalGallons);
//Display stats for one trip
showOneTrip(tripMileage, tripCost, tripCostPerMile);
cout << "Another? (y or n) ";
cin >> another;
}while (another == 'y' || another =='Y');
//Print totals of entered trips
showTotals(totalMiles, totalGallons, totalCost, overallMPG);
//Compare overall mpg from entered trips and compare with fleet average
showMileageComparison(overallMPG);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.