2014年12月9日 星期二

Upgrade to maverick and crashes apache user directory.

I was using apache user directory for a long time. So that I can access my website like http://localhost/~jason/xxxx.html. After I upgrade to maverick 10.10. It restored my configuration file to default. Inside the default httpd.conf, the module for php and user directory were commented out. The working httpd.conf was renamed to httpd.conf~previous.   A simple solution is to rename httpd.conf~previous to httpd.conf.



2013年8月29日 星期四

Exception: Prelude.read: no parse

* 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

2013年8月16日 星期五

Not in scope: `catch'

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 a
It 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.

2013年6月14日 星期五

Solve the problem in ghci "attempting to use module ... which is not loaded"

Problem

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

  1. Load the source code
  2. Set the module to Prelude
  3. 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> 

2013年5月3日 星期五

Reverse a integer in haskell

Algorithm

1. Change a integer into string (actually it is a array of char)
2. Revese the string
3. Change the string into the number

Code

reverseNumber :: Int -> Int
reverseNumber n = read (reverse (show n)) :: Int

reverseNumbers l = map (reverseNumber) l

2013年4月3日 星期三

Delayed a email for a specific time.

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)

2013年3月20日 星期三

Finding row, column index of 2d array by giving the total number of item.

It's have been a long period of time I didn't update my blog.
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.

Solution:
total_item_index = n -1
row_index = total_item index / total_number_of_column
column_index = total_item_index / total_number_of_column