Use the map and reduce functions we learned in class to implement function minAb
ID: 3718625 • Letter: U
Question
Use the map and reduce functions we learned in class to implement function minAbsoluteVal that determines the minimal absolute value of a list of integer numbers.
Example (define minAbsoluteVal (lambda (l) ... )) ...
(minAbsoluteVal ’(-5 -3 -7 -10 12 8 7)) --> 3
Here are the map and reduce functions that need to be used:
(define map (lambda (f l) (if (null? l) '( ) ( cons ( f (car l) ) (map f (cdr l) ) ) ) ) )
map takes two arguments: a function and a list
map builds a new list by applying the function to every element of the (old) list
(define reduce (lambda ( op l id) ( if (null? l) id (op (car l) (reduce op (cdr l) id)) ) ) )
Reduce: Higher order function that takes a binary, associative operation and uses it to “roll up” a list
Explanation / Answer
The scripted code for the above question is as follows:-
//Using raw_input function to get the integers as input by users
// Using list function to convert the inputs to list of integers
m= raw_input();
n= list(m);
// map function for returning absolute value of list of integers
// map takes lambda function as one of the arguments where the lambda function converts each of the input values
//for list n to their absolute values. The second argument is the list n itself. The output of the map function will be
// absolute values of the input from the users stored in list n passed on to argument values. We are actually building
// the new list by applying the lambda function(to find absolute value) to each and every element of old list
var map = function(key,values)
{
lambda n: abs(n),n
};
// We are defining a reduce function reduce_absmin to find the minimum value in the reduced set of absolute values
// We are using an iterative method for finding the minimum absolute value in the list n. n now comprises of
// converted absolute values of the input from users. We initialise the smallest intial absolute value as the 1st
// value[0th index] of the list n which has been passed on to the argument values.We compare each of the absolute
// values in the list n with the smallest absolute value for each iteration
var reduce_absmin = function(key,values)
{
var intmin= values[0];
values.forEach(function(intval)
{
if (intval < intmin):
intmin = intval;
})
return intmin;
};
// Displaying the absolute minimum value
print ("Absolute minimum:" intmin);
Note: Comments and Assumptions : - Instead of using a user input list n we could mimic Lisp's car/cdr in python. We can use a list which represents Lisp's styled linked list and not a vector. Linked list equivalent is usually implemented in python using nested tuples.The function can be :-
def car(L):
return L[0]
def cdr(L):
return L[1]
def length(L):
if not L: return 0
return 1 + length(cdr(L))
L = (a,(b,None)) // represents Lisp's equivalent of [a,b] in python
length(L)
is O(n) //linear function to bind the order of magnitude
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.