[Solved] How to write this function in point-free style?
Another solution (needs import of Control.Applicative): between min max = liftA2 (&&) (min <) (max >) 1 solved How to write this function in point-free style?
Another solution (needs import of Control.Applicative): between min max = liftA2 (&&) (min <) (max >) 1 solved How to write this function in point-free style?
It’ll be perfectly safe with a smart constructor and stored dimensions. Of course there are no natural implementations for the operations signum and fromIntegral (or maybe a diagonal matrix would be fine for the latter). module Matrix (Matrix(),matrix,matrixTranspose) where import Data.List (transpose) data Matrix a = Matrix {matrixN :: Int, matrixM :: Int, matrixElems :: … Read more
import Control.Monad (replicateM) main :: IO () main = mapM_ print . map (uncurry (+)) =<< flip replicateM readIntPair =<< readLn readIntPair :: IO (Integer, Integer) readIntPair = do x <- readLn y <- readLn return (x, y) replicateM is from Control.Monad, the other functions are imported automatically from the Prelude. You will also want … Read more
By using ghci and simply inputing the code snippets you will see which of the values are accepted: Prelude> data Bin = B Bin | C [ Int] Prelude> a = C [ ] Prelude> b = B ( B C [ 2]) <interactive>:3:9: error: • Couldn’t match expected type ‘[Integer] -> Bin’ with actual … Read more
You can use the :t command in ghci to find out the type of any expression. GHCi, version 7.10.3: http://www.haskell.org/ghc/ 😕 for help Prelude> :t flip foldr id flip foldr id :: Foldable t => (a -> (a1 -> a1) -> a1 -> a1) -> t a -> a1 -> a1 solved Get the type … Read more
I think you’re looking for the inverse of this function conv = concat . f where f [] = [] f (x:xs) = map (x+) xs : f xs > conv [3,10,5] [13,8,15] > conv [2,5,15,34] [7,17,36,20,39,49] I came up something like this. Note that for length less than 3 there can’t be a unique … Read more
Here are a few hints foldAEB2 Nil Un Conc is the identity function foldAEB2 Nil Un Conc :: AEB2 a -> AEB2 a Un :: a -> AEB2 a \a -> Conc (Un a) (Un a) :: a -> AEB2 a 4 solved Function double in haskell
hof f list1 list2 = zipWith f list1 list2 f1 list1 list2 = hof (+) list1 list2 f2 list1 list2 = hof (++) list1 list2 f3 list1 list2 = hof (*) list1 list2 f4 list1 list2 = hof func list1 list2 where func x _ = x+42 f5 list1 list2 = hof func list1 list2 … Read more
You have a couple of typos in your imports. I think you mean: import Data.List import System.IO primeNumbers = [3,5,7,11] morePrime = primeNumbers ++ [13,17,19] i.e. Data.list should be Data.List and Syste.IO should be System.IO. 3 solved I cannot execute my Haskell code [closed]
Either isn’t simply a union of types; it’s a tagged union, which means every value has to explicitly specify which “side” of the type the wrapped value occurs on. Here’s an example, (with a Show instance derived for your Card type): *Main> Card Hearts Jack <interactive>:3:13: error: • Couldn’t match type ‘Court’ with ‘Either Pip … Read more
There are following approaches. 1) Create a list pf pairs which are all combinations [(10,10),(10,15),..,(15,10),(15,3)..]. Now you can use simple any function on this list to check if any pair add up to given number. getCoupleList :: [a]->[(a,a)] getCoupleList [] = [] getCoupleList [x] = [] getCoupleList (x:xs) = map (\y->(x,y)) xs ++ getCoupleList xs … Read more
It works like a foreach loop, so foreach (x in [1..3]) { foreach (y in [x .. x * 2]) { yield y; } } First x is 1, so y in [1 .. 2] Then x is 2, so y in [2 .. 4] Then x is 3, so y in [3 .. 6] … Read more
I suppose the aim of the assignment is to teach you about list comprehensions, filter and similar constructs, and not to have you write functions that test for primality or create the list of divisors in any sensible way. Therefore what you need is a predicate divides, divides :: Int -> Int -> Bool a … Read more
I think you might be looking for something like this: combine :: Int ->Int -> (Int,Int,Int) combine n m = (x1, y1, gcd n m) where (x1, y1) = gcdext n m gcdext :: Int -> Int -> (Int, Int) gcdext n m = gcdexthelper n m 1 0 0 1 where gcdexthelper n m … Read more
Your call to and_ does not see director and “Brad Bird” directly; it gets the function that results from applying director to “Brad Bird” (and similarly for the second argument. and_ :: (Movie -> Bool) -> (Movie -> Bool) -> Movie -> Bool and_ f1 f2 (_, rank, dir) | … The bigger problem is … Read more