A simple solution:
Use /Library/Haskell/bin/uninstall-hs to remove the installed packages
install the packages again
* Exception: Prelude.read: no parse
I got this exception when I tested this function with floating number and integer number
solveRPN' :: (Num a, Read a) => String -> a solveRPN' = head . foldl foldingFunction  . words where foldingFunction (x:y:ys) "*" = (x * y):ys foldingFunction (x:y:ys) "+" = (x + y):ys foldingFunction (x:y:ys) "-" = (y - x):ys foldingFunction xs numberString = read numberString:xs solveRPN' "10 4.0 3 + 2 * -" *** Exception: Prelude.read: no parse
By default, the ghc will deduce this function as (Num Int, Read Int). The error is casued by the Prelude.read function which cannot parse a "Floating number string" into Int. However, we can explicity specific the type of expression is Float. Integer string can be parsed into Floating number but Floating number cannot be parsed into Int.
read "4.0" :: Int *** Exception: Prelude.read: no parse read "4" :: Float 4.0 solveRPN' "10.0 4 3 + 2 * -" :: Float -4.0
Not in scope: `catch'
I am reading learn you a haskell for great good but i faced an error of Not in scope: `catch' when I read the exception section in input & output chapter.
The reason of this problem is the missing of catch function in the newer version of Prelude and System.IO.Error. In the older version of System.IO.Error, catch is defined as the same as catcahIOError. and it was preluded .
The code was the testing under older version of ghci
Prelude> :t catch catch :: IO a -> (IOError -> IO a) -> IO a Prelude> import System.IO.Error Prelude System.IO.Error> :t catchIOError catchIOError :: IO a -> (IOError -> IO a) -> IO a Prelude System.IO.Error> import Control.Exception Prelude System.IO.Error Control.Exception> :t catch <interactive>:1:1: Ambiguous occurrence `catch' It could refer to either `Control.Exception.catch', imported from `Control.Exception' (and originally defined in `Control.Exception.Base') or `Prelude.catch', imported from `Prelude' (and originally defined in `System.IO.Error')
However in the new version of the libraries, catch function has been removed. And the solution is simple, just replacing catch with catchIOError.
Edit: There is an alternative way to solve this problem. Use the catch function defined in Control.Exception.
catch :: Exception e => IO a -> (e -> IO a) -> IO aIt also catches the IOError, because IOError is an instance of type class Exception. (Interesting fact is that, catchIOError in System.IO.Error is actually implemented with catch in Control.Exception) However, if you just use Control.Exception to catch the exception and you do not import System.IO.Error package. You will lose the functions helping to get the attribute of IOError, such as ioeGetFileName and isDoesNotExistError.
I am reading the Learn you a Haskell for greate Good and I faced the problem of "attempting to use module ... which is not loaded" when I am at the end of chapter module, finally I got the solution on it
Steps of Solution
- Load the source code
- Set the module to Prelude
- Import the modules
Load the source code
Compile the source code into the GHCI
*Geometry.Cuboid> :l Geometry/Cube.hs Geometry/Sphere.hs [1 of 3] Compiling Geometry.Cuboid ( Geometry/Cuboid.hs, interpreted ) [2 of 3] Compiling Geometry.Cube ( Geometry/Cube.hs, interpreted ) [3 of 3] Compiling Geometry.Sphere ( Geometry/Sphere.hs, interpreted ) Ok, modules loaded: Geometry.Sphere, Geometry.Cuboid, Geometry.Cube.
Set the module to Prelude
Change back the current module in the GHCI to Prelude, by default the last module loaded becomes the current module.
*Geometry.Cube> :module Prelude
Import the modules
Import the modules we just loaded in the GHCI, and you can see the qualified name of the module will be append into the namespace.
Prelude> import qualified Geometry.Sphere as Sphere Prelude Sphere> import qualified Geometry.Cuboid as Cuboid Prelude Sphere Cuboid> import qualified Geometry.Cube as Cube Prelude Sphere Cuboid Cube>
1. Change a integer into string (actually it is a array of char) 2. Revese the string 3. Change the string into the number
reverseNumber :: Int -> Int reverseNumber n = read (reverse (show n)) :: Int reverseNumbers l = map (reverseNumber) l