haskell

In Haskell, how do I get an average float from a list of integers?

天涯浪子 提交于 2020-05-17 07:44:09
问题 I have written a simple function that displays: Name of place, Degrees North, Degrees East, and a list of rainfall numbers. How do I get an average rainfall specific to a place? For example in my code, how do I get an average rainfall for London? Sorry if my code is not the best, I'm just learning Haskell. import Data.Char import Data.List type Place = (String, Float, Float, [Int]) testData :: [Place] testData = [("London", 51.5, -0.1, [0, 0, 5, 8, 8, 0, 0]), ("Cardiff", 51.5, -3.2, [12, 8,

Haskell sqrt type error

别说谁变了你拦得住时间么 提交于 2020-05-16 04:09:23
问题 OK, so I'm attempting to write a Haskell function which efficiently detects all the factors of a given Int . Based off of the solution given in this question, I've got the following: -- returns a list of the factors of n factors :: Int -> [Int] factors n = sort . nub $ fs where fs = foldr (++) [] [[m,n `div` m] | m <- [1..lim+1], n `mod` m == 0] lim = sqrt . fromIntegral $ n Sadly, GHCi informs me that there is No instance for (Floating Int) in the line containing lim = etc. etc. I've read

Making a datatype an instance of Functor to map on a field which is of parametric type

落花浮王杯 提交于 2020-05-16 04:07:51
问题 Follow up on this question about Learn You a Haskell for Great Good . The author, at the end of Chapter 8 declares this datatype (slighly simplified, I hope it's fine) data Barry t k p = BarryV p (t k) deriving (Show) and then makes it an instance of Functor instance Functor (Barry a b) where fmap f (BarryV x y) = BarryV (f x) y then concluding There we go! We just mapped the f over the first field. Yes. The first. So my question is: what if I want to map over, say, the second field? Actually

Making a datatype an instance of Functor to map on a field which is of parametric type

允我心安 提交于 2020-05-16 04:07:13
问题 Follow up on this question about Learn You a Haskell for Great Good . The author, at the end of Chapter 8 declares this datatype (slighly simplified, I hope it's fine) data Barry t k p = BarryV p (t k) deriving (Show) and then makes it an instance of Functor instance Functor (Barry a b) where fmap f (BarryV x y) = BarryV (f x) y then concluding There we go! We just mapped the f over the first field. Yes. The first. So my question is: what if I want to map over, say, the second field? Actually

Making a datatype an instance of Functor to map on a field which is of parametric type

守給你的承諾、 提交于 2020-05-16 04:06:12
问题 Follow up on this question about Learn You a Haskell for Great Good . The author, at the end of Chapter 8 declares this datatype (slighly simplified, I hope it's fine) data Barry t k p = BarryV p (t k) deriving (Show) and then makes it an instance of Functor instance Functor (Barry a b) where fmap f (BarryV x y) = BarryV (f x) y then concluding There we go! We just mapped the f over the first field. Yes. The first. So my question is: what if I want to map over, say, the second field? Actually

Haskell sqrt type error

一个人想着一个人 提交于 2020-05-16 04:04:46
问题 OK, so I'm attempting to write a Haskell function which efficiently detects all the factors of a given Int . Based off of the solution given in this question, I've got the following: -- returns a list of the factors of n factors :: Int -> [Int] factors n = sort . nub $ fs where fs = foldr (++) [] [[m,n `div` m] | m <- [1..lim+1], n `mod` m == 0] lim = sqrt . fromIntegral $ n Sadly, GHCi informs me that there is No instance for (Floating Int) in the line containing lim = etc. etc. I've read

How can I compare and return data using a list of data

会有一股神秘感。 提交于 2020-05-15 07:48:23
问题 I'm a newbie to Haskell and I'm struggling to find a way to use class member variables to return the member variable I am looking for. I have this data: data Place = Place {name :: String, north :: Float, east :: Float, rainfall :: [Int] } deriving (Eq, Ord, Show) testData :: [Place] testData = [ Place "London" 51.5 (-0.1) [0, 0, 5, 8, 8, 0, 0], Place "Norwich" 52.6 (1.3) [0, 6, 5, 0, 0, 0, 3], Place "Birmingham" 52.5 (-1.9) [0, 2, 10, 7, 8, 2, 2], Place "Hull" 53.8 (-0.3) [0, 6, 5, 0, 0, 0,

Hamming numbers and double precision

可紊 提交于 2020-05-15 02:09:43
问题 I was playing around with generating Hamming numbers in Haskell, trying to improve on the obvious (pardon the naming of the functions) mergeUniq :: Ord a => [a] -> [a] -> [a] mergeUniq (x:xs) (y:ys) = case x `compare` y of EQ -> x : mergeUniq xs ys LT -> x : mergeUniq xs (y:ys) GT -> y : mergeUniq (x:xs) ys powers :: [Integer] powers = 1 : expand 2 `mergeUniq` expand 3 `mergeUniq` expand 5 where expand factor = (factor *) <$> powers I noticed that I can avoid the (slower) arbitrary precision

Hamming numbers and double precision

≯℡__Kan透↙ 提交于 2020-05-15 02:09:07
问题 I was playing around with generating Hamming numbers in Haskell, trying to improve on the obvious (pardon the naming of the functions) mergeUniq :: Ord a => [a] -> [a] -> [a] mergeUniq (x:xs) (y:ys) = case x `compare` y of EQ -> x : mergeUniq xs ys LT -> x : mergeUniq xs (y:ys) GT -> y : mergeUniq (x:xs) ys powers :: [Integer] powers = 1 : expand 2 `mergeUniq` expand 3 `mergeUniq` expand 5 where expand factor = (factor *) <$> powers I noticed that I can avoid the (slower) arbitrary precision

Hamming numbers and double precision

老子叫甜甜 提交于 2020-05-15 02:08:07
问题 I was playing around with generating Hamming numbers in Haskell, trying to improve on the obvious (pardon the naming of the functions) mergeUniq :: Ord a => [a] -> [a] -> [a] mergeUniq (x:xs) (y:ys) = case x `compare` y of EQ -> x : mergeUniq xs ys LT -> x : mergeUniq xs (y:ys) GT -> y : mergeUniq (x:xs) ys powers :: [Integer] powers = 1 : expand 2 `mergeUniq` expand 3 `mergeUniq` expand 5 where expand factor = (factor *) <$> powers I noticed that I can avoid the (slower) arbitrary precision