This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| > module MyParseLib | |
| > (Parser, mplus, orelse, item, many, some, manywith, somewith, | |
| > sat, char, string, digit, lower, upper, letter, alphanum, | |
| > ident, space, token, symbol, applyParser) where | |
| > import Data.Char | |
| > import Control.Monad | |
| > newtype Parser a = MkP (String -> [(a,String)]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| > import Data.Char | |
| > instance Functor Parser where | |
| > -- map f p = MkP g | |
| > fmap f p = MkP g | |
| > where g s = [(f x, s') | (x,s') <- papply p s] | |
| > alphanum :: Parser Char | |
| > -- alphanum = sat isAlphaum | |
| > alphanum = sat isAlphaNum |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Matrix = [[Int]] | |
| dropAt :: Int -> [a] -> [a] | |
| dropAt i xs = [x| (x, j) <- zip xs [0..], i /= j] | |
| cofactor :: Int -> Int -> Matrix -> Matrix | |
| cofactor i j m = [dropAt j v| (v, k) <- zip m [0..], i /= k] | |
| det :: Matrix -> Int | |
| type Matrix = [[Int]] | |
| dropAt :: Int -> [a] -> [a] | |
| dropAt i xs = as ++ bs where (as, _ :bs) = splitAt i xs | |
| cofactor :: Int -> Int -> Matrix -> Matrix |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Control.Monad.Writer | |
| gcd' :: Int -> Int -> Writer [String] Int | |
| gcd' a b | |
| |b == 0 = do | |
| tell["= " ++ show a] | |
| return(a) | |
| |otherwise = do | |
| let nb = a `mod` b | |
| expr = "gcd(" ++ show(b) ++ ", " ++ show(nb) ++ ")" | |
| msg = show(a) ++ " = " ++ show(b) ++ " * " ++ show(a `div` b) ++ " + " ++ show(nb) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Control.Monad.Writer | |
| size :: [[a]] -> Int | |
| size = foldr (\ xs n -> length xs + n) 0 | |
| move :: [[Int]] -> (Int, Int) -> Writer [String] [[Int]] | |
| move xss (from, to) = return([move_ xs y (from, to) i| (xs, i) <- zip xss [0..]]) | |
| where (y:_) = xss !! from | |
| move_ :: [Int] -> Int -> (Int, Int) -> Int -> [Int] | |
| move_ [] y (from, to) i = if i == to then [y] else [] | |
| move_ (x:xs) y (from, to) i |i == from = xs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Parsing (parse, line) | |
| import System.IO | |
| import System.Environment | |
| import Data.Char | |
| import Data.Map as M | |
| import Data.Time.Clock | |
| import Control.Monad.State | |
| -- http://d.hatena.ne.jp/mzp/20090308/bench | |
| bench f = do from <- getCurrentTime |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ldf :: Int -> Int -> Maybe Int | |
| ldf k n | k ^ 2 > n = Nothing | |
| | rem n k == 0 = Just k | |
| | otherwise = ldf (k+1) n | |
| prime :: Int -> Bool | |
| prime n = case (ldf 2 n) of | |
| Just m -> m == n | |
| Nothing -> True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ sqlite3 test.db | |
| SQLite version 3.7.9 2011-11-01 00:52:41 | |
| Enter ".help" for instructions | |
| Enter SQL statements terminated with a ";" | |
| sqlite> create table fruits(name string, value integer); | |
| sqlite> insert into fruits values('apple', 100); | |
| sqlite> insert into fruits values('orange', 130); | |
| sqlite> .q |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Guardのマッチング規則を定義 | |
| guard :minitest, spring: "bin/rails test", all_on_start: false do | |
| watch(%r{^test/(.*)/?(.*)_test\.rb$}) | |
| watch('test/test_helper.rb') { 'test' } | |
| watch('config/routes.rb') { integration_tests } | |
| watch(%r{^app/models/(.*?)\.rb$}) do |matches| | |
| "test/models/#{matches[1]}_test.rb" | |
| end | |
| watch(%r{^app/controllers/(.*?)_controller\.rb$}) do |matches| | |
| resource_tests(matches[1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #usage: ruby main.rb | |
| class Parser | |
| def initialize | |
| @stack = [] | |
| end | |
| def parse(line) | |
| scanner = Scanner.new(line) | |
| while (token = scanner.next) do | |
| add_token(token) | |
| end |