Question

In ML, define a function listsquare that when applied to the empty list of integers returns the empty list, and when applied to a non-empty list of integers return a list where each elenent is squared. For example, listsquares [2,3,4] returns [4,9,16]. Deine the function using a letlexpression in ML. val testList = [1,2,3,4,5] val testResults listsquare testList sti_-ex-4-6-4Tests. SnU-BoE し19 (Fundamental) kind of haskell function ,
0 0
Add a comment Improve this question Transcribed image text
Answer #1

fun squareAll(xs) =
   case xs of
       [] => []
   | (x::y) => [(x*x)] :: squareAll(y)
     
squareAll [3];

Add a comment
Know the answer?
Add Answer to:
kind of haskell function , In ML, define a function listsquare that when applied to the...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Haskell Define function-divisors that receives a number and returns a list of all divisors of that...

    Haskell Define function-divisors that receives a number and returns a list of all divisors of that number.For example, if the input is 20, then the output would be[1,2,4,5,10,20]. You may use list comprehension, list ranges, and function mod for this purpose.Hint:Given numbern, consider all numbers from 1 ton, and then keep only the ones that dividen.

  • a) 7% Define a function (the function definition only) named combine_lists that receives two lists of...

    a) 7% Define a function (the function definition only) named combine_lists that receives two lists of integers (the function has 2 parameters). You can assume that each list is the same length and each list is not empty. The function will create a new list which is composed of the addition of each element of the two lists. The function returns the new list. For example, if the function is sent this list: [2,4,6,8,10] and this list: [5,6,7,8,9] then the...

  • Define a function called collapse() which takes a list as input. Each element of the list...

    Define a function called collapse() which takes a list as input. Each element of the list will either be an integer, or a list of integers. The function should modify the input list by replacing all of the elements which themselves are lists with the sum of their elements. For example: Test Result vals = [1, 2, 3, 4, 5] collapse(vals) print(vals) [1, 2, 3, 4, 5] vals = [1, [2, 3], 4, 5] collapse(vals) print(vals) [1, 5, 4, 5]...

  • Define a function add_nums_x_pos_given_list_nums (..) which receives a list that is guaranteed to only have numbers...

    Define a function add_nums_x_pos_given_list_nums (..) which receives a list that is guaranteed to only have numbers (or the empty list) and returns the sum of each number multiplied by the position where the number is. (In the case of the empty list the function it will return 0) For example add_nums_x_pos_given_list_nums ([10,20,30]) will return the value 80 which is the result of adding: 0 * 10 (10 is in position 0 in the list) + 1 * 20 + 2...

  • Haskell Programming 2. Returns the plural of a word by adding "s' unless the word ends...

    Haskell Programming 2. Returns the plural of a word by adding "s' unless the word ends in "y", in which can you remove the "y" and add "ies" 3. Write a function that takes two lists and returns a list of only those elements that appear in both lists. 4. Write a function that takes a list and an element and returns the number of times the element appears in the list. 5. Remove the leading spaces from a string...

  • Please use Haskell, thank you! For this question, you will implement (under specific constraints) a recursive...

    Please use Haskell, thank you! For this question, you will implement (under specific constraints) a recursive function in Haskell that takes a list of characters as an argument and returns a list that contains only every third element from the argument list in the same order. As a clarifying example, this function, when passed the argument list “ABCDEFGHIJKLMNO”, should return the list “ CFILO”. It should also be noted that your function must work (i.e., not terminate with an error)...

  • In Ocaml Define the function everyNth : ('a list) -> int -> ('a list) that will...

    In Ocaml Define the function everyNth : ('a list) -> int -> ('a list) that will take a list and a positive number i and produce a new list that has every element whose position in the given list is a multiple of i. Some example outputs for the function follow: # everyNth [1;2;3;4] 2;; - : int list = [2; 4] # everyNth [1;2;3;4] 1;; - : int list = [1; 2; 3; 4] # everyNth [1;2;3;4;5;6] 3;; -...

  • IN HASKELL func5 :: (Int -> Int) -> Int -> (Int, Int) func5 f x =...

    IN HASKELL func5 :: (Int -> Int) -> Int -> (Int, Int) func5 f x = (x, f x) Using map and the function above, write a function that takes a list of Ints and a function (Int -> Int) and returns a new list of tuples, with the first element being the original element, and the second being the result of the transformation. e.g. [1,2,3] becomes [(1,1), (2, 4), (3, 9)] if the function passed in returns the square...

  • In Haskell programming language (1) Write a "averageThree" function to return the average of three integers....

    In Haskell programming language (1) Write a "averageThree" function to return the average of three integers. averageThree :: Integer -> Integer -> Integer -> Float (2) Write a "howManyAboveAverage" function which returns how many of three integer inputs are above its average value. (utilize the averageThree function.) howManyAboveAverage :: Integer -> Integer -> Integer -> Integer (3) Write a "howManyWithinThreshold" function that returns how many of the first three arguments are within the threshold (the fourth argument) of the average...

  • 20. [5 points] Rewrite the following ML. function using patterns fun factn. ifn-0 then 1 else n fact (n-1) 21. [S points) Using map function, write an ML function int2real of type int list real l...

    20. [5 points] Rewrite the following ML. function using patterns fun factn. ifn-0 then 1 else n fact (n-1) 21. [S points) Using map function, write an ML function int2real of type int list real list that takes a list of integers and returns the same numbers converted to real. For example, if the input is [1,2,3], the output should be like [1.0,2.0,3.0 22. [5 points] Using foldr function, write a function duplist of type·a list 'a list that takes...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT