haskell

Haskell filtering a nested list with specific data constructors

拜拜、爱过 提交于 2019-12-31 07:43:52
问题 Suppose I have the data type data Joke = Funny String | Lame String and say I have the following nested list [[Funny "Haha", Lame "boo"], [Funny "Haha"], [Lame "BOO"]] How would I go about filtering such a nested list so that any list within the nested list that contains Funny "Haha" is removed? In other words, I'm trying to filter the list so that I receive the following result: [[Lame "BOO"]] Any list that contains Funny "Haha" is removed. Would appreciate any help, I'm having a terrible

Sequential numbers in a list haskell

懵懂的女人 提交于 2019-12-31 07:38:11
问题 I am new to haskell and I was attempting a few coding problems that I previously completed for java, however the following problem has me stumped. Basically the idea is to write a function that takes in a list of integers ([Int]) establishes whether a list of integers has consecutive 1's within it. For example the output of the following would be: Input: func [0,0,1,1,0] Output: True A sample solution for this problem in haskell would be greatly appreciated, Thanks 回答1: One way would be to

Which is the type of (flip .)?

我们两清 提交于 2019-12-31 07:37:05
问题 I'm trying to understand why the type of: (flip .) is: (a -> a1 -> b -> c) -> a -> b -> a1 -> c First of all, the type of: flip: is (a -> b -> c) -> b -> a -> c (.): is (b -> c) -> (a -> b) -> a -> c I will rename the variables to be more clear in my explanation, so the types: flip: is (ax -> bx -> cx) -> bx -> ax -> cx (.): is (by -> cy) -> (ay -> by) -> ay -> cy Then I try substituing like this: ax = (by -> cy) bx = (ay -> by) cx = ay -> cy So the resulting type is: (ay -> by) (by -> cy) ->

Which is the type of (flip .)?

人盡茶涼 提交于 2019-12-31 07:37:03
问题 I'm trying to understand why the type of: (flip .) is: (a -> a1 -> b -> c) -> a -> b -> a1 -> c First of all, the type of: flip: is (a -> b -> c) -> b -> a -> c (.): is (b -> c) -> (a -> b) -> a -> c I will rename the variables to be more clear in my explanation, so the types: flip: is (ax -> bx -> cx) -> bx -> ax -> cx (.): is (by -> cy) -> (ay -> by) -> ay -> cy Then I try substituing like this: ax = (by -> cy) bx = (ay -> by) cx = ay -> cy So the resulting type is: (ay -> by) (by -> cy) ->

How to parse a Tuple (String,Int) in Haskell using parsec

筅森魡賤 提交于 2019-12-31 07:31:47
问题 I think i already managed to parse strings to strings and strings to Ints but I also need to parse a (String,Int) type as userRatings is in order to read from a textFile correctly and I am using Parsec This is the Parsing along with the import import Text.Parsec ( Parsec, ParseError, parse -- Types and parser , between, noneOf, sepBy, many1 -- Combinators , char, spaces, digit, newline -- Simple parsers ) -- Parse a string to a string stringLit :: Parsec String u String stringLit = between

How to parse a Tuple (String,Int) in Haskell using parsec

核能气质少年 提交于 2019-12-31 07:31:45
问题 I think i already managed to parse strings to strings and strings to Ints but I also need to parse a (String,Int) type as userRatings is in order to read from a textFile correctly and I am using Parsec This is the Parsing along with the import import Text.Parsec ( Parsec, ParseError, parse -- Types and parser , between, noneOf, sepBy, many1 -- Combinators , char, spaces, digit, newline -- Simple parsers ) -- Parse a string to a string stringLit :: Parsec String u String stringLit = between

Testing if an inputted Int is a perfect number

白昼怎懂夜的黑 提交于 2019-12-31 07:27:06
问题 I've been looking into perfect numbers, and I found this interesting bit of code on rosettacode: perfect n = n == sum [i | i <- [1..n-1], n `mod` i == 0] Now, I understand what a perfect number is, and I know which numbers are considered perfect, however I'm struggling to work out which parts of this code do what. I understand that it's working out the factors of the inputted number, and combining them together to see if it matches the input itself, but I'm not sure how it's doing this. If

identifying number of words in a paragraph using haskell

北城以北 提交于 2019-12-31 07:06:06
问题 I am new to Haskell and functional programing. I have a .txt file which contains some paragraphs. I want to count the number of words in each paragraph, using Haskell. I have written the input/output code paragraph-words:: String -> int no_of_words::IO() no_of_words= do putStrLn "enter the .txt file name:" fileName1<- getLine text<- readFile fileName1 let wordscount= paragraph-words text Can anyone help me to write the function paragraph-words. which will calculate the number of words in each

Haskell use multiple functions inside of another function

混江龙づ霸主 提交于 2019-12-31 07:00:47
问题 I'm creating a program to compute the solution to a cubic equation in haskell. I'm new to functional languages and I'm having some difficulties. Here is my code: cubicQ :: Float -> Float -> Float -> Float cubicQ a b c = ((3 * a * c) - (b**2)) / (9 * (a**2)) --calculates Q cubicR :: Float -> Float -> Float -> Float -> Float cubicR a b c d = ((9 * a * b * c) - (27 * a**2 * d) - (2 * b**3)) / (54 * a**3)--calculates R cubicS :: Float -> Float -> Float cubicS r q = (r + (sqrt(q**3 + r**2))) ** (1

Show instance for matrix in Haskell

两盒软妹~` 提交于 2019-12-31 05:23:11
问题 I've been trying to create a show instance in order to visualize a given matrix and also, to create an outline with columns around and in between the matrix. What I managed to accomplish so far is the following: data Mat a = Mat [[a]] instance (Show a) => Show (Mat a) where show (Mat x) = "\n" ++ " ---\n"++unlines ( map (\r -> showRow r ++ "\n ---") x ) ++ "\n" where showRow list = "¦ "++unwords ( map (\v -> show v ++" ¦") list) Assuming we have a matrix Mat [[1,2,3],[4,5,6]] that we would