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.
CODE:
divisors :: Integer -> [Integer] --function declaration
divisors x =[ y | y<-[1..x], x `mod` y ==0] --function definition
main = do
print(divisors 20) --calling a function
SCREENSHOT:
SAMPLE OUTPUT:
***Please upvote if it helps and feel free to comment if you have any query
Haskell Define function-divisors that receives a number and returns a list of all divisors of that...
In Haskell: Write a recursive function fibonacci that computes the n-th Fibonacci number. fibonacci :: Int -> Int Write a recursive function myProduct that multiplies all the numbers in a list. myProduct :: [Integer] -> Integer Using the technique of List Comprehension write a function that would remove even numbers from a list of lists. For Example: given input: [[1,2,3,4], [6,3,45,8], [4,9,23,8]] expected output: [[1,3], [3,45],[9,23]]. Show how the library function replicate :: Int -> a-> [a] that produces a...
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...
Write the function get_factors(n) that returns a list containing all of the divisors (without remainder) of the integer n. Use the built-in function range() to create the appropriate sequence over which to run the list comprehension. Here are some usage examples: >>> get_factors(10) [1, 2, 5, 10] >>> get_factors(2017) # a prime number [1, 2017] >>> get_factors(60) # highly non-prime! why we have 60 minutes per hour [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60] >>>...
Please help me with Haskell structure question. Thank you Define function least that receives three numbers and returns the least value, in the following cases. (a) Use min function. (b) Use if-expressions.
haskell code: divisableBy3 :: [a] -> [a] divisableBy3 v = filter odd(filter(\v -> v 'mod' 3 == 0)) Goal to filter all numbers in a list and then filter to keep only the ones divisbable by 3. getting an error: error: * Syntax error on 'mod' Perhaps you intended to use TemplateHaskell or TemplateHaskellQuotes * In the Template Haskell quotation 'mod' | 7 | divisableBy3 v = filter odd(filter(\v -> v 'mod' 3 == 0)) | ^^^^^ how to fix...
(IN HASKELL) Declare the type and define a function (say fun) that takes two whole numbers (say m and n) as input. The function then returns m ^ n. That is, m to the power of n. Please use recursion only, you should not use m ^ n, or pow or any other library function, only recursion.
USING SCHEME PROGRAMMING LANGUAGE define a function addSubList that processes a list of lists of numbers and adds up all sub-lists. The output of your function is a flat list of numbers. (You can assume that your function only receives valid input). Example: (define Q '(1 2 (3 4) 1 5 (7 8))) ;;;;;;;;;;;;;;;;;;;;; ; (addSubList Q) ; => '(1 2 7 1 5 15) ;;;;;;;;;;;;;;;;;;;
PYTHON Define a function reverseNestedList(...) which receives one possibly nested list (lst1) and returns the reverse of both the given list and any nested lists inside it. Note that the testing inputs will not include lists that are nested more than twice (a list in a list in a list ++...) [[[...],1], 1] Good luck! As an example, the following code fragment: lst1 = [[1, 2], [3, 4]] print(reverseNestedList(lst1)) should produce the output: [[4, 3], [2, 1]]
1-a-write a function that gets a number as a parameter and returns all its divisors (assume not more than 10 ) b- write a C program that reads two values from the above and prints their common divisors
In PYTHON: Write a function that receives a list of integers and returns the number of integers that are larger than the element immediately before them. For example, given [1,2,3,-5,0,-5,-10] the function should return 3 (since 1<2, 2<3, and -5<0).