(IN HASKELL) Declare the type and define a function (say fun) that takes a list
ID: 3672902 • Letter: #
Question
(IN HASKELL)
Declare the type and define a function (say fun) that takes a list of natural numbers (say ns) and a number (say n) as input. Then the program returns a list of numbers that contains only the multiples of number n from the input list. for instance, fun [11, 12, 4, 9, 15, 21, 3, 5] 3 gives this list back [12, 9, 15, 21, 3] because they are multiple of 3.
Write two versions of this program: in one version use list comprehension technique, and in the other version use recursion.
Explanation / Answer
(IN HASKELL)
Declare the type and define a function (say fun) that takes a list of natural numbers (say ns) and a number (say n) as input. Then the program returns a list of numbers that contains only the multiples of number n from the input list. for instance, fun [11, 12, 4, 9, 15, 21, 3, 5] 3 gives this list back [12, 9, 15, 21, 3] because they are multiple of 3.
Write two versions of this program: in one version use list comprehension technique, and in the other version use recursion.
Ans:
comprehension technique:
Fun : : [Integer] ->[Integer] - > Integer
Fun ns n [x x < - ns, x ‘mod’ n]
using recursion.
Fun : : [Integer] ->[Integer] - > Integer
Fun ns n filter(ns ‘mod’ n)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.