Question
1{- Counting 2 - 3 - Provides functions for counting and combinatorics. When pattern 4 - matching is possible, prefer it over if-then-else. 5 -}
1{- Counting 2 - 3 - Provides functions for counting and combinatorics. When pattern 4 - matching is possible, prefer it over if-then-else. 5 -} 6 7 8 {- Binomial Coefficients 9 - 10 - Define c n k using the recurrence relation we discussed in class 11 - for binomial coefficients. Do not use factorials to define this 12 - function 13 -} 14 --comb :: Integer -> Integer -> Integer 15 16 17 {- Factorial 18 - 19 - Recursively define the factorial function. For safety, please guard 20 - against infinite recursion by defining fact n = 1 if n is negative. 21 -} 22 --fact :: Integer -> Integer 23 24 25 {- Permutations 26 - 27 - Define p n k so that it returns the number of possible k-permutations 28 - of n objects. You may use factorials for this definition. Remember 29 - to use `div` instead of the / operator when dividing integers. 30 -} 31 --perm :: Integer -> Integer -> Integer 32 33 34 {- Pascal's Triangle 35 - 36 - Define pascal n so that it returns the nth row of Pascal's Triangle as 37 - a list. e.g., pascal 2 should return [1,2,1]. 38 - 39 - This can be done without any of the built-in library functions that we 40 - have not discussed. Do not use map, zip, zipWith, fold, etc. 41 - 42 - Do use the neighborsums function. The neighborsums function should 43 - take a list and return a new list whose elements are the sums of 44 - adjacent elements in the original list. e.g., neighborsums [1,2,3] 45 - should return [3,5]. 46 -} 47 48 --neighborsums :: [Integer] -> [Integer] 49 50 --pascal :: Integer -> [Integer]
Please use haskell to write program for those counting problems.
This is instruction.
Coding part should go here. Use Haskell please
Gountang 3 Open the file counting.hs and define each of the funct ons beginning with 4 comio. comb nk fact n perm nk neighborsums xs pascal n The types of each function have been provided but commented out so that 13 you can load the file to test your code as you go. Uncomment the function 14 type for each function you work on as you go. The file will not load 15 if you do not provide a function definition for each uncommented function 16 type. Start the Haskell interpreter with the command ghci Once inside the interpreter, load your code file with 24 25 :load counting 26 You can then test your function definitions by typing expressions such as 29 comb 35 (comb 3 5) + (comb 3 4) 30 fact B (fact 5) 'div (fact 3) 32 33 Note that when dividing integers, you must use the 'div operator instead 34 of the / operator. 36 to leave the interpreter, use the command quit Count-ng Provides functions for counting and combinatorics matching is possible, prefer it over if-then-else. When pattern Binomial Coefficients 1 Define cnk using the recurrence relation we discussed in class for binomial coefficients. Do not use factorials to define this function --comb :: Integer -> Integer -> Integer 17 - Factorial - Recursively define the factorial function. For safety, please guard against infinite recursion by defining fact n - 1 if n is negative, 20 - - 22 --fact :: Integer -> Integer 25 - Permutations IIIII Define pak so that it returns the number of possible k-permutations of n objects. You may use factorials for this definition. Remember - to use "div instead of the / operator when dividing integers. --perm :: Integer -> Integer -> Integer Pascal's Triangle 36 - Define pascal n so that it returns the nth row of Pascal's Triangle as a list. e.g., pascal 2 should return [1,2,1). 39 - This can be done without any of the built-in library functions that we have not discussed. Do not use map, zip, zipwith, fold, etc. Do use the neighborsums function. The neighborsums function should take a list and return a new list whose elements are the sums of adjacent elements in the original list. e.g., neighborsums [1,2,3] should return [3,5). -) 48 --neighborsums :: (Integer) -> [Integer] 17 (- Factorial 19 - Recursively define the factorial function. For safety, please guard 20 - against Infinite recursion by defining fact 1 is negative. 22 -fact 11 Integer -> Integer 25 - Permutations 26 27 - Define D nk so that it returns the number of possible k-permutations 28 - of objects. You may use factorials for this definition. Remember 29 - to use 'div instead of the operator when dividing Integers. 30 -1 31 --perm :: Integer -> Integer -> Integer 34 - Pascal's Triangle 36 - Define pascal so that it returns the nth row of Pascal's Triangle as 37 - a list. e... pascal 2 should return (1,2,1). 40 - This can be done without any of the built-in library functions that we have not discussed. Do not use map, zip, zipwith, fold, etc. 42 - 43 - 44 - 45 - 46 Do use the neighborsus function. The neighborsus function should take a list and return a new list whose elements are the sums of adjacent elements in the original list. 2.9., neighborsums (1,2,3) should return 13,5). 48 neighborsums :: (Integer] -> [Integer] 50 --pascal :: Integer -> [Integer] Gountang 3 Open the file counting.hs and define each of the funct ons beginning with 4 comio. comb nk fact n perm nk neighborsums xs pascal n The types of each function have been provided but commented out so that 13 you can load the file to test your code as you go. Uncomment the function 14 type for each function you work on as you go. The file will not load 15 if you do not provide a function definition for each uncommented function 16 type. Start the Haskell interpreter with the command ghci Once inside the interpreter, load your code file with 24 25 :load counting 26 You can then test your function definitions by typing expressions such as 29 comb 35 (comb 3 5) + (comb 3 4) 30 fact B (fact 5) 'div (fact 3) 32 33 Note that when dividing integers, you must use the 'div operator instead 34 of the / operator. 36 to leave the interpreter, use the command quit Count-ng Provides functions for counting and combinatorics matching is possible, prefer it over if-then-else. When pattern Binomial Coefficients 1 Define cnk using the recurrence relation we discussed in class for binomial coefficients. Do not use factorials to define this function --comb :: Integer -> Integer -> Integer 17 - Factorial - Recursively define the factorial function. For safety, please guard against infinite recursion by defining fact n - 1 if n is negative, 20 - - 22 --fact :: Integer -> Integer 25 - Permutations IIIII Define pak so that it returns the number of possible k-permutations of n objects. You may use factorials for this definition. Remember - to use "div instead of the / operator when dividing integers. --perm :: Integer -> Integer -> Integer Pascal's Triangle 36 - Define pascal n so that it returns the nth row of Pascal's Triangle as a list. e.g., pascal 2 should return [1,2,1). 39 - This can be done without any of the built-in library functions that we have not discussed. Do not use map, zip, zipwith, fold, etc. Do use the neighborsums function. The neighborsums function should take a list and return a new list whose elements are the sums of adjacent elements in the original list. e.g., neighborsums [1,2,3] should return [3,5). -) 48 --neighborsums :: (Integer) -> [Integer] 17 (- Factorial 19 - Recursively define the factorial function. For safety, please guard 20 - against Infinite recursion by defining fact 1 is negative. 22 -fact 11 Integer -> Integer 25 - Permutations 26 27 - Define D nk so that it returns the number of possible k-permutations 28 - of objects. You may use factorials for this definition. Remember 29 - to use 'div instead of the operator when dividing Integers. 30 -1 31 --perm :: Integer -> Integer -> Integer 34 - Pascal's Triangle 36 - Define pascal so that it returns the nth row of Pascal's Triangle as 37 - a list. e... pascal 2 should return (1,2,1). 40 - This can be done without any of the built-in library functions that we have not discussed. Do not use map, zip, zipwith, fold, etc. 42 - 43 - 44 - 45 - 46 Do use the neighborsus function. The neighborsus function should take a list and return a new list whose elements are the sums of adjacent elements in the original list. 2.9., neighborsums (1,2,3) should return 13,5). 48 neighborsums :: (Integer] -> [Integer] 50 --pascal :: Integer -> [Integer]Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started