Haskellã®åå¼·ããã¦ããããElixirã®ãã¤ãã©ã¤ã³æ¼ç®åãF#ç±æ¥ã§ãããã¨ãç¥ã£ã
Elixirã¨ããããã°ã©ãã³ã°è¨èªã«ããã¤ãã©ã¤ã³ãã¯ãæ¼ç®åã¨ããã®ããããé¢ç½ããªã¼ã¨æã£ã¦ããããåãElixirã¨ãç¥ããªãã ããããããããã©ããªãã®ãå¾ã§æ¸ãã¨ãã¦ãHaskellã®åå¼·ããã¦ãããåçã®ãã®ãå®è£
ãã¦ããè¨äºãã¿ã¤ããã©ããããã¯F#ç±æ¥ã§ãããã¨ãç¥ã£ãã
ãã¦ããã ããªãä¾ã ãã©ãæ°ã®ãªã¹ãã«ä½ã(map)ãã¦æ¡ä»¶ãæºãããã®ãæãåºãã¦(filter)ãæå¾ã«è¶³ã(fold)ã¿ãããªãã¨ã£ã¦ãHaskellã§ã¯æ®éãã¨ãã°æ¬¡ã®ããã«æ¸ãï¼ã¨æãï¼
Prelude> sum . filter even . map (+1) $ [1..10] 30
Haskellãªãã¦ç¥ããªã人ãããã¨æãã®ã§ãã»ã¼åçã®ã³ã¼ããPerlã§æ¸ãã¨ãããªã
use strict; use warnings; use List::Util qw/sum/; my $ans = sum grep { $_ % 2 == 0 } map { $_ + 1 } (1 .. 10); print( $ans );
ç´°ããªææ³ã®éãã¯ããããã£ã¦ããã¨ã¯åãã ã
Rubyã®äººãªãããæ¸ããããããããªã
irb> (1..10).map{|x| x + 1 }.select{|n| n.even? }.reduce(0){|a, b| a + b } => 30
ã©ã¡ããããã¨ãããã¨ã¯ãªãã®ã ãã©ãè¡ãå¦çã®å
容ã«ãã£ã¦ã¯Rubyã®ãããªé çªã§æ¸ããã»ãããããããããã¨ããããï¼Unixã®ã³ãã³ãããã¤ãã©ã¤ã³ã§ã¤ãªãã§ãããã¨ãæãåºãã¦ã»ããï¼
F#ã«ã¯ãã¤ãã©ã¤ã³æ¼ç®å |> ã¨ããããããããã¨ãå®ç¾ããæ¼ç®åãããããããHaskellã§ã¯ãããå®ç¾ããæ¼ç®åï¼ã¨ããããã ã®é¢æ°ï¼ãç°¡åã«æ¸ããã¨ãã§ãã次ã®ããã«æ¸ã
-- |pipeline operator -- >>> sum . filter even . map (+1) $ [1..10] -- 30 -- -- >>> [1 .. 10] |> map (+1) |> filter even |> sum -- 30 infixl 0 |> (|>) :: a -> (a -> b) -> b x |> f = f x
Elixirã®ãã¤ãã©ã¤ã³ã¯æã¯ãã¯ãã§å®è£ ããã¦ãã¦ååã /> ã ã£ãããæè¿ã®ãªãªã¼ã¹(0.7.2)ã§çµã¿è¾¼ã¿ã®æ¼ç®åã«ãªã£ã¦ååã |> ã«ãªã£ãããã ã
iex(4)> [1,2,3,4] |> Enum.map(&1 + 2) |> Enum.filter(fn(x) -> rem(x, 2) == 0 end) |> List.foldl(0, &1 + &2) 10
ãªãElixirã®é¢æ°ã¯Haskellã¿ããã«ã«ãªã¼åããã¦ãããã¯ããªãã®ã§ãæ®éã¯ãããªãã®ä½ããªããçµå±çµã¿è¾¼ã¿æ¼ç®åã«ãªã£ãããããããããã®ãã¦ã¼ã¶ãä½ããã¨ããããã¯ãï¼Exlirã®ãã¯ãã¯æ½è±¡æ§ææ¨ãæä½ããæ¬ç©ã®ãã¯ãã ï¼ã§ã使ããããªãã¨æããã¨ãããã¨ã¯æ¬ç©ã®ãã¯ãããªãè¨èªã ã¨å¾ããããããã®ãä½ãã®ã¯é£ããã
ãã¦ãããã ãã 㨠id:kazu-yamamoto ããã®è¨äºãèªãã°ããã£ã¦ãã話ã§ã¤ã¾ããªãã®ã§ãF#ã®é¢æ°åææ¼ç®å >> ã¨åçã®ãã®ãHaskellã§ä½ã£ã¦ã¿ãããããä½ã£ã¦ã¿ãã¨ãã£ã¦ããããç³ç°¡åãªã®ã ãã
Haskellã«ã¯ãã¡ããé¢æ°åææ¼ç®å . ããããä¸ã§ã使ã£ã¦ããããF#ã®ããã¨ã¯éé ã§ããã |> ã§å¤ã渡ãã¦ããã®ã¯æ¥½ããããå¼æ°ãæåã«æ¸¡ããªããã°ãããªãã®ã§ãçµæã¨ãã¦é¢æ°ã欲ããæã¯ã©ã ãå¼ã«ãããããªãã
Prelude> (\s -> s |> map (+1) |> filter even |> sum) [1 .. 10] 30
ã§ããã¶ãHaskellã ã¨ããã®ãããªé¢æ°ãä½ãåºããããã¨ãããã ãããéé ã®é¢æ°åææ¼ç®å |>> ã¯ä»¥ä¸ã
-- |pipelined compose operator -- >>> (map (+1) |>> filter even |>> sum |>> show |>> putStr) [1 .. 10] -- 30 -- -- >>> :m + Data.List -- >>> ( words |>> sort |>> group |>> map (\ls -> (head ls, length ls)) ) "ab a b a a b" -- [("a",3),("ab",1),("b",2)] infixl 0 |>> (|>>) :: (a -> b) -> (b -> c) -> a -> c g |>> f = f . g
ããã½ã¼ã¹ã³ã¼ãã¯githubã«ããããdoctestå½¢å¼ã®ä½¿ç¨ä¾å
¼åä½ãã¹ããããã
ãã¯ããåãã¯SICPãèªãã¨ãã
ãã®è¨äºã¯VOYAGE GROUP Advent Calendar ã®12/24åã§ã
ãªããæ¨æ²ãéã«åããã12/24ã«éã«åãã¾ããã§ããããä»æ´ã ãã¾ããããããªããã
SICPï¼è¨ç®æ©ããã°ã©ã ã®æ§é ã¨è§£éï¼ã¨è¨ãæ¬ããåç¥ã§ããï¼ç§ã¯ãã®SICPã¨ããæ¬ã大好ãã§ã社å
ã§ãä½åº¦ãèªæ¸ä¼ãå¬ãã¦ãã¾ãããªãèªæ¸ä¼ããããã¨ããã¨ãä¸äººã§èªã¿ããã®ã¯è¾ãï¼äººãããï¼ããã§ãã
ãã®è¨äºã§ã¯ä½ããããªã«è¯ãã®ãã¨ãã説æããã¾ãã
ãããããã®æ¬ã¯ãMITã®6.001ã¨ããè¨ç®æ©ç§å¦è¬ç¾©ã®æç§æ¸ã¨ãã¦ãã¶ã¤ã³ãããå®éã«ä½¿ããã¦ãããã®ã ããã§ãï¼ä»ã¯å¥ãªæ¬ãæ¡ç¨ããã¦ããï¼ã
åæ§ã®åæ©çãªè¨ç®æ©ç§å¦ã®æç§æ¸ã®ä¸ã§ãã®æ¬ãç¹ç°ã ã¨ç§ãæããã®ã¯ããã¾ããªãããç´æãå¯è½ãªéãå»ãã¦ãããå°æ°ã®æ ¹æ¬çãªåä½åçã®ã¿ãä»®å®ãã¦ãå¤æ°ã¨ã¯ä½ããé¢æ°ã¨ã¯ä½ããããã°ã©ãã³ã°ããã¨ããè¡çºã¯ä½ãã説æãããã¨ãã¦ããç¹ã§ãã
ããããæå㪠Hello World ã®Cçãæ²è¼ãã¾ããããã ãã®ããã°ã©ã ã®ä¸ã«ããã¤ã®ãã¾ããªããå«ã¾ããã§ããããã
#include<stdio.h> /* Q.ããã¯ä½ï¼ A.ãã¾ããªã */ main() /* Q. ããã¯ä½ï¼ A.ãã¾ããªã */ { printf("hello world\n"); /* Q. printf ( " \n ) ; ã¯ããããä½ï¼ A. ãã¾ããªã */ }
ããã確ãã«LLã ã¨ãã£ã¨çãã§ãã¾ãããããããã§åé¡ã«ãã¦ããã®ã¯ãã¾ããªãã®å°ãªãã§ãputsã£ã¦ä½ï¼ã¨ããåå¦è ã®è³æ¥µãã£ã¨ããªåã«çå£ã«çãããã¨ããã¨å²ãã¨é¢åãªã®ã§ã¯ãªãã§ããããã
puts "hello world" # putsã¯é¢æ°ï¼ã¡ã½ããï¼ã¡ãã»ã¼ã¸ï¼åºæ¬æç¶ãï¼æ¨æºã©ã¤ãã©ãªï¼
ãããã¯ãä»ã¯ããéåæãå¿ãã¦ãã¾ã£ãã ãããã©ã次ã®å¼ï¼è¨èªã«ãã£ã¦ã¯æï¼ãè¦ã¦ä¸ããã
i = i + 1
i = i + 1 ã ã£ã¦ï¼çå¼ãæãç«ã£ã¦ãªããããªããï¼
ããªãããã£ã¨æã¯ããã«éåæãè¦ããã¯ããªã®ã§ããä»ãéåæãå¿ãã¦ãã¾ã£ãããªãã¯ãããã«éåæãæã¤åå¦è ã«ã©ã説æããã¨ããã®ã§ããããï¼
ãªããããã®èª¬æãå°é£ãã¨ããã¨ãããã¯ãããæ¬å½ã¯é£ãããã¨ããã¦ããããã§ãã
æ¬å½ã¯IOã¯é£ããããå¯ä½ç¨ãé£ããã®ã§ãã
ãããSICPã§ã¯ã©ã説æãã¦ãããã¨ããã¨ãIOã«é¢ãã説æã¯ã»ã¨ãã©ãã¦ããªããæå°éã§ãå¯ä½ç¨ã«é¢ãã¦ã¯æåã¯èª¬æãã¾ãããå¯ä½ç¨ããªãæå°éã®ã«ã¼ã«ã ãã§è¨è¿°ã§ããåä½ã¢ãã«ãæåã¯æ示ãã¾ãããã°ããã®éããã®åä½ã¢ãã«ã®ä¸ã§ã§ããè²ã ãªé¢ç½ããã¨ã説æãã¾ããããã¦ãããã¹ãå¾ã«å¯ä½ç¨ãã説æã§ããå®å ¨ãªã«ã¼ã«ãæ示ãã¦ãã¾ãã
ãåä½ã¢ãã«ãã¨è¨ãã¾ããããããã¯ãã®åã«ãåä½åçãã¨è¨ãã¾ãããSICPã¯ã²ãããåä½ã¢ãã«ã®èª¬æããã¦ããã®ã ã¨ããè¦æ¹ããããã¨ãã§ãã¾ãã
ãã®è¦æ¹ã®ä¸ã§ã¯ãSICPã®ç« ç«ã¦ã以ä¸ã®ããã«è¦ããã¨ãã§ãã¾ãï¼ãæç¶ããã¨ããç¨èªã¯ãä»ã®è¨èªã§ãããé¢æ°ãã¨æãã°ããã§ããããã°ã©ã è¨èªã«ããããé¢æ°ãã¯å³å¯ã«ã¯æ°å¦ã«ããããé¢æ°ãã§ã¯ãªããããSICPã§ã¯è¬èã«ãæç¶ããã¨å¼ãã§ãããã®ã¨æãã¾ãï¼ã
- 1ç« æç¶ãä½ç¨ã®ç½®ãæãã¢ãã«ã¨æç¶ãæ½è±¡
- 2ç« ãã¼ã¿æ½è±¡
- 3ç« æç¶ãä½ç¨ã®ç°å¢ã¢ãã«ã¨å¯ä½ç¨
- 4ç« åä½ã¢ãã«ãå®éã«ããã°ã©ã ã§å®è£ ããï¼è©ä¾¡å¨ï¼evalæç¶ããã¾ãã¯ã¤ã³ã¿ã¼ããªã¿ï¼ã®å®è£
- 5ç« è©ä¾¡å¨ãä½ãã®æ©æ¢°èªã«å¤æããï¼ç¿»è¨³å¨ï¼ã³ã³ãã¤ã©ï¼ã®å®è£
ãã®æ¬ãèªãã¨ä½ãããã®ããããã¯ããã°ã©ã ã®åä½ã¢ãã«ã身ã«ã¤ããã¨ã§ãããã¾ããªãããã¹ããããã®ã³ããããçµé¨åï¼ï¼ããåãããªããããæ¸ãã¨ããåãï¼ã§ã¯ãªããæ確ãªåçã«åºã¥ãã¦ãæ確ãªææã¨æå³ããã£ã¦ããã°ã©ã ãè¨è¿°ããèªã¿ãæ½è±¡ãæ§ç¯ãããã¨ãã§ããããã«ãªãã¾ãã
å®å
¨ãªåå¦è
ãèªãã§ãã¾ãããã®ããããã¾ããããè·æ¥ããã°ã©ãã§ä¸å¿ã®ããã°ã©ã ã®æ¸ãæ¹ã¯åããããæ¬æ ¼çãªè¨ç®æ©ç§å¦ã®æè²ãåãããã¨ããªããåºç¤ã«ä¸å®ããããããªæ¹ããä¸æ©å
ã«é²ãããã®æ¬ã¨ãã¦ããå§ãã§ãã
èå³ãæãããæ¹ã¯ãæ£æä¼ã¿ã«ã§ããèªãã§ã¿ãã¨ããã®ã§ã¯ãªãã§ããããã
[PHP] PHPã®json_encodeã§ã空ã®é åã¨ç©ºã®é£æ³é åãåºå¥ããããããã¦ãã¦
<?php $json = array( 'dict' => (object)array(), 'list' => array(), ); //{"dict":{},"list":[]} echo json_encode($json);
ããã£ã¦ãè¦ã¤ãããªãã£ãã monmonããã®è§£æ³ã id:tomi-ru ãããæ¹åããã
æ§é ä½ã®æé»ã®ã³ã³ã¹ãã©ã¯ã¿ï¼
ã©ããã¦ãé«éåãããå¦çããã£ã¦ä¹
ã
ã«C++ãæ¸ãã¦ãã¾ãã
ããæ¸ãã¦ãã¦ããï¼ã¨æããã¨ããã£ãã®ã§è¨äºãæ¸ãã¦ã¿ã¾ãã
ãµã³ãã«ã³ã¼ãã¯ä»¥ä¸ã®ãããªãã®ã§ãã
#include<iostream> #include<string> #include<map> struct point { int x; int y; }; void disp(const point& pt) { std::cout << "(" << pt.x << ", " << pt.y << ")" << std::endl; } int main(){ //garbage value point pt1; disp(pt1); // (0, 0) std::map<std::string, point> m; disp(m["hoge"]); // (0, 0) implicit constructor? point pt2 = point(); disp(pt2); // (0, 0) initialize point pt3 = {}; disp(pt3); return 0; }
//garbage value
point pt1;
disp(pt1);
ãããã´ãã表示ããã®ã¯Cã®æè¦ã§è¨ãã°ç´å¾ã§ãã
ã¨ããã
// (0, 0) std::map<std::string, point> m; disp(m["hoge"]);
ãªãã¨ãã㯠"(0, 0)" ã¨è¡¨ç¤ºãããã®ã§ããã
std::map ã® operator[] ã¯ããã¼ã«å¯¾å¿ããã¨ã³ããªãåå¨ããªãå ´åã«ã¯ããã©ã«ãã³ã³ã¹ãã©ã¯ã¿ã§ãªãã¸ã§ã¯ããä½ãçãªç解ããã¦ãã¾ããããpt1ã®ã±ã¼ã¹ã¨æåãç°ãªãããã§ãã
ãã£ï¼ã¨æã£ã¦ä½¿ã£ã¦ããã³ã³ãã¤ã©ã®STLã®mapã®ã½ã¼ã¹ãªã©ãèªãã§ã¿ããè²ã
試ãã¦ããã¨ãã©ãã
// (0, 0) implicit constructor?
point pt2 = point();
disp(pt2);
ãã®ãããªæ¸ãæ¹ã§ã¯ "(0, 0)"ã«åæåããããããªã®ã§ããã
C++ã®æ§é ä½ã¯publicãããã©ã«ãã§ãã以å¤ã¯classã¨åãã¨ããã®ã¯ç¥ã£ã¦ãã¾ãã
ã¨è¨ããã¨ã¯Cçãªè¨è¿°ã§æ§é ä½ã宣è¨ããå ´åã¯ãã³ã³ãã¤ã©ãæé»ã®ã³ã³ã¹ãã©ã¯ã¿ããã³ãã¼ã³ã³ã¹ãã©ã¯ã¿ãªã©ãä½ã£ããããã®ã ããã¨ããæãè¾¼ãã§ããã®ã§ãããããã pt1 ã®æ¸ãæ¹ã¨ pt2 ã®æ¸ãæ¹ã¯ç価ã§ããã¨æã£ã¦ãã¾ããã
ã¾ããã ã以ä¸ã®ãããªæ¸ãæ¹(ãããpt1ãpt2ã¨ç価ã§ããã©ã«ãã³ã³ã¹ãã©ã¯ã¿ãå¼ã°ããã¨æã£ã¦ãã¾ãã)ã¯ã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªã£ãã®ã§ä½ãåéããã¦ããã®ããããã¾ããã
// compile error
point pt4();
disp(pt4);
æ®éã§ããã° class ã«ãã¦ã³ã³ã¹ãã©ã¯ã¿ãæ示çã«ä¸ããã¹ããªã®ã§ãããããé«éåãç®çãªã®ã§å¯è½ã§ãããã¤ä»æ§ã«åã£ãæåã§ãããªãã°ä½åãªãã¨ãæ¸ããã«æ¸ã¾ãããã®ã§ãããããã¯ãã»ã»ã»
ããã°ã©ãã³ã°è¨èªC++ããã£ã¨çºããã ãã§ã¯ããã®æåãæ£å½åããè¨è¿°ã¯è¦ã¤ãããã¾ããã§ãããã¨ãªãã¨C++ã®ä»æ§æ¸ãèªãã¹ããªã®ã§ããããããã¾ãèªã¿ãããªãããèªãã§ãå¦çç³»ãä»æ§ã«åã£ã¦ãããã©ãããå®ãã§ã¯ãªãã¨ãããã
妥å½æ§ã¨ã¯ä»æ§ã®æä½ - SQLã¤ã³ã¸ã§ã¯ã·ã§ã³å¯¾çã¨ããªãã¼ã·ã§ã³
ç¹°ãè¿ãã«ãªãã¾ããã妥å½æ§æ¤è¨¼ã¯ä»æ§ã®åé¡ã§ãã£ã¦ã»ãã¥ãªãã£å¯¾çã§ã¯ããã¾ããã
ããªãã¼ã·ã§ã³ã¯ä»æ§ã®åé¡ã§ãã£ã¦ã»ãã¥ãªãã£å¯¾çã§ã¯ãªãã¨ã¯ã©ããããã¨ã説æãã¾ããSQLã¤ã³ã¸ã§ã¯ã·ã§ã³ã®å¯¾çã¯ã1. SQLãæååçµåã§ä½ããªã 2. ãã¬ã¼ã¹ãã«ãã使ã ã§ããããªãã¼ã·ã§ã³ã¯é¢ä¿ããã¾ããã
ç°¡åãªä¾
Webã¢ããªã±ã¼ã·ã§ã³ã§éµä¾¿çªå·ãæå®ãããã©ã¼ã ãèãã¾ãããã
æ¥æ¬ã®éµä¾¿çªå·ãæå®ãããã©ã¼ã ã®è¨è¨ã§ããè¦ããã®ã¯å¤§ããåãã¦2éããããä¸3æ¡ã¨ä¸4æ¡ãå¥ã
ã«å
¥åããããã®ã¨ã1ã¤ã®ãã©ã¼ã ã«ã¾ã¨ãã¦å
¥åããããã®ã§ããä½æããè£å®ãããè¨è¨ããããã¾ããããã§ã¯ããã¦ããã¾ãã
<input type="text" name="postal_code_1"> -
<input type="text" name="postal_code_2">
<input type="text" name="postal_code"> ä¾ï¼100-0001
ã©ã¡ããé¸ãã§ãããã®ã§ãããé¸ãã è¨è¨ã«ãã£ã¦ä»æ§ãå¤ãããã¨ã«æ³¨æãã¦ãã ããã
ã¯ã¨ãªãã©ã¡ã¿ postal_code_1 ãåãåã£ããµã¼ãã¯æ°å¤ã®ã¿ã妥å½ãªå
¥åã¨ãã¦åãå
¥ããä»æ§ã¨ãªãã§ãããããã®éã«ããããåè§æ°åã®ã¿ãåãå
¥ããã®ããããããå
¨è§æ°åãå
¥åããããé©åã«å¤æãã¦ãããã®ãã決ããå¿
è¦ãããã¾ãã
postal_codeã®å ´åã¯æ°å¤ã®ã¿7æ¡ã®å
¥åãåãå
¥ãããããã¤ãã³ãå¿
é ã¨ããããããããå
¨è§ã®ãã¤ãã³ã¯ã©ãããªã©ãããã«ææ決å®ããå¿
è¦ãããã¾ãã
ããã§ã¯ä»¥ä¸ã®ä¸é£ã®ææ決å®ããããããå ±æãã¦ä»æ§ã¨ããã¨ãã¾ãã
- ã¯ã¨ãªãã©ã¡ã¿ postal_code_1 ã¯éµä¾¿çªå·ä¸3æ¡ã®æ°å¤æååãåãåã
- ã¯ã¨ãªãã©ã¡ã¿ postal_code_2 ã¯éµä¾¿çªå·ä¸4æ¡ã®æ°å¤æååãåãåã
- ããããå ¨è§æåã®å ¥åã«ã¤ãã¦ã¯ãããããå ¨è§æ°å¤ãUTF8ã§è¡¨ç¾ããã¦ããå ´åã®ã¿åãå ¥ããä¸ã§ãµã¼ãå´ã§ããããåè§æ°å¤ã«å¤æãã
- å ¥åãæ°å¤ã®ã¿ã§æ§æããã¦ãããå¿ è¦ãªæ¡æ°ã«æºããªãå ´åã¯ãã7æ¡ã®éµä¾¿çªå·ãå ¥åãã¦ãã ãããã¨ããã¡ãã»ã¼ã¸ã表示ãã¦åå ¥åãä¿ã
- ãã以å¤ã®å ¥åã«ã¤ãã¦ã¯ããéµä¾¿çªå·ãå ¥åãã¦ãã ãããã¨ããã¡ãã»ã¼ã¸ã表示ãã¦åå ¥åãä¿ã
ããã§è¡ãããªãã¼ã·ã§ã³ã¯ãpostal_code_1/postal_code_2 ããããããæ°å¤ãã®ã¿ã§æ§æããã¦ãããã©ãããå¿
è¦ãªæ¡æ°ã§ãããã©ãããã«ãªãã¾ãã
ããã¾ã§ã®ä»æ§ããã ããªãã¨ããªãã¼ã·ã§ã³ï¼å¦¥å½æ§æ¤è¨¼ï¼ã¯å®ç¾©ã§ããªããã¨ã«æ³¨æãã¦ãã ãããä½ããã£ã¦å¦¥å½ã¨ãããã¯ä»æ§ãå®ãããã®ã§ãããå¾ã£ã¦ä»æ§ããªããã°å¦¥å½æ§ãè«ãããã¨ã¯ã§ãã¾ãããã¾ããpostal_codeã«7æ¡åå
¥åãããå ´åã¯å
¨ãç°ãªãããªãã¼ã·ã§ã³ãè¡ããã¨ã«ãªãã¾ãã
ããªãã¼ã·ã§ã³ãã»ãã¥ãªãã£å¯¾çã¨ã¯ç°ãªãå ·ä½ä¾
ãã¤ã¯ãããã°ãµã¼ãã¹ãä½ãã¨ãã¾ãããã
- ã¦ã¼ã¶ã®ã¤ã¶ããã¯æ大140æåï¼ãã¤ãæ°ã§ã¯ãªãæåæ°ï¼
- 空ã®ã¤ã¶ããã¯åãã¤ããªã
- 妥å½ãªUTF8ã¨ã³ã³ã¼ãã£ã³ã°æååã®ã¿ãåãã¤ãã
- 140æåãè¶ ããã¤ã¶ããã¯ã¡ãã»ã¼ã¸ã表示ãã¦åå ¥åãä¿ã
ãã®ä»æ§ã®ãã¨ã§ã§ããããªãã¼ã·ã§ã³ã¯
- ã¤ã¶ããã®ã¨ã³ã³ã¼ãã£ã³ã°ã妥å½ãã©ãã
- æåæ°ã1æå以ä¸140æå以ä¸ã§ãããã©ãã
ã ãã§ããã"K&R" from
ãã®å
¥åã«ã¯SQLãHTMLã¨ãã¦ç¹å¥ãªæå³ãæã¤æåãå«ã¾ãã¦ãã¾ãããããããRDBMSã¨ã®ããåãã§é©åã«ãã¬ã¼ã¹ãã«ãã使ç¨ãã¦ããããHTMLã¨ãã¦åºåããç´åã«HTMLã¨ãã¦ç¹å¥ãªæå³ãæã¤æåãé©åã«ã¨ã¹ã±ã¼ããã¦ããã°æ°ã«ããå¿
è¦ã¯ããã¾ããã
ããã§ããå±éºï½ï½ï½ãªæååãåé¤ããã°ããããããï¼ï½ï½ï½ä¿ºå¤©æï½ï½ï½ï½ï½ãã¨ãã¦ãKR from OReillyãã®ããã«ãã¦ãã¾ãæè¡ãªã©ãä½ãæ£ãããç解ããªãã§ä¸é©åãªå¯¾ççæ³ãã¨ããã¨ãå°éç¨èªã§ããµãã¿ã¤ãºè³ãã¨è¨ãã¾ãã
ããªãã¼ã·ã§ã³ã¯ä»æ§ã®åé¡ã§ãã£ã¦ã»ãã¥ãªãã£å¯¾çã§ã¯ãªãã¨ããã®ã¯ããããæå³ã§ããç¹°ãè¿ãã«ãªãã¾ãããSQLã¤ã³ã¸ã§ã¯ã·ã§ã³ã®å¯¾çã¯ã1. SQLãæååçµåã§ä½ããªã 2. ãã¬ã¼ã¹ãã«ãã使ã ã§ããããªãã¼ã·ã§ã³ã¯é¢ä¿ããã¾ããã
æååçµåã§SQLãæ§ç¯ãããªã©ä¸é©åãªåãæ±ãããã¦ããå ´åã¯ãä¸è¨ã®ãããªä»æ§ã§ã¯ããªãã¼ã·ã§ã³ãè¡ãªã£ã¦ãããããªSQLã«ãªããã¨ã§ãããã
#tqrk03 ã§çºè¡¨ããå 容ãã¢ã¬ã ã£ãã®ã§ãç¡éã¹ããªã¼ã ãrubyã§ãã£ã
æ±æ¥Rubyä¼è°(#tqrk03)ã«éã³ã«è¡ã£ããã@tadasyã®ç½ ã«ãã¡ããã¦ç¡æå³ã«3åLTãããã¨ã«ãªã£ãã
ãªãã®æºåããã¦ãªãã£ãã®ã§ã空æ°ãèªã¾ãã«åã®è¨äºã«æ¸ããHaskellããããã£ã¦ãã話ããããã¨ã«ããã
ã ã£ã¦Rubyããããã¼ãã ããã
ãã§ãã¿ããªé
ãå
¥ã£ãå¢ãã§ç¬ã£ã¦ããä½ä¸ã¤æå³åãããªãã£ãã¨æãããã®ã§ãç¡éã¹ããªã¼ã ã®ãã¤ãRubyã«ç§»æ¤ãããç§ã¯Rubyã¾ã£ããããããªãã®ã§ããªããããããªæ¸ãæ¹ã ã£ããæãã¦ã»ããã
ããã°ã©ã ã ãæ¸ãã¦åå°½ããã®ã§ã解説ãå¿
è¦ãªäººã¯SICPãèªãã§ã»ããã
class Stream def initialize(car, &cdr) @car = car @cdr = cdr end def car @car end def cdr @cdr.call end def take(n) (n == 0) ? [] : (cdr.nil?) ? [@car] : cdr.take(n-1).unshift(@car) end def ref(n) (n == 0) ? @car : cdr.nil? ? nil : cdr.ref(n-1) end def map(&f) Stream.new(f.call(@car)) { cdr.map(&f) } end end class << Stream def zipWith(s1, s2, &f) car = f.call(s1.car, s2.car) new(car) { zipWith(s1.cdr, s2.cdr, &f) } end def repeat(x) new(x) { repeat(x) } end def integerStartinFrom(n) new(n) { integerStartinFrom(n+1) } end def integer integerStartinFrom(1) end def add(s1, s2) zipWith(s1, s2) {|a, b| a + b } end def mul(s1, s2) zipWith(s1, s2) {|a, b| a * b } end def partialSums(s) new(s.car) { add(repeat(s.car), partialSums(s.cdr)) } end end class Series end class << Series def integrate(s) Stream.zipWith(s, Stream.integer) {|a, b| a / b } end def exp Stream.new(1.0) { integrate(exp) } end def cos Stream.new(1.0) { integrate(sin.map{|a| -a }) } end def sin Stream.new(0.0) { integrate(cos) } end end def sum(array) array.inject(0) {|a, b| a + b } end ones = Stream.repeat(1) integer = Stream.integer p ones.take(10) p integer.take(10) p Stream.add(ones, integer).take(10) p Stream.mul(integer, integer).take(10) p Stream.partialSums(integer).take(10) p Stream.partialSums(Series.exp).ref(10) p Math.exp(1) p Stream.partialSums(Series.sin).ref(10) p Math.sin(1) p Stream.partialSums(Series.cos).ref(10) p Math.cos(1)
Haskellã§SICP3ç« ã®ã¹ããªã¼ã ãæ¸ãã¦ã¿ãããããã£ã
è¨èªä»æ§ãè¦ã¦ããããªãSICP3ç« ã ããã¨æã£ã¦ãã£ã¦ã¿ãããã»ã¨ãã©ã2è¡ï¼ãã¡å宣è¨ã1è¡ï¼ã§æ¸ãã¦ãããããã¨ã§è§£èª¬ã¨ãæ¸ããããããªããæ¸ããªããããããªãã
module Main where addStream :: Num a => [a] -> [a] -> [a] addStream = zipWith (+) scaleStream :: Num a => a -> [a] -> [a] scaleStream factor = map (*factor) mulStream :: Num a => [a] -> [a] -> [a] mulStream = zipWith (*) integersStartingFrom :: Num a => a -> [a] integersStartingFrom n = n : integersStartingFrom (n + 1) ones :: Num a => [a] ones = repeat 1 integers :: Num a => [a] integers = integersStartingFrom 1 partialSums :: Num a => [a] -> [a] partialSums (x:xs) = x : addStream (repeat x) (partialSums xs) integrateSeries :: Fractional a => [a] -> [a] integrateSeries xs = mulStream xs (map recip integers) expSeries = 1.0 : integrateSeries expSeries cosSeries = 1.0 : integrateSeries (map negate sinSeries) sinSeries = 0.0 : integrateSeries cosSeries mulSeries :: Num a => [a] -> [a] -> [a] mulSeries (a:as) (b:bs) = a*b : addStream (scaleStream a bs) (mulSeries as (b:bs)) sqrtStream :: Fractional a => a -> [a] sqrtStream x = guesses where guesses = 1.0 : map improve guesses improve guess = average guess (x/guess) average a b = (a+b)/2 eularTransform :: Fractional a => [a] -> [a] eularTransform (x0:x1:x2:xs) = x' : eularTransform (x1:x2:xs) where x' = x2 - (square (x2 - x1)) / (x0 - 2*x1 + x2) square x = x * x piSummands :: Fractional a => a -> [a] piSummands n = 1.0/n : map negate (piSummands (n+2)) piStream :: Fractional a => [a] piStream = scaleStream 4 (partialSums (piSummands 1)) makeTableau :: ([a]->[a]) -> [a] -> [[a]] makeTableau t xs = xs : makeTableau t (t xs) acceleratedSequence :: ([a]->[a]) -> [a] -> [a] acceleratedSequence t xs = map head (makeTableau t xs) ln2Summands :: Fractional a => a -> [a] ln2Summands n = 1/n : map negate (ln2Summands (n+1)) ln2Stream :: Fractional a => [a] ln2Stream = partialSums (ln2Summands 1) geometricSeris :: Num a => a -> a -> [a] geometricSeris a r = map ((a*).(r^)) (integersStartingFrom 0) polySeries :: Num a => a -> [a] polySeries = geometricSeris 1 applySeries :: Num a => [a] -> a -> [a] applySeries series x = (partialSums (mulStream series (polySeries x))) integral :: Num a => [a] -> a -> a -> [a] integral integrand initial dt = int where int = initial : addStream (scaleStream dt integrand) int solve' :: Num a => (a->a) -> a -> a -> [a] solve' f y0 dt = y where y = integral dy y0 dt dy = map f y solve :: Fractional a => (a->a) -> a -> Int -> a solve f y0 n = (solve' f y0 dt) !! n where dt = 1 / (fromIntegral n) solve2nd :: Num a => a -> a -> a -> a -> a -> [a] solve2nd a b dt y0 dy0 = y where y = integral dy y0 dt dy = integral ddy dy0 dt ddy = addStream (scaleStream a dy) (scaleStream b y) solve2nd_gen :: Num a => (a -> a -> a) -> a -> a -> a -> [a] solve2nd_gen f dt y0 dy0 = y where y = integral dy y0 dt dy = integral ddy dy0 dt ddy = zipWith f dy y