* 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
In the previous tutorial about a half year ago, I talked about how to create a simple delayed email with the great gem, delayed job. In the tutorial, the delayed duration is a constant for every object. But if you want to do this based on different condition, say the attribute of the delayed object. You can simply create a new Job Class and enquene the job with delay.
class NewJob def initialize(job) @job = job end def perform Notifier.job_created(@job).deliver end end
In the controller, you can enqueue the job to run at a specific time. I have used random number to simulate the random situation of the delayed time of each job.
delayed_time = Random.rand(1..10).minutes.from_now Delayed::Job.enqueue(NewJob.new(@job) , :run_at => delayed_time)
I have found a interesting problem that I thought it's very complicated.
Supposed you are given a 2D array, the number of row and column is known.
The problem is to find the row, column index of nth item in the 2D array.
total_item_index = n -1
row_index = total_item index / total_number_of_column
column_index = total_item_index / total_number_of_column