Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: Runtime error “Could not deduce (Integral Float) arising from a use of..” (Jack Vice) 2. Hutton ex 7.7 and 7.8 (trent shipley) ---------------------------------------------------------------------- Message: 1 Date: Thu, 23 Aug 2018 10:35:22 -0400 From: Jack Vice <jack.v...@gmail.com> To: beginners@haskell.org Subject: Re: [Haskell-beginners] Runtime error “Could not deduce (Integral Float) arising from a use of..” Message-ID: <CALAZO9c_NuLqozN_h8wF-09dfoQiMhSLSJE=niQst=w6qkp...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" David and Kyle, Thank you both. I declared the types and added a 'fromIntegral' and got things working! On Thu, Aug 23, 2018 at 8:35 AM David McBride <toa...@gmail.com> wrote: > Floating is the type class of types that are floating point. The two most > common instances are Float and Double. Integral is the class of integer > types. Most commonly Int and Integer. > > stdDev > :: (Floating a1, Floating a, Integral a1) => > > When you see a type like that it means type a1 is both a Floating and also > an Integral. Intellectually that is impossible, but as far as ghc is > concerned there could be a type that is an instance of both classes, so it > allows it. But when you try to call it, there's no type in scope that you > can affix to a1 to please it, so it will always error. > > The reason it has both Floating and Integral on the same type is that you > are using several functions on various arguments of your function that > imply that the types of those arguments must be instances of various > classes. > > fromIntegral :: (Integral a, Num b) => a -> b > sumOfMinusMeans :: (Eq t, Floating t) => Int -> t -> [[t]] -> t > sqrt :: Floating a => a -> a -- (this function may not be a factor) > > I strongly recommend you write out a type for stdDev, but fix a1 to a > concrete type and then ghc can tell you why that type won't work. > stdDev :: Int -> Int -> [Double] -> [[Double]] -> [Double] > > • No instance for (Integral Double) > arising from a use of ‘fromIntegral’ > > stdDev :: Int -> Int -> [Integer] -> [[Integer]] -> [Double] > > • No instance for (Floating Integer) > arising from a use of ‘sumOfMinusMeans’ > > And then think really hard about what types you want stdDev to accept. > Rework its the definition until the compiler is happy. I suspect it is an > extraneous fromIntegral. > > On Thu, Aug 23, 2018 at 6:26 AM, Jack Vice <jack.v...@gmail.com> wrote: > >> I am trying to calculate the standard deviation for each index in a list >> of lists of floats. Using Ubuntu 18.04, ghc 8.0.2. I am getting the >> following runtime error which I have googled and still don't understand >> what "Integral Float" is exactly or even which parameter is causing the >> trouble. >> >> *Main> let z = stdDev 0 2 y x >> <interactive>:250:9: error: >> • Could not deduce (Integral Float) arising from a use of ‘stdDev’ >> from the context: Floating a >> bound by the inferred type of z :: Floating a => [a] >> at <interactive>:250:5-38 >> • In the expression: stdDev 0 (length (head (x))) y x >> In an equation for ‘z’: z = stdDev 0 (length (head (x))) y x >> >> Code: >> >> -- i is start index, l is length of each list, ms is list of means, >> -- xs is Matrix >> stdDev i l ms xs >> | i < l = sqrt(fromIntegral(sumOfMinusMeans i (ms!!i) xs) / >> fromIntegral(l)):(stdDev (i+1) l ms xs) >> | otherwise = [] >> >> --i is index, m is mean for the index >> sumOfMinusMeans i m (x:xs) >> | xs == [] = (x!!i - m)**2 >> | i < length x = (x!!i - m)**2 + (sumOfMinusMeans i m xs) >> | otherwise = 0 >> >> Types: >> >> *Main> :t stdDev >> stdDev >> :: (Floating a1, Floating a, Integral a1) => >> Int -> Int -> [a1] -> [[a1]] -> [a] >> >> *Main> :t sumOfMinusMeans >> sumOfMinusMeans :: (Eq t, Floating t) => Int -> t -> [[t]] -> t >> >> Variables: >> >> *Main> y >> [380.0,1.0] >> *Main> x >> [[600.0,1.0],[400.0,1.0],[170.0,1.0],[430.0,1.0],[300.0,1.0]] >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> >> > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180823/f118bf97/attachment-0001.html> ------------------------------ Message: 2 Date: Fri, 24 Aug 2018 02:45:46 -0700 From: trent shipley <trent.ship...@gmail.com> To: Haskell Beginners <beginners@haskell.org> Subject: [Haskell-beginners] Hutton ex 7.7 and 7.8 Message-ID: <CAEFLybKmC+36syVRwjPJxHfghxmwwrxjQf5A4MuQYbaKr=m...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" My attempt not only compiled, but seems to run OK. (Really, a just gave it a couple of trivial tests, and said good enough. I didn't really mess with corner cases like empty lists. It was late, and I didn't want to tempt fate.) THUS, I am mostly looking for style feedback, although if there are any obvious logic errors, I'd be "happy" to learn about those too. {-- 7. Modify the binary string transmitter example to detect simple transmission errors using the concept of parity bits. That is, each eight-bit binary number produced during encoding is extended with a parity bit, set to one if the number contains an odd number of ones, and to zero otherwise. In turn, each resulting nine-bit binary number consumed during decoding is checked to ensure that its parity bit is correct, with the parity bit being discarded if this is the case, and a parity error being reported otherwise. Hint: the library function error :: String -> a displays the given string as an error message and terminates the program; the polymorphic result type ensures that error can be used in any context. 8. Test your new string transmitter program from the previous exercise using a faulty communication channel that forgets the first bit, which can be modelled using the tail function on lists of bits. Hutton, Graham. Programming in Haskell (Kindle Locations 2842-2851). Cambridge University Press. Kindle Edition. --} import Data.Char type Bit = Int byte :: Int byte = 8 parityByte :: Int parityByte = 9 bin2int :: [Bit] -> Int bin2int = foldr (\x y -> x + 2 * y) 0 -- Hutton, Graham. Programming in Haskell (Kindle Locations 2647-2649). Cambridge University Press. Kindle Edition. int2bin :: Int -> [Bit] int2bin 0 = [] int2bin n = n `mod` 2 : int2bin (n `div` 2) -- Hutton, Graham. Programming in Haskell (Kindle Locations 2654-2656). Cambridge University Press. Kindle Edition. make8 :: [Bit] -> [Bit] make8 bits = addParity (take byte (bits ++ repeat 0)) -- Parity functions addParity :: [Bit] -> [Bit] addParity xs = if even (sum xs) then xs ++ [0] else xs ++ [1] checkParity :: [Bit] -> Bool checkParity xs = (((even . sum) (take ((length xs) - 1) xs)) == ((even . last) xs)) || (((odd . sum) (take ((length xs) - 1) xs)) == ((odd . last) xs)) errorParity :: [Bit] -> ([Bit], Bool) errorParity xs = if checkParity xs then (xs, checkParity xs) else error "Parity error" dropParity :: [Bit] -> [Bit] dropParity xs = take ((length xs) - 1) xs -- Hutton, Graham. Programming in Haskell (Kindle Locations 2662-2663). Cambridge University Press. Kindle Edition. -- TRANSMISSION encode :: String -> [Bit] encode = concat . map (make8 . int2bin . ord) -- Hutton, Graham. Programming in Haskell (Kindle Locations 2673-2675). Cambridge University Press. Kindle Edition. chop8 :: [Bit] -> [[Bit]] chop8 [] = [] chop8 bits = (dropParity . fst . errorParity) (take parityByte bits) : chop8 (drop parityByte bits) -- Hutton, Graham. Programming in Haskell (Kindle Locations 2681-2683). Cambridge University Press. Kindle Edition. decode :: [Bit] -> String decode = map (chr . bin2int) . chop8 -- Hutton, Graham. Programming in Haskell (Kindle Locations 2686-2688). Cambridge University Press. Kindle Edition. -- channel :: [Bit] -> [Bit] -- channel = id channel :: [Bit] -> [Bit] channel = tail -- Hutton, Graham. Programming in Haskell (Kindle Locations 2696-2697). Cambridge University Press. Kindle Edition. transmit :: String -> String transmit = decode . channel . encode -- Hutton, Graham. Programming in Haskell (Kindle Locations 2694-2695). Cambridge University Press. Kindle Edition. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180824/0a8f7614/attachment.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 122, Issue 15 ******************************************