Question

(USING ML) (Using ML) Write a function dupList of type 'a list -> 'a list whose...

(USING ML)

(Using ML)

  1. Write a function dupList of type 'a list -> 'a list whose output list is the same as the input list but with each element of the input list repeated twice in a row. For example, if the input is [1, 2, 3], the output list should produce [1, 1, 2, 2, 3, 3]. If the input list [], the output list should be [].

    • Do not use explicit recursion but use one of the fold functions. Do not write any additional functions, dupList should be the only function.

Homework Answers

Answer #1

Q:- Write a function dupList of type 'a list -> 'a list whose output list is the same as the input list but with each element of the input list repeated twice in a row. For example, if the input is [1, 2, 3], the output list should produce [1, 1, 2, 2, 3, 3]. If the input list [], the output list should be [].

  • Do not use explicit recursion but use one of the fold functions. Do not write any additional functions, dupList should be the only function.

Answer:------
fun dupList l = foldr(fn (a, b) => a::a::b) [] l;

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
IN SML Write a function dupList of type 'a list -> 'a list whose output list...
IN SML Write a function dupList of type 'a list -> 'a list whose output list is the same as the input list but with each element of the input list repeated twice in a row. For example, if the input is [1, 2, 3], the output list should produce [1, 1, 2, 2, 3, 3]. If the input list [], the output list should be []. Do not use explicit recursion but use one of the fold functions. Do...
Use python to write a function that will generate the ith row of pascal’s triangle (using...
Use python to write a function that will generate the ith row of pascal’s triangle (using recursion) The method signature should be pascal(row). It should return a list of elements that would be in that row of Pascal’s Triangle. For example the 0th row is [1] and the 2nd row is [1,2,1]
using dr.racket programing language If we write a function that tests whether a list contains only...
using dr.racket programing language If we write a function that tests whether a list contains only strings, odd numbers, or even numbers, you will notice that the code that iterates through the list stays the same, with the only change being the predicate function that checks for the desired list element. If we were to write a new function for each of the tests listed above, it would be more error-prone and an example of bad abstraction. We could write...
Write in a c++ recursion function to prints the digits in reverse order using a recursion...
Write in a c++ recursion function to prints the digits in reverse order using a recursion function. Input: 23567 Output: 76532
Program has to be written in ML language. Write functions in ML to do the following:...
Program has to be written in ML language. Write functions in ML to do the following: 1. Given a list, return that list with its first and third elements deleted. Assume the length of the list is at least 3. 2. Given a list of real pairs, return the list containing the larger element in each pair. Examples: larger_in_pair([]) = [] larger_in_pair([(1.0,2.5),(4.7,3.6),(5.5,8.8)] = [2.5,4.7,8.8] 3. Given two lists of integers, return the list of the sums of the elements in...
In racket, implement a tail-recursive function called sum-pairs that creates a new list by adding the...
In racket, implement a tail-recursive function called sum-pairs that creates a new list by adding the elements of an input list in pairs. That is the first element of the resulting list is the sum of the first two elements of the input, the second element of the resulting list is the sum of the 3rd and 4th elements of the input, and so on. If there is an odd number of elements, then the last element remains unchanged. As...
***************PLEASE GIVE ANSWERS IN RACKET PROGRAMMING LANGUAGE ONLY****************** Write a recursive Racket function "update-if" that takes...
***************PLEASE GIVE ANSWERS IN RACKET PROGRAMMING LANGUAGE ONLY****************** Write a recursive Racket function "update-if" that takes two functions, f and g, and a list xs as parameters and evaluates to a list. f will be a function that takes one parameter and evaluates true or false. g will be a function that takes one parameter and evaluates to some output. The result of update-if should be a list of items such that if x is in xs and (f x)...
Write the function has duplicate() which takes as input a vector v and outputs true if...
Write the function has duplicate() which takes as input a vector v and outputs true if there is a re- peated element in v and false if there is no repeated elements. Below is the algorithm which you should implement. default output is 0 for elt1 in v: for elt2 later in v than elt1: if elt1==elt2, make output 1, break, end end end Checking if elt1==elt2 counts as one step. Setting the output value also counts as one step....
Write a function custom sort(v) that takes as input a vector v and as output returns...
Write a function custom sort(v) that takes as input a vector v and as output returns the vector w sorted into increasing order. For example, if the input is [−2 1 3 1 5], the output should be [−2 1 1 3 5]. Don’t use the built-in ”sort” function or anything similar. matlab question
Be able to write Python programming of a factorial of an integer (given input parameters, output...
Be able to write Python programming of a factorial of an integer (given input parameters, output characteristics and expected output behavior, do not use internal python functions for factorials), recall for example that: 5! = 5 ∗ 4 ∗ 3 ∗ 2 ∗ 1 = 120 and to continue asking unless user says to stop NOT USING FACTORIAL FUNCTION
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT