基礎知識で詰まる

http://www.haskell.org/haskellwiki/99_questions/46_to_50#Problem_46
日本語での理解があやしい。
46, 47は入力値としてA,BがTrueかFalseをとるから、そのときCがTrue | False どっちになるか というのをABC全部可能性列挙しろ、ってことかな。
48はA, B, CがTrue or False
たぶん。ロジックのすごく基本なところなんだろうし、置いて行かれてしまう。問題文でググッてもocamlで解いてみたとかprologだとこうなるとか読めないのしか無いw

IO出てくるとまだよく分かんないから
Bool -> Bool -> Bool -> [(Bool, Bool, Bool)] 満たすようにあとで書こう

Edited: 2012-04-05 14:15
いやこれもちがうな
(Bool, Bool) -> (Bool, Bool) -> (Bool, Bool) -> [(Bool, Bool, Bool)] か? よくわかんね

cable install readline with homebrew

Below is cable install readline with homebrew. And temporally unlink to libiconv. Then I can install lambdabot. Too long!

$ cabal install readline --extra-include-dirs=$(brew --prefix)/include \
--extra-lib-dirs=$(brew --prefix)/lib \
--configure-option=--with-readline-includes=$(brew --prefix)/include \
--configure-option=--with-readline-libraries=$(brew --prefix)/lib

$ brew unlink libiconv
$ cabal install lambdabot
$ brew link libiconv

via:
http://stackoverflow.com/questions/8291137/how-do-you-install-the-haskell-readline-library-on-mac-osx

filter, fst, snd, zip, cycle

filter, fst, snd, zip, cycleがよく理解できてないのでサンプル見て真似した
filter odd [1, 2, 3, 4]
#=> [1, 3]
snd (‘a’, ‘b’)
#=> ‘b’
fst (‘a’, ‘b’)
#=> ‘a’
cycle [1..3]
#=> [1, 2, 3, 1, 2, 3, 1, …]
zip [1, 2, 3] [“one”, “two” “three”]
#=> [(1, “one”), (2, “two”), (3, “three”)]

Prelude.filter

lazyLines 動かないが切り分けられず

ふつケル読んでてlazyLines 動かそうとしてるが動かない
問題の切り分けがわからない
$ ./server
は実行出来るバイナリじゃないと言われてしまうので、
$ ruby server.rb
で動かす方で考える。

とりあえず動きそうなpatchをあてて、
ふつケルの LazyLines を ruby1.9 で動かす – @yujioramaの日記 http://d.hatena.ne.jp/yujiorama/20110430/1304165258

1.9.2以降で問題になりそうな$LOAD_PATHなやつを追加してみて make all

ghc –make -W -fno-warn-unused-matches -cpp -DPOSIX CGIMain.hs -o index.cgi
[ 1 of 14] Compiling LineParser ( LineParser.hs, LineParser.o )
[ 2 of 14] Compiling PathUtils ( PathUtils.hs, PathUtils.o )
[ 3 of 14] Compiling FileUtils ( FileUtils.hs, FileUtils.o )
FileUtils.hs:16:1:
Warning: The import of `System.IO' is redundant
except perhaps to import instances from `System.IO'
To import instances alone, use: import System.IO()
[ 4 of 14] Compiling TextUtils ( TextUtils.hs, TextUtils.o )
[ 5 of 14] Compiling URLEncoding ( URLEncoding.hs, URLEncoding.o )
[ 6 of 14] Compiling HTML ( HTML.hs, HTML.o )
[ 7 of 14] Compiling Syntax ( Syntax.hs, Syntax.o )
[ 8 of 14] Compiling Config ( Config.hs, Config.o )
[ 9 of 14] Compiling Database ( Database.hs, Database.o )
Database.hs:21:1:
Warning: The import of `Data.List' is redundant
except perhaps to import instances from `Data.List'
To import instances alone, use: import Data.List()
[10 of 14] Compiling Template ( Template.hs, Template.o )
[11 of 14] Compiling URLMapper ( URLMapper.hs, URLMapper.o )
URLMapper.hs:16:1:
Warning: The import of `Data.List' is redundant
except perhaps to import instances from `Data.List'
To import instances alone, use: import Data.List()
[12 of 14] Compiling CGI ( CGI.hs, CGI.o )
CGI.hs:19:1:
Warning: The import of `Control.Monad' is redundant
except perhaps to import instances from `Control.Monad'
To import instances alone, use: import Control.Monad()
CGI.hs:69:18:
Warning: Pattern match(es) are non-exhaustive
In a case alternative:
Patterns not matched:
(_, (GHC.Types.C# #x) : _) with #x `notElem` ['=']
[13 of 14] Compiling LazyLines ( LazyLines.hs, LazyLines.o )
LazyLines.hs:23:1:
Warning: The import of `Data.Maybe' is redundant
except perhaps to import instances from `Data.Maybe'
To import instances alone, use: import Data.Maybe()
LazyLines.hs:41:34: Warning: Defined but not used: `name'
LazyLines.hs:43:50: Warning: Defined but not used: `content'
[14 of 14] Compiling Main ( CGIMain.hs, CGIMain.o )
Linking index.cgi …
ld: warning: could not create compact unwind for .LFB3: non-standard register 5 being saved in prolog
ghc –make -W -fno-warn-unused-matches -cpp -DPOSIX compile.hs -o compile
[5 of 5] Compiling Main ( compile.hs, compile.o )
Linking compile …
ld: warning: could not create compact unwind for .LFB3: non-standard register 5 being saved in prolog
ghc –make -W -fno-warn-unused-matches -cpp -DPOSIX showconfig.hs -o showconfig
[3 of 3] Compiling Main ( showconfig.hs, showconfig.o )
Linking showconfig …
ld: warning: could not create compact unwind for .LFB3: non-standard register 5 being saved in prolog

view raw
make-all
hosted with ❤ by GitHub

diff –git a/cgirunner.rb b/cgirunner.rb
index 2d530a9..6fca618 100755
— a/cgirunner.rb
+++ b/cgirunner.rb
@@ -7,7 +7,7 @@
# reserved.
#
# $IPR: cgi_runner.rb,v 1.9 2002/09/25 11:33:15 gotoyuzo Exp $
+$:.unshift File.dirname(__FILE__)
def sysread(io, size)
buf = ""
while size > 0
diff –git a/server.rb b/server.rb
index 5327f86..400c6dd 100755
— a/server.rb
+++ b/server.rb
@@ -1,4 +1,5 @@
# -*- coding: euc-jp -*-
+$:.unshift File.dirname(__FILE__)
require 'webrick'
require 'optparse'

view raw
ruby-1.9.2_load_path
hosted with ❤ by GitHub

$ ruby server.rb
*****************************************************
Type Ctrl-C to stop server
*****************************************************
[2012-02-20 02:28:21] INFO WEBrick 1.3.1
[2012-02-20 02:28:21] INFO ruby 1.9.3 (2012-02-16) [x86_64-darwin10.8.0]
[2012-02-20 02:28:21] DEBUG TCPServer.new(127.0.0.1, 10081)
[2012-02-20 02:28:21] DEBUG WEBrick::HTTPServlet::FileHandler is mounted on /.
[2012-02-20 02:28:21] INFO WEBrick::HTTPServer#start: pid=6993 port=10081
[2012-02-20 02:28:31] DEBUG accept: 127.0.0.1:60915
[2012-02-20 02:28:31] DEBUG accept: 127.0.0.1:60916
[2012-02-20 02:28:32] DEBUG WEBrick::HTTPServlet::FileHandler is invoked.
[2012-02-20 02:28:32] ERROR CGIHandler: /Users/sane/work/ghc-study/lazylines/index.cgi:
index.cgi: var/pages/FrontPage: hGetContents: invalid argument (Illegal byte sequence)
[2012-02-20 02:28:32] ERROR CGIHandler: /Users/sane/work/ghc-study/lazylines/index.cgi exit with 1
[2012-02-20 02:28:32] ERROR Premature end of script headers: /Users/sane/work/ghc-study/lazylines/index.cgi
localhost – – [20/Feb/2012:02:28:32 JST] "GET / HTTP/1.1" 500 360
[2012-02-20 02:28:32] DEBUG close: 127.0.0.1:60915
[2012-02-20 02:28:32] DEBUG WEBrick::HTTPServlet::FileHandler is invoked.
[2012-02-20 02:28:32] ERROR `/favicon.ico' not found.
localhost – – [20/Feb/2012:02:28:32 JST] "GET /favicon.ico HTTP/1.1" 404 282
[2012-02-20 02:28:32] DEBUG close: 127.0.0.1:60916

view raw
ruby-server
hosted with ❤ by GitHub

$ rvm list
rvm rubies
macruby-0.10 [ x86_64 ]
=> ruby-1.8.7-p334 [ i686 ]
ruby-1.9.2-p180 [ x86_64 ]
ruby-1.9.2-p290 [ x86_64 ]
ruby-1.9.3-p0 [ x86_64 ]
* ruby-1.9.3-p125 [ x86_64 ]

view raw
rvm-list
hosted with ❤ by GitHub

サーバは立ち上がるんだけど、http://localhost:10081/ にアクセスに行くと500エラーが返ってしまう。

ghc –make -W -fno-warn-unused-matches -cpp -DPOSIX CGIMain.hs -o index.cgi
[ 1 of 14] Compiling LineParser ( LineParser.hs, LineParser.o )
[ 2 of 14] Compiling PathUtils ( PathUtils.hs, PathUtils.o )
[ 3 of 14] Compiling FileUtils ( FileUtils.hs, FileUtils.o )
FileUtils.hs:16:1:
Warning: The import of `System.IO' is redundant
except perhaps to import instances from `System.IO'
To import instances alone, use: import System.IO()
[ 4 of 14] Compiling TextUtils ( TextUtils.hs, TextUtils.o )
[ 5 of 14] Compiling URLEncoding ( URLEncoding.hs, URLEncoding.o )
[ 6 of 14] Compiling HTML ( HTML.hs, HTML.o )
[ 7 of 14] Compiling Syntax ( Syntax.hs, Syntax.o )
[ 8 of 14] Compiling Config ( Config.hs, Config.o )
[ 9 of 14] Compiling Database ( Database.hs, Database.o )
Database.hs:21:1:
Warning: The import of `Data.List' is redundant
except perhaps to import instances from `Data.List'
To import instances alone, use: import Data.List()
[10 of 14] Compiling Template ( Template.hs, Template.o )
[11 of 14] Compiling URLMapper ( URLMapper.hs, URLMapper.o )
URLMapper.hs:16:1:
Warning: The import of `Data.List' is redundant
except perhaps to import instances from `Data.List'
To import instances alone, use: import Data.List()
[12 of 14] Compiling CGI ( CGI.hs, CGI.o )
CGI.hs:19:1:
Warning: The import of `Control.Monad' is redundant
except perhaps to import instances from `Control.Monad'
To import instances alone, use: import Control.Monad()
CGI.hs:69:18:
Warning: Pattern match(es) are non-exhaustive
In a case alternative:
Patterns not matched:
(_, (GHC.Types.C# #x) : _) with #x `notElem` ['=']
[13 of 14] Compiling LazyLines ( LazyLines.hs, LazyLines.o )
LazyLines.hs:23:1:
Warning: The import of `Data.Maybe' is redundant
except perhaps to import instances from `Data.Maybe'
To import instances alone, use: import Data.Maybe()
LazyLines.hs:41:34: Warning: Defined but not used: `name'
LazyLines.hs:43:50: Warning: Defined but not used: `content'
[14 of 14] Compiling Main ( CGIMain.hs, CGIMain.o )
Linking index.cgi …
ld: warning: could not create compact unwind for .LFB3: non-standard register 5 being saved in prolog
ghc –make -W -fno-warn-unused-matches -cpp -DPOSIX compile.hs -o compile
[5 of 5] Compiling Main ( compile.hs, compile.o )
Linking compile …
ld: warning: could not create compact unwind for .LFB3: non-standard register 5 being saved in prolog
ghc –make -W -fno-warn-unused-matches -cpp -DPOSIX showconfig.hs -o showconfig
[3 of 3] Compiling Main ( showconfig.hs, showconfig.o )
Linking showconfig …
ld: warning: could not create compact unwind for .LFB3: non-standard register 5 being saved in prolog

view raw
make-all
hosted with ❤ by GitHub

diff –git a/cgirunner.rb b/cgirunner.rb
index 2d530a9..6fca618 100755
— a/cgirunner.rb
+++ b/cgirunner.rb
@@ -7,7 +7,7 @@
# reserved.
#
# $IPR: cgi_runner.rb,v 1.9 2002/09/25 11:33:15 gotoyuzo Exp $
+$:.unshift File.dirname(__FILE__)
def sysread(io, size)
buf = ""
while size > 0
diff –git a/server.rb b/server.rb
index 5327f86..400c6dd 100755
— a/server.rb
+++ b/server.rb
@@ -1,4 +1,5 @@
# -*- coding: euc-jp -*-
+$:.unshift File.dirname(__FILE__)
require 'webrick'
require 'optparse'

view raw
ruby-1.9.2_load_path
hosted with ❤ by GitHub

$ ruby server.rb
*****************************************************
Type Ctrl-C to stop server
*****************************************************
[2012-02-20 02:28:21] INFO WEBrick 1.3.1
[2012-02-20 02:28:21] INFO ruby 1.9.3 (2012-02-16) [x86_64-darwin10.8.0]
[2012-02-20 02:28:21] DEBUG TCPServer.new(127.0.0.1, 10081)
[2012-02-20 02:28:21] DEBUG WEBrick::HTTPServlet::FileHandler is mounted on /.
[2012-02-20 02:28:21] INFO WEBrick::HTTPServer#start: pid=6993 port=10081
[2012-02-20 02:28:31] DEBUG accept: 127.0.0.1:60915
[2012-02-20 02:28:31] DEBUG accept: 127.0.0.1:60916
[2012-02-20 02:28:32] DEBUG WEBrick::HTTPServlet::FileHandler is invoked.
[2012-02-20 02:28:32] ERROR CGIHandler: /Users/sane/work/ghc-study/lazylines/index.cgi:
index.cgi: var/pages/FrontPage: hGetContents: invalid argument (Illegal byte sequence)
[2012-02-20 02:28:32] ERROR CGIHandler: /Users/sane/work/ghc-study/lazylines/index.cgi exit with 1
[2012-02-20 02:28:32] ERROR Premature end of script headers: /Users/sane/work/ghc-study/lazylines/index.cgi
localhost – – [20/Feb/2012:02:28:32 JST] "GET / HTTP/1.1" 500 360
[2012-02-20 02:28:32] DEBUG close: 127.0.0.1:60915
[2012-02-20 02:28:32] DEBUG WEBrick::HTTPServlet::FileHandler is invoked.
[2012-02-20 02:28:32] ERROR `/favicon.ico' not found.
localhost – – [20/Feb/2012:02:28:32 JST] "GET /favicon.ico HTTP/1.1" 404 282
[2012-02-20 02:28:32] DEBUG close: 127.0.0.1:60916

view raw
ruby-server
hosted with ❤ by GitHub

$ rvm list
rvm rubies
macruby-0.10 [ x86_64 ]
=> ruby-1.8.7-p334 [ i686 ]
ruby-1.9.2-p180 [ x86_64 ]
ruby-1.9.2-p290 [ x86_64 ]
ruby-1.9.3-p0 [ x86_64 ]
* ruby-1.9.3-p125 [ x86_64 ]

view raw
rvm-list
hosted with ❤ by GitHub

euc-jpのテンプレート読み込もうとしてうまくいってない感じだけど、よくわからない。1.9.3, 1.9.2, 1.8.7で試してみたけどどれもダメ。手持ちのruby環境がまずいのか、webrickなのか、またまた別なところなのかわからない。はげしくyak shavingなので動いたことにして進むしかなさげ。

ふつケル読んでる splitAt

文字列調べて、長かったらsplitAtって書いた。解答例見て書きなおした。

foldLine :: String -> [String]
foldLine line = case splitAt 60 line of
    (s, []) -> [s]
    (s, remain) -> s : foldLine remain

いきなりsplitAtして、後ろが空リストだったら、リストリテラルで1番目を返して、後ろがあれば、1番目の要素+残りに再帰。って賢いなー。自分でsplitAtみたいなの書くときもこういう関数の設計にすればうまくいくのね。

よく言われててふーんって聞き流してたけど、ふつうのhaskellは練習問題のちょうどよさ加減が気持ちいい。できそうでできない、頑張ったらできた、解答例見たらすっきり書いててそれが理解できて、もう一度自分で書いてみたらかけて、良いサイクルである。

ふつケルからのメモ書き 高階関数

square n = n * n
map square [1, 2, 3]
[(square 1), (square 2), (square 3)]

concatMap f xs

concatMap expandTab "ab\tcd\n"
concat $ map expandTab "ab\tcd\n"
concat $ [(expandTab 'a'), (expandTab 'b'), (expandTab '\t'), (expandTab 'c'), (expandTab 'd'), (expandTab '\n')]
concat $ [['a'], ['b'], (replicate tabStop ' '), ['c'], ['d'], ['\n']
concat ['a', 'b', " ", ['c'], ['d'], [\n]]
'ab cd\n'

map :: (a -> b) -> [a] -> [b]
map f [] = []
map f (x:xs) = f x : map f xs

map f (x:xs) = ((f x) : (map f xs))

: リストを生成する演算子
リストに対するパターンマッチでは(x:xs)がリストを先頭要素とそれ以降のリストに分解
関数定義の中で(y:ys) リストysの先頭にyを追加したリストを作成