Oct 19, 2009

Amazon.com Larger Font Bookmarklet

I like to read book reviews in Amazon.com with iPhone.
Amazon.com show optimized pages for iPhone.
I think its font size is so small.
Then I made a bookmarklet.

Amazon.com larger font bookmarklet

You can view pages with larger font when you run this bookmarklet.


  1. var e = document.getElementsByTagName('div');  
  2.   
  3. var n = e.length;  
  4.   
  5. for (var i = 0; i < n; i++) {  
  6.   
  7.    e[i].style.fontSize = '14px';  
  8.   
  9. }  

Oct 12, 2009

Making Web Application with Haskell #3

I make a web application with Haskell.
It's a simple application.
You input your name, then the application output your name.
See below.






The other day I made the application with Network.CGI.
Today I make it with Text.XHtml wich is included in GHC.


  1. import Network.CGI  
  2. import Text.XHtml  
  3.   
  4. inputForm :: Html  
  5. inputForm = form << [paragraph << "Who are you?",  
  6.                      textfield "name",  
  7.                      submit "" "send"]  
  8.   
  9. greet :: String -> Html  
  10. greet n = h1 << ("Hello, " ++ n ++ "!")  
  11.   
  12. page :: String -> Html -> Html  
  13. page t b = header << thetitle << t +++ body << b  
  14.   
  15. cgiMain :: CGI CGIResult  
  16. cgiMain = do name <- getInput "name"  
  17.              let x = maybe inputForm greet name  
  18.              output $ renderHtml $ page "Hello" x  
  19.   
  20. main :: IO ()  
  21. main = runCGI $ handleErrors cgiMain  



This code is simpler than the previous code.

Oct 9, 2009

Making Web Application with Haskell #2

I make a web application with Haskell.
It's a simple application.
You input your name, then the application output your name.
See below.






The other day I made the application without any library.
Today I make it with Network.CGI wich is included in GHC.


  1. import Network.CGI  
  2.   
  3. inputForm :: String -> String  
  4. inputForm script = concat [  
  5.   "<form action=\"", script, "\" method=\"GET\">",  
  6.   "<p>Who are you?</p>",  
  7.   "<input type=\"text\" name=\"name\" />",  
  8.   "<input type=\"submit\" value=\"send\" />",  
  9.   "</form>"]  
  10.   
  11. putName :: String -> String  
  12. putName name = concat ["<h1>Hello, ", name, "!</h1>"]  
  13.   
  14. body :: Maybe String -> String -> String  
  15. body name script =   
  16.   case name of   
  17.     Just x  -> putName x  
  18.     Nothing -> inputForm script  
  19.   
  20. html :: Maybe String -> String -> String  
  21. html name script = concat [  
  22.   "<html>",  
  23.   "<head><title>Hello</title></head>",  
  24.   "<body>",  
  25.   body name script,  
  26.   "</body>",  
  27.   "</html>"]  
  28.   
  29. cgiMain :: CGI CGIResult  
  30. cgiMain = do  
  31.   setHeader "Content-type" "text/html; charset = UTF-8"  
  32.   script <- scriptName  
  33.   name <- getInput "name"  
  34.   output $ html name script  
  35.   
  36. main :: IO ()  
  37. main = runCGI (handleErrors cgiMain)  



Complete!

Oct 7, 2009

Making Web Application with Haskell #1

I make a web application with Haskell.
It's a simple application.
You input your name, then the application output your name.
See below.






Today I make the application without any library.
I use standard input and output.


  1. import System.Environment  
  2.   
  3. inputForm :: String -> String  
  4. inputForm script = concat [  
  5.   "<form action=¥"", script, "¥" method=¥"GET¥">",  
  6.   "<p>Who are you?</p>",  
  7.   "<input type=¥"text¥" name=¥"name¥" />",  
  8.   "<input type=¥"submit¥" value=¥"send¥" />",  
  9.   "</form>"]  
  10.   
  11. getName :: String -> String  
  12. getName q = drop 5 q  
  13.   
  14. main = do   
  15.   putStrLn "Content-type: text/html; charset = UTF-8¥n"  
  16.   putStrLn "<html>"  
  17.   putStrLn "<head><title>Hello</title></head>"  
  18.   putStrLn "<body>"  
  19.   script <- getEnv "SCRIPT_NAME"  
  20.   query <- getEnv "QUERY_STRING"  
  21.   case query of  
  22.     "" -> putStrLn $ inputForm script  
  23.     _  -> putStrLn $ concat ["<h1>Hello, ", getName query, "!</h1>"]  
  24.   putStrLn "</body>"  
  25.   putStrLn "</html>"  



Complete!

Oct 3, 2009

Sum from 1 to 10 without loop

Can you sum from 1 to 10 without loop? It's a programming quiz which is in now in Japan.

You may solve it with inject method in Ruby.

  1. puts (1..10).inject(:+)  


Is it not interesting? Then, I write it with continuation.

  1. n, s = callcc {|$c| [1, 0] }  
  2. n, s = $c.call [n.succ, n + s] if n <= 10  
  3. puts s  


Additionally, you can use recursion without loop.