ãåé¡ã®äºå®ã¯ãããªããè² ããããšãã§ããªããšããããšã§ãããšããã³ãã¹ããŒã¯åœåçã«ããæ¯ãã€ããŸããã -ãã³ããŒã¯èš±å¯ããŠããŸããã
I.ã€ã«ããEãããããã ãŽãŒã«ãã³ã«ãŒã ã
å·æ·¡ãªãã©ã¬ã¯éæãªå®¹åšã«äœãã§ãã
ãã®ææãã匷ãã 圌女ã¯ãè ¹ããã¯å¥œãã§ã¯ãªã-å£ããçŽæ¥
é ã«é§ãå¯ããããããå·Šå³ã«æãå§ããŸããã
ã¡ã³ã¿ã«ãã¬ãŒã¹ãšæ ç¹ã®ç Žå£ã
M.ãŠã¹ãã³ã¹ããŒã ç§ãã¡ãããªãå Žæ ã
ãããããæåã«ã¢ãŒã©ã³ã®å匷ãå§ãã人ã¯èª°ã§ããã·ã¥ã©ãã©ã¬ããã®ç«å Žã«ãããšæããŠããã§ãããã Erlangã«ã¯ãå€æ°ãåå²ãåœãŠããçµæã1ã€ã®å€æ°ã«èç©ãããªã©ãã»ãšãã©ã®çŸä»£èšèªã®æŠå¿µã«éŠŽæã¿ããããŸããã ïŒå ¬å¹³ã«ã¯ããã°ããŒãã«ãªè€æ°å€æŽå€æ°ãã®Erlangã¹ã¿ã€ã«ã®åäœãå®è£ ã§ããããšã«æ³šæããŠãã ããããã®ãããåããã»ã¹ã«ã¯ããã°ã©ãå®çŸ©ã®ããŒãšå€ã®ãã¢ãæ ŒçŽããããã·ã¥ãã£ã¯ã·ã§ããªããããŸããçµã¿èŸŒã¿é¢æ°putïŒKeyãValueïŒãget ïŒããŒïŒãšä»ã®ããã€ãã®è£å©é¢æ°ã§ãããã¢ããªã±ãŒã·ã§ã³ã§ãã®ãããªèŸæžã䜿çšããã®ã¯æªãã¹ã¿ã€ã«ãšèŠãªãããäŸå€çãªå Žåã«ã®ã¿æšå¥šãããŸãïŒ http://www.erlang.org/doc/man/erlang.html\#put-2 ïŒïŒã çµæãšããŠãã«ãŒãå ã®å埩ã¯ãå埩å€æ°ã®å€ã®éåžžã®çºçã§ã¯å®è£ ã§ããŸããã çµæã®çŽ¯ç©ã¯ãååž°ãããã³æ«å°Ÿååž°ã«ãããµã€ã¯ã«ã®ç·šæã«ãã£ãŠã®ã¿å®è¡ãããŸãã ïŒãã¡ãããã«ãŒãã®çµæã®å埩ãšçŽ¯ç©ã¯ããªã¹ãã®ã©ã€ãã©ãªé¢æ°ïŒforeachïŒé¢æ°ããªã¹ãïŒããªã¹ãïŒfoldlïŒé¢æ°ãStartValueããªã¹ãïŒããªã¹ãïŒfoldrïŒé¢æ°ãStartValueããªã¹ãïŒïŒ http ïŒ//www.erlang.org/doc/man/lists.html ïŒããã³ã»ããã®é¡äŒŒç©ïŒ http://www.erlang.org/doc/man/sets.htmlãhttp://www.erlang.org /doc/man/ordsets.htmlãhttp://www.erlang.org/doc/man/gb_sets.html ïŒããã³é åïŒ http://www.erlang.org/doc/man/array.html ïŒãç§ãã¡ã®ç®æšã¯ãæ¢æã®ãœãªã¥ãŒã·ã§ã³ã䜿çšããã«ããµã€ã¯ã«ãèšè¿°ããæ¹æ³ãåŠã¶ããšã§ãããã®ãããããã§ã¯ãã®ãããªã©ã€ãã©ãªã®äœ¿çšãæ§ããŸãã
ãããã£ãŠãErlangã§ã¯ãéåžžã®æèãã¿ãŒã³ãç Žãããã®ããã°ã©ãã³ã°èšèªã®ã¿ã«ç¹åŸŽçãªæ°ãããã¿ãŒã³ã«çœ®ãæããå¿ èŠããããŸãã ãã¡ãããçæ³çãªæ²»çæ³ã¯ããã¹ãŠã®ã粟ç¥çãµããŒããšåŒ·åããç Žãããšãã§ããæŽè³ã§ãã ããããç§ãã¡ã«ãšã£ãŠã¯ãããã¯ããããéæ¿ãããæ段ã§ãããéã®æ¹åã«é²ãã§ãããã
èã¢ã³ãœããŒå€§çã®äººçã«ã¯ã圌ã®åŒåã®äžäººã«ã€ããŠã®ç©èªããããŸãã åŒåãæäŒã«ç«ã¡ãèã¢ã³ãããŠã¹ãè©©salãèªãã§ããã®ãèããŸããã æåã®è©©ç·šã®æåã®è©©ã鳎ããšããã«ïŒ
, ...
åŠçã¯ç¥æ®¿ãå»ããŸããã ãã以æ¥ã誰ã圌ã30幎è¿ãèŠãŠããŸããããããŠã圌ãç¥æ®¿ã«åã³çŸãããšããã¢ã³ãœããŒå€§çã¯ãªã圌ããããªã«é·ãæ®ããã©ãã«æ¶ããã®ãå°ããŸããã åŒåã¯çããŸããããç¶ãè©©ç·šã®èšèãèããŠãèéã«åŒéããŠããããã®èšèã§èšãããŠããããšãæãéããããšããŸããã éªæªãªèãã®è©è°äŒã«è¡ããªãã§ãã ãããã èšãæããã°ã圌ã¯ãããã®èšèããå®è·µçãªæèšãåŠã³ãä»ã§ã¯ããã«èªãããã«ãªããŸããã æ®å¿µãªãããç§ãã¡ã«ã¯ãã®ãããªæéã®äœè£ã¯ãªããç§ãã¡ã®ç®æšã¯ããã»ã©é«ããããŸããã ããããåºæ¬æŠå¿µã¯æ¡çšã§ããŸãã
2ã€ã®æšæºçãªçµã¿åããã®åé¡ãæ€èšããŸãã
- Nåã®èŠçŽ ã®æå®ãããã»ãããããã¹ãŠã®å¯èœãªé åãæ€çŽ¢ããŸã
- Nåã®èŠçŽ ã®æå®ãããã»ããããå¯èœãªãã¹ãŠã®çµã¿åãããæ€çŽ¢ããŸã
Erlangããã°ã©ãã³ã°èšèªã䜿çšããŠãããã解決ããããã®ããŸããŸãªã¢ãããŒããšæ¹æ³ãåæããå ·äœçãªäŸã䜿çšããŠãã®èšèªã®ããã°ã©ãã³ã°ã®æ©èœãç解ããç¿åŸããŸãã
ãã¹ãŠã®äŸã¯combinat.erlã¢ãžã¥ãŒã«ã«åéãããŠããã https ïŒ //github.com/raven29/combinat_erl.gitã§å ¥æã§ããŸãã
2ã€ã®ç®æš-2ã€ã®ååž°
ãœãªã¥ãŒã·ã§ã³ã¢ã«ãŽãªãºã ãšããŠãçŽæ¥ååž°åæã䜿çšããŸãã ããã¯ãååž°ã®åã¹ãããã§å®è¡ãããããšãæå³ããŸãã
- ãªã¹ããããã¹ãŠã®å¯èœãªå€ãã«ãŒããããããã®åå€ãæ¢åã®çµæã«è¿œå ããŸãã
- æ¢ã«äœ¿çšãããŠããå€ãé€å€ãããæ°ãããªã¹ãã®è»¢éã䌎ãååž°åŒã³åºãã
ã€ãŸãããã®æé ã¯2ã€ã®æ®µéã§æ§æãããŸãïŒååž°ã®ç¹å®ã®ã¬ãã«ã§ã®å·¡åæ€çŽ¢ãšæ¬¡ã®ã¬ãã«ãžã®ç§»è¡ã ãããã£ãŠããããã®2ã€ã®ç®æšã«ã¯äºéååž°åŒã³åºããå¿ èŠã§ãã ïŒã¢ã«ãŽãªãºã ã¯åçŽãªåæã«åºã¥ããŠãããããæé©åãå¿ èŠã§ãããã ããæåã«ãã®åçŽããšæ確ãã«ããã䜿çšãããæ¹æ³ãšææ³ãç°¡åã«èª¬æã§ãããããæ€èšäžã®äž¡æ¹ã®åé¡ã«é©çšããããããç®çã«éåžžã«é©ããŠããŸãæå°éã®éãã§ãé©åãªæ¯èŒãšé¡äŒŒãè¡ãããšãã§ããŸãïŒã
åºæ¬å®è£
åºæ¬æ©èœã¯ãcombinatïŒpermuts_outïŒListãNumberïŒããã³combinatïŒcombs_outïŒListãNumberïŒé¢æ°ã§å®è£ ãããŸãããããã®é¢æ°ã¯ãããããããªã¹ãã®èŠçŽ ããã®ãã¹ãŠã®é åããã³ãã¹ãŠã®æ°å€ã®é·ãã®çµã¿åãããåºåããŸãã 以äžã¯ãé åãåºåããpermuts_outïŒãªã¹ããæ°å€ïŒé¢æ°ã§ãã ãã®é¢æ°ã¯2åååž°çã«åŒã³åºãããŸãã6è¡ç®-ã«ãŒãã¹ã«ãŒã7è¡ç®-次ã®ååž°ã¬ãã«ã«ç§»åããŸãã ãã®æåŸã®åŒã³åºãã§ãçµæãå€æ°[RemainH | Result]ã§å¢åãããããã«æ¬¡ã®ã¬ãã«ã®ååž°ã«æž¡ãããäžè¬ãªã¹ããããã®çµæã«å«ãŸããèŠçŽ ãé€å€ãããŸãã Listã®4çªç®ã®åŒæ°ã¯ãé åã®å Žåã«ã®ã¿å°äœãæ£ããèšç®ããããã«å¿ èŠãªé ç®ã®ãœãŒã¹ãªã¹ãã§ãã
permuts_out(List, Number) -> permuts_out(List, [], Number, List). permuts_out(_Remain, Result, Number, _List) when length(Result) == Number -> io:format("~w~n", [Result]); permuts_out([], _Result, _Number, _List) -> ok; permuts_out([RemainH|RemainT], Result, Number, List) -> permuts_out(RemainT, Result, Number, List), permuts_out(List -- [RemainH|Result], [RemainH|Result], Number, List).
çµã¿åããã®åæ§ã®é¢æ°ã¯ãéä¿¡ãããå°äœãèšç®ããããã®ããåçŽãªã«ãŒã«ã§ã4çªç®ã®åŒæ°ãªã¹ãããªãå Žåã®ã¿ã以åã®é¢æ°ãšç°ãªããŸãã
combs_out(List, Number) -> combs_out(List, [], Number). combs_out(_Remain, Result, Number) when length(Result) == Number -> io:format("~w~n", [Result]); combs_out([], _Result, _Number) -> ok; combs_out([RemainH|RemainT], Result, Number) -> combs_out(RemainT, Result, Number), combs_out(RemainT, [RemainH|Result], Number).
2ã€ã®ååž°-2ã€ã®é¢æ°
ããæ確ã«ããããã«ã2ã€ã®ååž°åŒã³åºãã2ã€ã®ç°ãªãé¢æ°ã§è¡šãããšãã§ããŸãã é¢æ°åã®æ«å°Ÿã¯ãããããã®ç®çã«å¯Ÿå¿ããŠããŸãã* _iteration-ç¹å®ã®ååž°ã¬ãã«ã§å°äœãªã¹ããå埩åŠçããããã* _recursion-次ã®ååž°ã¬ãã«ã«ç§»åããããã
permuts_out_2(List, Number) -> permuts_out_iteration(List, [], Number, List). permuts_out_iteration([], _Result, _Number, _List) -> ok; permuts_out_iteration([RemainH|RemainT], Result, Number, List) -> permuts_out_iteration(RemainT, Result, Number, List), permuts_out_recursion(List -- [RemainH|Result], [RemainH|Result], Number, List). permuts_out_recursion(_Remain, Result, Number, _List) when length(Result) == Number -> io:format("~w~n", [Result]); permuts_out_recursion(Remain, Result, Number, List) -> permuts_out_iteration(Remain, Result, Number, List). combs_out_2(List, Number) -> combs_out_iteration(List, [], Number, List). combs_out_iteration([], _Result, _Number, _List) -> ok; combs_out_iteration([RemainH|RemainT], Result, Number, List) -> combs_out_iteration(RemainT, Result, Number, List), combs_out_recursion(RemainT, [RemainH|Result], Number, List). combs_out_recursion(_Remain, Result, Number, _List) when length(Result) == Number -> io:format("~w~n", [Result]); combs_out_recursion(Remain, Result, Number, List) -> combs_out_iteration(Remain, Result, Number, List).
ããããããã®ãªãã·ã§ã³ã¯é床ã®ç ©ããããããã¢ã³ããã¿ãŒã³ãšèŠãªãããšãã§ããŸãã
çµæãæ瀺ããŠãã ããïŒ
ã©ã€ãã©ãªé¢æ°ãäœæããå Žåãæšæºã¹ããªãŒã ãžã®åºåã¯ã»ãšãã©åœ¹ã«ç«ã¡ãŸããã çµæãååŸãããããäœããã®åœ¢ã§ã¯ã©ã€ã¢ã³ãã³ãŒãã«æž¡ãå¿ èŠããããŸãã Erlangã«ã¯ãçµæãèç©ããããã®ã°ããŒãã«å€æ°ãéçãã£ãŒã«ãã¯ãããŸããã 代ããã«ãé¢æ°åèšèªã«åºæã®ã¢ãããŒãã䜿çšã§ããŸãã
- äžåãååž°ã§å€ãè¿ã
- ã³ãŒã«ããã¯é¢æ°
- å®è¡ã¹ããªãŒã ãšã¹ãã¬ãŒãžã¹ããªãŒã
åãªãã·ã§ã³ã詳现ã«æ€èšããŠãã ããã
ããã«ã¯ã-æ»ã£ãŠã¯ã
ãããŸã§ãé¢æ°ã®æ¬äœã§äœãæçšãªããšãè¡ã£ãŠïŒã³ã³ãã¥ãŒã¿ãŒç»é¢ã«çµæã衚瀺ããããšãæçšã§ãããšèããããå ŽåïŒãäžéååž°ã«åãã£ãŠããŸãã éæ¹åã®ç§»åã§ã¯ãé¢æ°ã«ãã£ãŠè¿ãããçµæã¯ãŸã£ãã䜿çšãããŸããã§ããã ã€ãŸããäžåãã®ååž°ã®åãã¯ã空ãã§ããã ãã®ã¢ãããŒãã§ã¯ã衚瀺ããããã¹ãŠã®å€ããŸãšããããšã¯äžå¯èœã§ãã ãããã¯æ±ºããŠæ¥ç¶ãããŠããŸããã ããçç£çãªã®ã¯ãååž°ãçµäºãããšãã«é¢æ°ã«ãã£ãŠè¿ãããçµæã䜿çšããããšã§ãã ãã®å Žåãååž°é¢æ°ã®æåã®åŒã³åºãã¯ã环ç©çµæå šäœãè¿ãããšãã§ããŸãã åºæ¬æ©èœã®å€æŽã¯ä»¥äžã®ãšããã§ããã3ã€ã®ãã€ã³ããå«ãŸããŠããŸãã
- æšæºã¹ããªãŒã ã«åºåãã代ããã«çµæ[çµæ]ãè¿ãïŒ3è¡ç®ã11è¡ç®ïŒ
- ååž°ã®äžéšã«ãåæå€ãè¿ãããŸã-okã¢ãã ã®ä»£ããã«ç©ºã®ãªã¹ã[]ïŒè¡4ã12ïŒ
- é 次åŒã³åºãã®ä»£ããã«ãªã¹ãã++ããåèšããããšã«ããçµæã®èç©ïŒ6ã14è¡ç®ïŒ
permuts_resïŒãªã¹ããæ°å€ïŒããã³combs_resïŒãªã¹ããæ°å€ïŒé¢æ°ã¯ããããããã¹ãŠã®é åãšæ°å€ã®é·ãã®çµã¿åãããå«ããªã¹ãã®ãªã¹ããè¿ããŸãã
permuts_res(List, Number) -> permuts_res(List, [], Number, List). permuts_res(_Remain, Result, Number, _List) when length(Result) == Number -> [Result]; permuts_res([], _Result, _Number, _List) -> []; permuts_res([RemainH|RemainT], Result, Number, List) -> permuts_res(RemainT, Result, Number, List) ++ permuts_res(List -- [RemainH|Result], [RemainH|Result], Number, List). combs_res(List, Number) -> combs_res(List, [], Number). combs_res(_Remain, Result, Number) when length(Result) == Number -> [Result]; combs_res([], _Result, _Number) -> []; combs_res([RemainH|RemainT], Result, Number) -> combs_res(RemainT, Result, Number) ++ combs_res(RemainT, [RemainH|Result], Number).
ãããŠåœŒãšäžç·ã«å¥œããªããšãããŠãã ããïŒ
1ã€ã®ã³ã¬ã¯ã·ã§ã³å€æ°ã«çµæãèç©ããã®ã§ã¯ãªããäœæçŽåŸã«åèŠçŽ ã§äœã䟿å©ãªããšãè¡ããšäŸ¿å©ãªå ŽåããããŸãã ãã®ã¢ãããŒãã«ãããçç£æ§ãåäžãããã¡ã¢ãªæ¶è²»ãå€§å¹ ã«åæžã§ããŸãã è¿œå ã®åŒæ°ãšããŠæž¡ãããã³ãŒã«ããã¯é¢æ°ã䜿çšããŠå®è£ ã§ããŸãã é¢é£ãããªãã·ã§ã³ã以äžã«ãªã¹ãããŸãã
permuts_clb(List, Number, Callback) -> permuts_clb(List, [], Number, List, Callback). permuts_clb(_Remain, Result, Number, _List, Callback) when length(Result) == Number -> Callback(Result); permuts_clb([], _Result, _Number, _List, _Callback) -> ok; permuts_clb([RemainH|RemainT], Result, Number, List, Callback) -> permuts_clb(RemainT, Result, Number, List, Callback), permuts_clb(List -- [RemainH|Result], [RemainH|Result], Number, List, Callback). combs_clb(List, Number, Callback) -> combs_clb(List, [], Number, Callback). combs_clb(_Remain, Result, Number, Callback) when length(Result) == Number -> Callback(Result); combs_clb([], _Result, _Number, _Callback) -> ok; combs_clb([RemainH|RemainT], Result, Number, Callback) -> combs_clb(RemainT, Result, Number, Callback), combs_clb(RemainT, [RemainH|Result], Number, Callback).
Callbackå€æ°ã§ã¯ã1ã€ã®åŒæ°ããã®é¢æ°ã®ååãæž¡ãããšãã§ããŸãïŒã¢ãŒã©ã³1-ã¢ãŒã©ã³ã®çšèªã«ãããšïŒã ãããã£ãŠãããšãã°ãèŠçŽ [1,2,3] 2ã®ãã¹ãŠã®é åãå°å·ããããã«åŒã³åºãããšãã§ããŸãã
combinat:permuts_clb([1,2,3], 2, fun(X)->io:format("~w~n",[X]) end).
ã³ãŒã«ããã¯é¢æ°ã®ããæå³ã®ããã¢ããªã±ãŒã·ã§ã³ã«ã€ããŠã¯ã次ã®ã»ã¯ã·ã§ã³ã§èª¬æããŸãã
ããã°ãã©ã¶ãŒã¯ããªããèŠãŠããŸã
Erlangã§ã®åå²ååž°ã®çµæã®çŽ¯ç©ãå®è£ ããå¥ã®æ¹æ³ã¯ã2ã€ã®ã¹ã¬ããã䜿çšããããšã§ãã 1ã€ã®ã¹ã¬ããããšã°ãŒãã¥ãŒã¿ãŒã§ãããããã°ã©ã ãå®è¡ãããŸãã å¥ã®ã¹ã¬ããã¯ãªãã¶ãŒããŒã§ãããååž°é¢æ°ãçµæãæž¡ããŸãã å®è¡äžã®ã¹ã¬ãããäœæ¥ãå®äºãããšãç£èŠã¹ã¬ããã¯åèšçµæã衚瀺ããŸãã éèŠïŒå®è¡ã¹ã¬ãããšããŠãerlã·ã§ã«ãå®è¡ãããŠããã¡ã€ã³ã¹ã¬ããïŒã¹ãŒããŒãã€ã¶ãŒïŒã䜿çšã§ããŸããã ãã®ã¹ã¬ããã¯ãããã°ã©ã ã®å®è¡åŸã«ç Žæ£ãããŸããã erlã¢ããªã±ãŒã·ã§ã³ãã¢ã³ããŒãããããŸã§ååšãç¶ããŸãã
以äžã¯ã察å¿ããå®è£ ã§ãã 3è¡ç®ã¯ã梯åã®åºå£ããèšå®ããŸããããã«ãããæ£åžžã«å®äºããå Žåã§ããé¢é£ããããã»ã¹ããã®ã¡ãã»ãŒãžã®éä¿¡ãä¿èšŒãããŸãã ããã©ã«ãã§ã¯ãtrap_exitãã©ã°ã¯falseã«èšå®ãããŠããŸããããã¯ãç°åžžçµäºãçºçããå Žåã«ã®ã¿ãé¢é£ããããã»ã¹ããã¡ãã»ãŒãžãåä¿¡ããããšãæå³ããŸãã 5è¡ç®ã§ã¯ãå®è¡äžã®ã¹ã¬ãããéå§ïŒããã³åæã«ãã€ã³ãïŒããŸãã ãã®ã¹ã¬ããã§ã¯ãpermuts_clbïŒãŸãã¯combs_clbïŒé¢æ°ãèµ·åãããåŒæ°ListãNumberãããã³åãŠãããã®çµæããªãã¶ãŒããŒããã»ã¹ã«æž¡ãCallbackã³ãŒã«ããã¯é¢æ°ãåãåããŸãã
fun(R)->Supervisor!R end
6è¡ç®ã§ã¯ãã«ãŒãïŒ[]ïŒé¢æ°ã¯åèšçµæã®ç©ºã®åæå€ã§éå§ããŸãã ãã®é¢æ°ã¯ãå®è¡äžã®ã¹ããªãŒã ããã®ã¡ãã»ãŒãžããªãã¹ã³ããŸãã 次ã®çµæãåä¿¡ãããšãã«ãŒãïŒTotal ++ [Result]ïŒãååž°çã«åŒã³åºããïŒ14è¡ç®ïŒãåŒæ°ã«å®è¡äžã®ã¹ããªãŒã ããæ°ããå°çããçµæãè¿œå ãããŸãã å®è¡äžã®ã¹ã¬ããã®äœæ¥ãå®äºãããšããã©ããŒã«ããçµäºããçºçããŸããç¹æ®ã¿ã€ãã®ã¿ãã«ïŒ10è¡ç®ïŒãã«ãŒãïŒïŒã«æž¡ããããã¿ãŒã³ãããã³ã°ã®çµæãšããŠãå šäœã®çµæã衚瀺ããïŒ11è¡ç®ïŒãå®è¡ã¹ããªãŒã ãšã®æ¥ç¶ãåæãããŸãïŒ12è¡ç®ïŒ ïŒ ã¹ãŒããŒãã€ã¶ãŒ-ãªãã¶ãŒããŒã¹ã¬ããã®pidãã¯ãŒã«ãŒ-ãšã°ãŒãã¥ãŒã¿ãŒã¹ã¬ããã®pidã
%% Function = permuts_clb | combs_clb proc(Function, List, Number) -> process_flag(trap_exit, true), Supervisor = self(), spawn_link(combinat, Function, [List, Number, fun(R)->Supervisor!R end]), loop([]). loop(Total) -> receive {'EXIT', Worker, normal} -> io:format("~w~n", [Total]), unlink(Worker); Result -> loop(Total ++ [Result]) end.
ãã®é¢æ°ã¯ã解決ãããã¿ã¹ã¯ã«å¿ããŠãpermuts_clbãŸãã¯combs_clbãæåã®åŒæ°ãšããŠåŒã³åºããŸãã ããšãã°ãèŠçŽ [1,2,3]ããã®ãã¹ãŠã®é åã®2ã«ããåºåã¯ãåŒã³åºãã«ãã£ãŠè¡ãããŸãã
combinat:proc(permuts_clb, [1,2,3], 2).
åå¿è ã«å žåçãªãšã©ãŒããããããããŸããã ãŸããspawn_linkïŒïŒã®åã«loopïŒïŒãå®è¡ã§ããŸããã å®è¡ã¹ã¬ãããéå§ãããåã«èµ·åããããªã¹ããŒé¢æ°ã¯ããããããã®ã¹ã¬ããããã®åäžã®ã¡ãã»ãŒãžãèŠéããªããããããã¯ããä¿¡é Œæ§ãé«ããšæãããŸãã ãããããã®çµæãããã»ã¹ã¯ã«ãŒãïŒïŒã§ãã³ã°ãã次ã®è¡ã¯åŒã³åºãããŸããã 次ã«ãã¹ãŒããŒãã€ã¶ãŒå€æ°ã䜿çšããŠãªãã¶ãŒããŒã¹ã¬ããã«ã¡ãã»ãŒãžãéä¿¡ããããšãå¿ é ã§ãã
self()!R
å®è¡äžã®ã¹ããªãŒã ã§åŒã³åºããããšselfïŒïŒé¢æ°ãå®è¡ãããããã«å¿ããŠå®è¡äžã®ã¹ããªãŒã ã®pidãåãå ¥ãããããåäœããŸããã ãããã®ã³ã¡ã³ãã«å¯Ÿãã建èšçãªæ¹å€ãããŠãããw495ãšEvilBlueBeaverã«æè¬ããŸãïŒãããŠãããç解ããã®ãå©ããŠãããã ãã§ãïŒã
ãããŠãã1ã€å°ããªã³ã¡ã³ãïŒprocïŒïŒé¢æ°ãè©ŠããšãããŸããŸãªå¥åŠãªããšãèµ·ãããŸããããšãã°ãé¢æ°ããé 延ãã䌎ãçµæãçæãå§ããå ŽåããããŸãã ååŒã³åºãã§ãåã®åŒã³åºãã®çµæãè¿ããŸãã ããã¯ãã¡ã€ã³ã¹ã¬ããããªãã¶ãŒããŒã¹ã¬ãããšããŠèµ·åããŠããããã§ããå¯èœæ§ããããŸãã ãããã£ãŠãé害ãçºçãããšã次ã®loopïŒïŒã®åŒã³åºãã¯ãæåŸã®åŒã³åºãïŒååšããå ŽåïŒããã®ãã¹ãŠã®ã¡ãã»ãŒãžãæåã«åŠçããŸãã ãã®æå³ã§ãspawnïŒïŒãŸãã¯spawn_linkïŒïŒé¢æ°ã«ãã£ãŠçæãããå¥ã®ã¹ããªãŒã ã«ããªã¹ããŒã¹ããªãŒã ãå®è£ ããæ¹ãä¿¡é Œæ§ãé«ããªããŸãã
ç解ããããšã¯ãæå¹ã«ããããšã§ã
äžéšã®ããã°ã©ãã³ã°èšèªã«ã¯ãã ãªã¹ãå å è¡šèš ããšåŒã°ããæ§ææ§æäœããããŸãã ããã«ãããã³ã³ãã¯ãã§ãšã¬ã¬ã³ããªåœ¢åŒã§ãªã¹ãã®å埩走æ»ãæå®ã§ãããã®çµæãæ°ãããªã¹ããçæãããŸãããã®çµæãå ã®ãªã¹ãã®åèŠçŽ ã«é¢æ°ãé©çšããããšã«ãããåèŠçŽ ãå ã®ãªã¹ãããååŸãããŸãã ãã®èšèšã¯ãæ°åŠçéåè«ã®è¡šèšæ³ã«åºã¥ããŠããŸãã ããã§ãããšãã°ããªã¹ãå å æ§æã§ã¯ã1ãã9ãŸã§ã®ãã¹ãŠã®æŽæ°ã®2ä¹ã®åºåã¯æ¬¡ã®ããã«ãªããŸãã
[X*X || X <- [1,2,3,4,5,6,7,8,9]].
ãªã¹ãå å è¡šèšã§ã¯ãè€æ°ã®ãªã¹ããæž¡ããŠæ¡ä»¶ã課ãããšãã§ããŸãã äŸãšããŠã1ãã9ãŸã§ã®ä¹ç®ããŒãã«ã®åºåãèããŸãã
[io:format("~w * ~w = ~w~n", [I, J, I*J]) || I <- [1,2,3,4,5,6,7,8,9], J <- [1,2,3,4,5,6,7,8,9]].
å åã®é åã䌎ãå埩çµæãé€å€ãããŠããä¹ç®è¡šïŒ
[io:format("~w * ~w = ~w~n", [I, J, I*J]) || I <- [1,2,3,4,5,6,7,8,9], J <- [1,2,3,4,5,6,7,8,9], I < J].
ãã·ã¢æåŠã§ã¯ãããªã¹ãã®ç解ãã¯ããªã¹ãã®å å«ããããªã¹ãçæããšç¿»èš³ãããŸãã 翻蚳ãç解ãã®äž»ãªæå³ã¯ãç解ãããããç解ãããã§ãã ã ãããè±èªã§ç解ããããšã¯å«ããããšã§ãã
ç解æ§é ã¯ãªã¹ãã ãã§ãªããä»ã®ã¿ã€ãã®ã³ã¬ã¯ã·ã§ã³ã«ãååšããããšã«æ³šæããŠãã ããã Erlangã«ã¯ãªã¹ãå å è¡šèšãšãã€ããªå å è¡šèšããããŸãã
ãã®çš®ã®äžã§æããšã¬ã¬ã³ããª
ãªã¹ãå å æ§é ã§ã¯ããªã¹ãã®å埩走æ»ãæå®ã§ããŸãããã®çµæãåºæ¬é¢æ°ã¯æ¬¡ã®åœ¢åŒãåããŸãã
permuts_comp(List, Number) -> permuts_comp(List, [], Number). permuts_comp(_Remain, Result, Number) when length(Result) == Number -> io:format("~w~n", [Result]); permuts_comp(Remain, Result, Number) -> [permuts_comp(Remain -- [R], [R] ++ Result, Number) || R <- Remain].
permuts_compé¢æ°ã¯ããªã¹ãå å è¡šèšããèªèº«ãååž°çã«åŒã³åºããŸãã
ããã¯ãããããæããšã¬ã¬ã³ããªçœ®æé¢æ°ã§ãã
ã§ããªãããæ¬åœã«ãããå Žå...
æ®å¿µãªããã以åã®çµæãçµã¿åããã®é¢æ°ã«äžè¬åããããšã¯ããã»ã©æçœã§ã¯ãããŸããã å®éããã®å Žåã®ãªã¹ãå å è¡šèšã§ã¯ããªã¹ãå šäœã§ã¯ãªããåã®åŒã³åºãããã®çªå·ã«ãã£ãŠæ±ºå®ãããæ®ãã®ã¿ã転éããå¿ èŠããããŸãã ãªã¹ãã®ä»£ããã«é åãããã°ãç®çã®ã€ã³ããã¯ã¹ãç°¡åã«èšç®ã§ããŸãã ããããé åã¯åºæ¬çãªErlangåã§ã¯ãããŸããã ã¢ã¬ã€ã©ã€ãã©ãªã䜿çšããããã¢ã¬ã€ãæåã§æŽçããŸãã
ããã¯éåžžã«ç°¡åã§ããããšããããã察å¿ããå®è£ ã以äžã«ç€ºããŸãã å ã®ãªã¹ãããListIndexedã¿ãã«ã®ãªã¹ããäœæããŸããåèŠçŽ ã«ã¯å ã®ãªã¹ãã®èŠçŽ ãšæŽæ°ã€ã³ããã¯ã¹ãå«ãŸããŸãïŒ2è¡ç®ïŒã é¢æ°ãªã¹ãïŒæšæºãªã¹ãã¢ãžã¥ãŒã«ã®zipïŒãªã¹ã1ããªã¹ã2ïŒã¯ããã®ãããªå€æã«é©ããŠããŸãã çµæãåºåãããšããlistsïŒunzipïŒListIndexedïŒé¢æ°ã䜿çšããŸãããã®é¢æ°ã¯ãã€ã³ããã¯ã¹ãªãã®å ã®å€èŠ³ã®ã€ã³ããã¯ã¹ä»ããªã¹ããè¿ããŸãïŒ5è¡ç®ïŒã ãããŠæãéèŠãªããš-ãªã¹ãå å è¡šèšã§ã¯ãå埩ã«å«ãŸããã€ã³ããã¯ã¹ã«å¿ èŠãªå¶éãç°¡åã«æå®ã§ããããã«ãªããŸããïŒ11è¡ç®ïŒã
combs_comp(List, Number) -> ListIndexed = lists:zip(List, lists:seq(1, length(List))), combs_comp(ListIndexed, [], Number). combs_comp(_Remain, Result, Number) when length(Result) == Number -> {ResultValue, _I} = lists:unzip(Result), io:format("~w~n", [ResultValue]); combs_comp(Remain, [], Number) -> [combs_comp(Remain -- [R], [R], Number) || R <- Remain]; combs_comp(Remain, [{HValue,HIndex}|T], Number) -> [combs_comp(Remain -- [{R,I}], [{R,I}] ++ [{HValue,HIndex}|T], Number) || {R,I} <- Remain, I > HIndex].
ããã¯ããåä»ã«èŠããããã¯ã©ã€ãã©ãªé¢æ°ãªã¹ãã«é Œããªããã°ãªããªãã£ãç§ãã¡ã®äŸã®äžã§å¯äžã®ããã°ã©ã ã§ãïŒzipïŒList1ãList2ïŒããªã¹ãïŒunzipïŒListTupleïŒããªã¹ãïŒseqïŒStartValueãLengthïŒã ãã®è©Šã¿ã¯ãã¢ã³ããã¿ãŒã³ã®äŸãšèããããšãã§ããŸãã æå ã®ã¿ã¹ã¯ã§ã¯ãé åã¢ãžã¥ãŒã«ã䜿çšããæ¹ãäžè²«æ§ããããŸãããããã¯å¥ã®è©±ã«ãªããŸã...