exercise 1 Paragraph 144 Chapter 9-A Third Look at ML The fo expression for crea
ID: 3741178 • Letter: E
Question
exercise 1
Paragraph 144 Chapter 9-A Third Look at ML The fo expression for creating anonymous functions (using matches). The idea of higher-order functions The idea of currying. ? The long and short forms for writing curried functions. .The predefined, curried, higher-order functions map, folds, and foldl Exercises The first 25 exercises should all have one-line solutions using map, foldr or foldl You can also use other predefined functions, of course, but do not write any additional named functions and do not use explicit recursion. If you need helper functions, use anonymous ones. For example, if the problem says "write a function add2 that takes an int list and returns the same list with 2 added to every element," your answer should be fun add2 x map (fo aa +2)x You have seen some of these problems before. The trick now is to solve them in this new, concise form Exercise 1 Write a function i12rI of type int list- > real list that takes a list of integers and returns a list of the same numbers converted to type real. For Oxample, if you evaluate i12r1 [1,2, 3] you should get 1:02.0, 3.01 Exercise 2 Write a function ordlist of type char 1 ist-> int 1 ist that takes a list of characters and returns the list of the integer codes of those characters. For example if you evaluate ordlist "And # "b", # you should get [65.98, 671 .Explanation / Answer
Exercise1:-
Code(written inPython 2.7):-
def il2rI(int_list):
real_list=[]
for element in int_list: #select elements one by one from the integer list
real_list.append(float(element)) #convert element in float and append it to the real list
return real_list #this is your final list of the real numbers
int_list=[1,2,3]
real_list=il2rI(int_list)
print real_list
Explanation:-
Exercise2:-
(Code is written in Python 2.7)
def ordlist(char_list):
int_list=[]
for element in char_list: #select elements one by one from the character list
int_list.append(ord(element)) #convert element in its Ascii value and append it to the real list
return int_list #this is your final list of the Ascii values of the character list
char_list=['A','b']
int_list=ordlist(char_list)
print int_list
Explanation:-
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.