PHP を少しだけ触ってみました><

これから一からプログラミングを覚えようと考えています。 様々… - 人力検索はてな この質問をみてて、PHP がにんき?みたいだったので試してみたくなりました!

<?php

function get_files($dir) {
    $ret = array();
    $dh  = opendir($dir);
    while (($f = readdir($dh)) !== FALSE) {
        if ($f == "." || $f == "..") {
                continue;
        }
        $f = $dir . "/" . $f;
        if (is_dir($f)) {
            $ret += get_files($f);
        } else {
            $ret[] = $f;
        }
    }
    closedir($dh);
    return $ret;
}

print_r(get_files("data"));

// PHP だけのファイルは ?> 書かないほうがいいみたい

ファイルを再帰的に列挙する関数を書いてみました。うーん。すごく難しいです 。・°°・(>_<)・°°・。 いろいろ調べてみたけど、統一的なやりかた?みたいなのができなくてうまく書けない(;_;) みんなどうやって書いてるのかなぁ????

Blosxom のつづき

blosxkel.hs

ファイルを列挙してデータ構造をつくって、時間でソートして出力するところまでやってみたよ(o⌒∇⌒o) すごい難しい(*ノ_<*)

liftIO の意味をよくわかってないけど、これがないとエラーになっちゃうみたい。なんでだろう?? カレントモナド? がよくわかってないのかなぁ? Network.CGI - 女子高生ぷろぐらまーなお☆のブログ の MonadCGI m => っていうのと関係あるのかなぁ?

それとそれと、cgiMain の外で scriptName をよぶと

    No instance for (MonadCGI IO)
      arising from use of `scriptName' at blosxkel.hs:83:12-21
    Possible fix: add an instance declaration for (MonadCGI IO)

みたいになっちゃう。これもやっぱり MonadCGI m => の宣言と関係ありそう?? よくわからない (;>_<;)

あ、それと、CodeRepos のコミット権をもらいました! CodeRepos のみなさんよろしくお願いします (;>_<;)

ひさしぶり☆

いっぱい☆がついてて嬉しいなヾ(´▽`;)ゝ

HaskellBlosxom を書いているのは中断してたりヽ( ̄ー ̄ )ノ 難しいし、IO ばっかりだから手続っぽいし (笑)

http://lovecall.14.dtiblog.com/ こんなブログを見つけてすこしライバル視 (笑) わたしは PHP わからないんだけど、やろうっかな ( ̄~ ̄;)

Python で blosxom !

書こうと思ったけどやめちゃった (笑) 雰囲気はわかったからいいかな〜(’’;)

#!/usr/bin/env python

import os

opts = {
        'data-dir' : "data",
        'data-ext' : ".txt",
        }

class BlosxomPy:
    class Entry:
        pass

    def __init__(self, opts):
        self.opts = opts

    def run(self):
        def sort_by_mtime(a, b):
            return cmp(b.time, a.time)
        entries = self.entries()
        entries.sort(sort_by_mtime)
        print repr([e.name for e in entries])

    def entries(self):
        ret = []
        for path in self.listfiles(opts['data-dir']):
            f = file(path)
            content = f.readlines()

            e = self.Entry()
            e.file  = path
            e.name  = path.replace(opts['data-dir'], '').replace(opts['data-ext'], '')
            e.time  = os.stat(path).st_mtime
            e.title = content[0]
            e.body  = content[1:-1]
            ret.append(e)

        return ret

    def listfiles(self, dir):
        def _listfiles(dirs, result):
            if len(dirs) == 0:
                return result

            dir = dirs.pop()
            for file in os.listdir(dir):
                file = os.path.normpath(os.path.join(dir, file))
                if os.path.isdir(file):
                    dirs.append(file)
                else:
                    result.append(file)
            # tail call
            return _listfiles(dirs, result)
        return _listfiles([dir], [])

BlosxomPy(opts).run()

Python は Pyblosxom とかゆうのがあるし、べつにいいかなぁ??

  • リストの内包表記は楽しいかも
  • いろんなところのコロンを忘れる (笑)
  • リファレンスから目的の関数がひきにくい (オブジェクト指向じゃないからかな)

書いたあとに調べてみたら Python は末尾再帰最適化しなかった(ノ_<。)

実は変更してました (笑)

Haskell がわからなすぎるので (笑)、bloscheme のほうをリファクタリングとかしてましたへ(゜∇、°)

個別エントリのマッチ条件をもっと厳しくしないとだめかなー( ̄〜 ̄;)??簡単にプリフィックスマッチにしてるから、複数エントリがヒットしちゃうことがある(。>_<。)

IO モナドにハマってますヽ(  ̄д ̄;)ノ

#!runghc

import System.Directory
import System.Time
import Data.Maybe
import Text.Regex

data Entry = Entry {
    path      :: FilePath,
    title     :: IO String,
    timestamp :: IO ClockTime,
    body      :: IO [String]
}

isMatch :: FilePath -> Bool
isMatch path = isJust $ matchRegex reg path where reg = mkRegex ".txt$"

consEntries :: FilePath -> Entry
consEntries fp = Entry {
    path      = fp,
    title     = titl,
    timestamp = time,
    body      = body
} where time = do t <- getModificationTime fp
                  return t
        fcon = do b <- readFile fp
                  return $ splitAt 1 $ splitRegex (mkRegex "\r?\n") b
        titl = do t <- fcon
                  return $ head $ fst $ t
        body = do b <- fcon
                  return $ snd $ b

getMatchedEntries :: IO [Entry]
getMatchedEntries = do list <- getDirectoryContents "data"
                       return $ map consEntries $ filter isMatch $ list

main = do list <- getMatchedEntries
          putStr $ unlines $ map (\e -> title e) $ list

これだと

blosxkel.hs:38:29:
    Couldn't match `String' against `IO String'
      Expected type: [String]
      Inferred type: [IO String]
    In the expression: (map (\ e -> title e)) $ list
    In the second argument of `($)', namely `(map (\ e -> title e)) $ list'

ってでちゃうんですよね (*>_<*)

data の中の IO をもう一回 bind しないといけないのかなぁ??? むずかしい…… 書きかたがわからない (笑)