ã¯ããã«
.NETããã°ã©ãã³ã°ã«äœããã®åœ¢ã§é¢ä¿ããŠãã人ãªã誰ã§ããVisual Studioã®æ¬¡ã®ããŒãžã§ã³ã§ã¯æ°ããããã°ã©ãã³ã°èšèªãæ¢ã«çµã¿èŸŒãŸããŠããããšãç¥ã£ãŠããŸãã FïŒãåãªãFNPãã¯ããã«è¶
ããŠããããšã瀺ãããã«ïŒFNPã ãããããããããŸããïŒã次ã®ãã¹ãŠãæžããŸããã
ãã®èšäºã¯ãããªãã®é·ãã«ãé¢ããããèšèªã®å®å
šãªæ©èœãå®å
šã«èª¬æããããšãæå³ããŠããŸããã ããã¯ç°¡åãªæŠèŠã§ãããããŸããŸãªæ©èœãå®èšŒããããã«èšèšãããŠããããããããåå¥ã®èšäºã«å€ãããã®ã§ããã1ã€ã®èšäºã«ãå€ããŸããã
ããã«ããã®ãããªé·ãèšäºãæžããã®ã§ãå°æ¥ã®ããã«äºåãäœããããšæã£ãã®ã§ãå°æ¥ãç§ã¯åºæ¬çãªã¬ãã«ã®åãã«è¶³ããªããã®ã«æ°ãåãããªãã§ãããã ãã¡ãããããã«æ± ã«åãã-ããã¯å¹æçã§ãããã©ããªåºç€ãå·ã€ããŸããã
ãããŠã次åã¯ãéåžžã®ãããã§ãã·ã§ãã«ããã°ã©ãã³ã°ã¢ã¯ãã£ããã£ã«å¯ŸããFïŒã®é©åæ§ã«é¢ããåºæ¿çãªãããã¯ã®äŸã玹ä»ããŸãã
ç¹°ãè¿ãã«ãªããŸãããå®éã«ã¯å€§éã®ããã¹ãããããŸãã ãããŠãç§ã¯ããªãã«èŠåããªãã£ããšèšã£ãŠã¯ãããŸããã =ïŒ
FïŒæ©èœç
ãã¡ããããŸã第äžã«ãFïŒã¯é¢æ°åèšèªã§ããã€ãŸããFïŒã§æãå®å
šã«å®è£
ãããŠããã®ã¯ãé¢æ°åãã©ãã€ã ã®ãµããŒãã§ãã ãåãã®ããã«ããã®äžã®å€ãã®ããŒã¯ãŒããšãªãã©ã«ã¯OCamlããåçšãããŠããŸãããFïŒã®äž»ãªäœæè
ã§ããDon Symeããã€ãŠOCamlã«æãæºããŠããã®ã§ãé©ãããšã§ã¯ãããŸããã
FïŒã«ã€ããŠã®å€ãã®ç¥èã¯ãçŽç²ãªé¢æ°åããã°ã©ãã³ã°èšèªã§ãããèªè
ã¯ç§ã®éå»ã®æçš¿ããåŠãã ãããããŸããããèšèªã®å®å
šãªå°è±¡ãäœæããããã ãã«ããã¹ãŠãç°¡åã«ç¹°ãè¿ããŸãã
èå¥åãããŒã¯ãŒããæ©èœã
å¥åŠãªããšã«ãFïŒã䜿çšãããšãããã°ã©ãã¯èå¥åãå®çŸ©ããŠãåŸã§é¢æ°ã«ã¢ã¯ã»ã¹ã§ããŸãã ããã¯ã
letããŒã¯ãŒãã䜿çšããŠå®è¡
ããããã®åŸã«èå¥åã®ååããã©ã¡ãŒã¿ãŒã®ãªã¹ããçå·ïŒé¢æ°ãå®çŸ©ããåŒïŒãç¶ããŸãã ãã®ãããªãã®ïŒ
let k = 3.14
let square x = x**2.0
åœä»€åããã°ã©ãã³ã°ãšã¯ç°ãªããæåã®åŒã¯å€æ°ã§ã¯ãªãå®æ°ãå®çŸ©ããŸãããã®å€ã¯ããã°ã©ã ã®å®è¡äžã«å€æŽã§ããªãããã§ãã äžè¬çã«ãFïŒã¯é¢æ°ãšå€ãåºå¥ããŸãããé¢æ°ã¯ãã©ã¡ãŒã¿ãŒãšããŠãèªç±ã«æž¡ãããšãã§ããå€ã§ãã
ãã¹ãŠã®FïŒããŒã¯ãŒãã®ãªã¹ãã¯
ããã«ãããŸã ã åèãšããŠæäŸãããŠãã2çªç®ã®ãªã¹ãã®åèªã¯ãçŸæç¹ã§ã¯äœ¿çšãããŠããŸããããå°æ¥ã®ããã«äºçŽãããŠããŸãã ãããã¯äœ¿çšã§ããŸãããã³ã³ãã€ã©ã¯èŠåãåºããŸãã
FïŒã¯
ã«ãªãŒåãããé¢æ°ããµããŒãããŸããããã¹ãŠã®ãã©ã¡ãŒã¿ãŒãäžåºŠã«è»¢éã§ããããã§ã¯ãããŸããã
let add ab = a + b //'a -> 'a -> 'a
let addFour = add 4 //'a -> 'a
2çªç®ã®èå¥åã¯ã1ã€ã®ç©ºããã©ã¡ãŒã¿ãŒããæ¢ã«é¢æ°ãå®çŸ©ãããã1ã€ã¯4ãšããŠå®çŸ©ãããŠããŸããããã¯ãé¢æ°ãå€ã§ãããšããçè«ãããäžåºŠç€ºããŠããŸãã é¢æ°ã¯å€ã§ããããã©ã¡ãŒã¿ãŒã®å®å
šãªã»ãããåãåã£ãŠããªããããåã«å€ã§ãããå¥ã®é¢æ°ãè¿ãã ãã§ãã
ãã ãã.NETã®ãã¹ãŠã®é¢æ°ã«ã¯ã«ã¬ãŒæ©èœã®ããããã£ã¯ãããŸããããŸããFã§äœ¿çšããããã«ãã¿ãã«ïŒè€æ°ã®ç°çš®å€ã®ã»ããïŒã䜿çšãããŸãã ã¿ãã«ã¯ããèªäœã«å€ãã®ç°ãªããã©ã¡ãŒã¿ãŒãå«ãããšãã§ããŸãããFïŒã¯1ã€ã®ãã©ã¡ãŒã¿ãŒãšèŠãªãããçµæãšããŠå
šäœã«ã®ã¿é©çšãããŸãã ã¿ãã«ã¯ãã«ã³ãã§åºåãããæ¬åŒ§å
ã«èšè¿°ãããŸãã
let add (a,b) = a + b
let addFour = add 4
FïŒã«ãããšãäžé©åãªåã®ãã©ã¡ãŒã¿ãŒãã€ãŸã 'a *'ã®ä»£ããã«intã«é¢æ°ãé©çšããããšããŠããããããã®ãããªã³ãŒãã¯ã³ã³ãã€ã«ãããŸããã
ãã ããç¬èªã®é¢æ°ãç¹ã«ä»ã®ããã°ã©ãã䜿çšããé¢æ°ãéçºããå Žåã¯ã䜿çšããæè»æ§ãæããã«é«ããããå¯èœã§ããã°ã«ã¬ãŒã«ããå¿
èŠãããããšã«æ³šæããŠãã ããã
èªè
ã®çããã¯ãFïŒé¢æ°ã§ã¯æ»ãå€ãæ瀺çã«å®çŸ©ããå¿
èŠããªãããšã«ãã§ã«æ°ä»ããŠããŸãã ãã ããé¢æ°å
ã§äžéå€ãèšç®ããæ¹æ³ã¯æ確ã§ã¯ãããŸãããïŒ ããã§FïŒã¯ã¡ãœããã䜿çšããŸããããã®å€ãã¯ãã¹ããŒã¹ã®å©ããåããŠå¿ããããšãã§ãããšæããŸãã é¢æ°ã®å
éšèšç®ã¯éåžžã4ã€ã®ã¹ããŒã¹ã§åºåãããŠããŸãã
let midValue ab =
let dif = b - a
let mid = dif / 2
mid + a
ã¡ãªã¿ã«ãFïŒããã°ã©ã ãèŠã人ã®1人ãã³ãŒãå
ã«
#lightã³ãã³ããåžžã«ååšããããšã«é©ããå Žåããã®å¹æã®1ã€ã¯ãã¹ããŒã¹ãéèŠã«ãªãããšã§ãã ããã«ããã
in ã;;ã
begin ã
endãªã©ãOCamlã«ç±æ¥ããå€ãã®ããŒã¯ãŒããšæåã®äœ¿çšãåé¿ã
ããŸãã
åèå¥åã«ã¯ç¬èªã®é©çšåéããããå®çŸ©ã®å Žæããå§ãŸãïŒã€ãŸããå®çŸ©ã®å Žæãããã³ãŒãã§ããã䜿çšããããšã¯äžå¯èœã§ãïŒãå®çŸ©ãããã»ã¯ã·ã§ã³ã®çµããã§çµãããŸãã ããšãã°ãåã®äŸã®äžéèå¥ådifããã³midã¯ãmidValueé¢æ°ã®å€åŽã§ã¯æ©èœããŸããã
é¢æ°å
ã§å®çŸ©ãããèå¥åã¯ãå€éšã¬ãã«ã§å®çŸ©ãããèå¥åãšæ¯èŒããŠããã€ãã®ç¹ç°æ§ããããŸã-letãšããåèªã䜿çšããŠåå®çŸ©ã§ããŸãã ããã¯ãäžéå€ãä¿æããããã®ãã¹ãŠã®æ°ãããã»ãšãã©ã®å Žåã»ãšãã©æå³ã®ãªãååãçºæããããšãã§ããªãã®ã§äŸ¿å©ã§ãã ããšãã°ãåã®äŸã§ã¯ã次ã®ããã«æžãããšãã§ããŸãã
let midValue ab =
let k = b - a
let k = k / 2
k + a
ããã«ãããã¯å®å
šãªæå³ã§ã®ãªãŒããŒã©ã€ãã§ãããå€æ°ã®å€ã®å€æŽã§ã¯ãªããããèå¥åã®å€ã ãã§ãªãããã®åãéåžžã«é©åã«å€æŽã§ããŸãã
let changingType () =
let k = 1
let k = "string"
FïŒã§ã¯ããããããªã¹ãããã©ãŒã«ããªã©ã®ã·ãŒã±ã³ã¹ãåŠçãããããæ©èœã«ãããã»ãšãã©ã®å Žåããµã€ã¯ã«ãªãã§å®è¡ã§ããŸãããå¿
èŠãªå Žåã¯ãååž°ã䜿çšã§ããŸãã ç解ããããã®ã¯ããµã€ã¯ã«ãŸãã¯ååž°ã¯äžè¬ã«æªè§£æ±ºã®åé¡ã§ãããç§ã®æèŠã§ã¯ã©ã¡ããããªãå®è¡å¯èœã§ãã FïŒã®é¢æ°ããã®å®çŸ©å
ã§èªèº«ã«ã¢ã¯ã»ã¹ããããã«ã¯ã
letã®åŸã«
recããŒã¯ãŒããè¿œå ããå¿
èŠããããŸãã
FïŒã¯åŒ·ãåä»ããããèšèªã§ããã€ãŸããééã£ãåã®å€ãæã€é¢æ°ã䜿çšããããšã¯ã§ããŸããã é¢æ°ã¯ãä»ã®å€ãšåæ§ã«ãç¬èªã®ã¿ã€ããæã£ãŠããŸãã å€ãã®å ŽåãFïŒèªäœãé¢æ°ã®ã¿ã€ããæšæž¬ããããšãã°æ¬¡ã®ããã«ãããŸãã«ãªãå ŽåããããŸãã
let square x = x*x
å㯠'
a-> ' aã§ããããã§ã 'aã¯intãfloatãäžè¬çã«ã¯anyã§ã*æŒç®åããªãŒããŒããŒããããŸãã
å¿
èŠã«å¿ããŠãé¢æ°ãã©ã¡ãŒã¿ãŒã®åãèªåã§èšå®ã§ããŸãïŒããšãã°ãã¯ã©ã¹ã¡ãœããã䜿çšããå¿
èŠãããå ŽåïŒã
let parent (x:XmlNode) = x.ParentNode
ã©ã ããšæŒç®å
FïŒã¯å¿åé¢æ°ãŸãã¯ã©ã ãããµããŒãããŸãããããã¯ãé¢æ°ããã©ã¡ãŒã¿ãŒãšããŠå¥ã®é¢æ°ã«æž¡ããããšãã«ååãä»ããå¿
èŠããªãå Žåã«äœ¿çšãããŸãã 以äžã®ã©ã ãã®äŸïŒ
List.map (fun x -> x**2) [1..10]
ãã®é¢æ°ã¯ã1ãã10ãŸã§ã®ãã¹ãŠã®æ°å€ã®äºä¹ã§æ§æããããªã¹ããäœæããŸãã
ããã«ãFïŒã«ã¯ã
functionããŒã¯ãŒãã䜿çšããŠã©ã ããå®çŸ©ããå¥ã®æ¹æ³ããããŸãã ãã®æ¹æ³ã§å®çŸ©ãããã©ã ãã«ã¯ããã¿ãŒã³ãããã³ã°æäœãå«ãŸããŠããå ŽåããããŸãããå¿
èŠãªãã©ã¡ãŒã¿ãŒã¯1ã€ã ãã§ãã ãã ãããã®å Žåã§ããé¢æ°ã®ã«ã¬ãŒæ©èœãä¿åããããšã¯å¯èœã§ãã
function x -> function y -> x + y
FïŒã®ã©ã ãã¯
ã¯ããŒãžã£ããµããŒãããŸãããããã«ã€ããŠã¯ã¬ãã¥ãŒã®åŸåã§ããã«è©³ãã説æããŸãã
FïŒã§ã¯ãæŒç®åïŒåé
ããã³äºé
ïŒã¯ãé¢æ°ãåŒã³åºãããçŸçãªæ¹æ³ãšèŠãªãããšãã§ããŸãã CïŒãšåæ§ã«ãæŒç®åã¯ãªãŒããŒããŒãããããããããŸããŸãªåã§äœ¿çšã§ããŸãããCïŒãšã¯ç°ãªããæŒç®åãããŸããŸãªåã®ãªãã©ã³ãã«é©çšããããšã¯ã§ããŸãããã€ãŸããæ°åïŒããã³å®æ°ãæã€æŽæ°ïŒã®æååãè¿œå ããããšã¯ã§ããŸããããã£ã¹ããè¡ããŸãã
FïŒã䜿çšãããšãæŒç®åããªãŒããŒããŒãããããç¬èªã«å®çŸ©ãããã§ããŸãã
let (+) ab = a - b
printfn "%d" (1 + 1) // "0"
æŒç®åã«ã¯ã次ã®æåã®ä»»æã®ã·ãŒã±ã³ã¹ã䜿çšã§ããŸãïŒ$ïŒ
ïŒ* + _ã/ <=>ïŒ@ ^ |ãïŒ
let (+:*) ab = (a + b) * a * b
printfn "%d" (1 +:* 2) // "6"
ãªã¹ãã®åæå
å¥ã®åŒ·åãªFïŒãã¯ããã¯ã¯ããªã¹ããåæåããããšã§ããããã«ãããç¹å¥ãªæ§æã䜿çšããŠãããªãè€éãªãªã¹ããé
åãããã³ã·ãŒã±ã³ã¹ïŒIEnumerableã«çžåœïŒãçŽæ¥äœæã§ããŸãã ãªã¹ãã¯æ¬åŒ§[]ã§ãã·ãŒã±ã³ã¹ã¯{}ã§ãé
åã¯[| |]ã
æãç°¡åãªæ¹æ³ã¯ãã®ã£ããã決å®ããããšã§ããããã¯ãããšãã°æ¬¡ã®ããã«ïŒ..ïŒã䜿çšããŠèšå®ãããŸãã
let lst = [1 .. 10]
let seq = { 'a' .. 'z' }
ãŸãããã1ã€ïŒ..ïŒãè¿œå ããããšã§ãééã®éžæã¹ããããèšå®ã§ããŸãã
let lst = [1 .. 2 .. 10] // [1, 3, 5, 7, 9]
ããã«ããªã¹ããäœæãããšãã«ãã«ãŒãã䜿çšã§ããŸãïŒã«ãŒãã¯ãåäžãŸãã¯ä»»æã®çšåºŠã«ãã¹ãã§ããŸãïŒ
let lst = [for i in 1..10 -> i*i] // [1, 4, 9,..]
ãã ããããã ãã§ã¯ãããŸããã ãªã¹ããåæåãããšãã
yieldã¹ããŒãã¡ã³ã ïŒã·ãŒã±ã³ã¹ã«1ã€ã®èŠçŽ ãè¿œå ïŒãš
yieldïŒã䜿çšããŠãå
¥åããèŠçŽ ãæ瀺çã«æå®ã§ããŸã
ã ïŒå€ãã®èŠçŽ ãè¿œå ããŸãïŒããã³ãã¬ãŒããšã®è«çæ§é ãã«ãŒããæ¯èŒã䜿çšã§ããŸãã ããšãã°ãããã¯ããã®ãã©ã«ããŒãšãã®ãã¹ãŠã®ãµããã©ã«ããŒã«å«ãŸãããã¹ãŠã®ãã¡ã€ã«ã®ååã®ã·ãŒã±ã³ã¹ãäœæããæ¹æ³ã§ãã
let rec xamlFiles dir filter =
seq { yield! Directory.GetFiles(dir, filter)
for subdir in Directory.GetDirectories(dir) do yield! xamlFiles subdir filter}
ãã¿ãŒã³æ¯èŒ
ãã³ãã¬ãŒããšã®æ¯èŒã¯ãéåžžã®æ¡ä»¶ã¹ããŒãã¡ã³ããŸãã¯ã¹ã€ããã«å°ã䌌ãŠããŸãããããå€ãã®æ©èœããããŸãã äžè¬ã«ãæäœæ§æã¯æ¬¡ã®ããã«ãªããŸãã
match with
[|]1|2|..|10 -> 1
|11 when 1 -> 2
...
ãã³ãã¬ãŒããšã®æ¯èŒã¯äžããäžã«è¡ããããå¹
ã®çããã³ãã¬ãŒãã¯äžã«é
眮ããå¿
èŠãããããšãå¿ããªãã§ãã ããã æãäžè¬çãªãã³ãã¬ãŒãã¯æ¬¡ã®ããã«ãªããŸãïŒ_ïŒã¢ã³ããŒã¹ã³ã¢ïŒãããã¯ãèå¥åã®å€ã«é¢å¿ããªãããšãæå³ããŸãã ããã«ããã³ãã¬ãŒããšã®æ¯èŒãå®äºãïŒæªèª¿æ»ã®æ©èœã¯ãããŸããïŒããã¹ãŠã®èšç®ã§åãã¿ã€ãã®çµæãçæãããŸãã
ãã³ãã¬ãŒãã䜿çšããæãåçŽãªã¿ã€ãã®æäœã§ã¯ãèå¥åãšç¹å®ã®å€ïŒæ°å€ãæååïŒãæ¯èŒããŸããwhenããŒã¯ãŒãã䜿çšãããšããã³ãã¬ãŒãã«æ¡ä»¶ãè¿œå ããŠãèšç®ãå®è¡ã§ããŸãã
å¥ã®èå¥åãå€ã«çœ®ãæããããå Žåãæ€èšŒãããèå¥åã®å€ãããã«å²ãåœãŠãããŸãã
ãã³ãã¬ãŒããšæ¯èŒããããã«æãäžè¬çã«äœ¿çšããããªãã·ã§ã³ã¯ãã¿ãã«ãšãªã¹ãã®äžã«ãããŸãã xãïŒstring * intïŒåœ¢åŒã®ã¿ãã«ãšãããšãåæ§ã®ãã³ãã¬ãŒããäœæã§ããŸãã
match x with
| "" , _ -> ", !"
| _, i when i > 200 -> ", !"
| name, age -> sprintf " %s, %d" name age
| _ -> " "
ãã³ãã¬ãŒãã«èå¥åãããå Žåã察å¿ããå€ã«ãã£ãŠèå¥åãèªåçã«æ±ºå®ãããåŠçã§ååãã£ãŒã«ããšå¹Žéœ¢ãã£ãŒã«ããå¥ã
ã«äœ¿çšã§ããããšã«æ³šæããŠãã ããã
ãªã¹ãã¯ãŸã£ããåãæ¹æ³ã§åŠçãããŸãïŒããã¯å®éã«ã¯ãªã¹ãã§ã¯ãªãã以äžã§èª¬æããå·®å¥åãããçµåã§ãïŒã éåžžããªã¹ãïŒããªã¹ããïŒã®ãã³ãã¬ãŒãã¯ã空ã®å Žåã¯[]ãšããŠããŸãã¯ããã::ã¿ã€ãããããã®ããŒã«ã§ãããŒã«ãã¿ã€ã 'aãªã¹ãã®ããããã§ãããã ããä»ã®ãªãã·ã§ã³ãå¯èœã§ãã
match lst with
|[x;y;z] -> //lst , xy z.
|1::2::3::tail -> // lst [1,2,3] tail
ãã³ãã¬ãŒããšæ¯èŒãããšãã«èå¥åã«å€ãæž¡ãæ©èœã¯éåžžã«äŸ¿å©ãªã®ã§ãFïŒã§ã¯ã次ã®ããã«ãã³ãã¬ãŒãæ§æã䜿çšããã«ããã®ãããªå²ãåœãŠãçŽæ¥è¡ãããšãã§ããŸãã
let (name, age) = x
ãŸãã¯ããã§ãïŒ
let (name, _) = x
ã¿ãã«ã®æåã®èŠçŽ ã®ã¿ã«é¢å¿ãããå Žåã
æçš¿
FïŒã®ã¬ã³ãŒãã¯ã¿ãã«ã«äŒŒãŠããŸããããããã®åãã£ãŒã«ãã«ã¯ååããããŸãã ãšã³ããªã®å®çŸ©ã¯äžæ¬åŒ§ã§å²ãŸããã»ãã³ãã³ã§åºåãããŸãã
type org = { boss : string; tops :string list }
let Microsoft = { boss = "Bill Gates" ; tops = [ "Steve Balmer" , "Paul Allen" ]}
ã¬ã³ãŒããã£ãŒã«ãã«ã¯ãéåžžã©ããããã€ã³ããä»ããŠã¢ã¯ã»ã¹ããŸãã 以äžã«ç€ºãããã«ããšã³ããªã¯ã¯ã©ã¹ãæš¡å£ã§ããŸãã
ã¿ã°ä»ãããŒã«
FïŒã®ãã®ã¿ã€ãã䜿çšãããšãç°ãªãæ§é ãšæå³ãæã€ããŒã¿ãä¿åã§ããŸãã ããšãã°ãããã«ã¿ã€ãããããŸãïŒ
type Distance =
|Meter of float
|Feet of float
|Mile of float
let d1 = Meter 10
let d2 = Feet 65.5
3çš®é¡ã®ããŒã¿ã¯ãã¹ãŠåãã¿ã€ãïŒãªãã·ã§ã³ïŒã§ãããæããã«æå³ãç°ãªããŸãã ã©ãã«ä»ãçµåã¯ãåžžã«ãã³ãã¬ãŒããšã®æ¯èŒãéããŠåŠçãããŸãã
match x with
|Meter x -> x
|Feet x -> x*3.28
|Mile x -> x*0.00062
ãã§ã«è¿°ã¹ãããã«ããªã¹ããªã©ã®äžè¬çãªããŒã¿åã¯ãããŒã¯ã¢ãããããçµåã§ãã ãã®éå
¬åŒã®å®çŸ©ã¯æ¬¡ã®ããã«ãªããŸãã
type a' list =
|[]
|:: of a' * List
ãšããã§ãäžèšã®äŸãããããããã«ãFïŒã®ããŒã¯ã¢ãããããã»ããã¯ãäžè¬çãªæ¹æ³ã§ãã©ã¡ãŒã¿ãŒåã§ããŸãã
FïŒåœä»€ç
ãŠãããã¿ã€ã
ãŠãããã¿ã€ãã¯ãCïŒã®voidã¿ã€ãã«é¢é£ããŠããŸãã é¢æ°ãåŒæ°ãåããªãå Žåããã®å
¥åã¿ã€ãã¯ãŠãããã§ã;å€ãè¿ããªãå Žåããã®åºåã¿ã€ãã¯ãŠãããã§ãã é¢æ°åããã°ã©ãã³ã°ã§ã¯ãå€ãåãå
¥ããªãããŸãã¯å€ãè¿ããªãé¢æ°ã¯å€ãè¡šããŸããããåœä»€åãã©ãã€ã ã§ã¯ãå¯äœçšïŒããšãã°ãå
¥åºåïŒã®ããã«å€ãæã¡ãŸããã¿ã€ãunitã®å¯äžã®å€ã®åœ¢åŒã¯ïŒïŒã§ãã ãã®ãããªé¢æ°ã¯äœãåãå
¥ãããäœãè¡ããŸããïŒãŠããã->ãŠãããïŒã
let doNothingWithNothing () = ()
ååã®åŸã®æ¬åŒ§ã¯ããããå€ã§ã¯ãªãã空ã®å
¥åãæã€é¢æ°ã§ããããšãæå³ããŸãã åè¿°ããããã«ãé¢æ°ã¯å€ã§ãããé¢æ°å€ãšéé¢æ°å€ã«ã¯å€§ããªéãããããŸãã2çªç®ã¯1åã ãèšç®ããã1çªç®ã¯åŒã³åºãããšã«èšç®ãããŸãã
å€ãè¿ãé¢æ°ã¯ãã¹ãŠãignoreé¢æ°ã䜿çšããŠãŠãããã¿ã€ããè¿ãé¢æ°ã«å€æã§ããŸãã ãããé©çšããŠããã®é¢æ°ã§ã¯æ»ãå€ã§ã¯ãªãå¯äœçšã«ã®ã¿é¢å¿ãããããšãã³ã³ãã€ã©ã«äŒããŸãã
å¯å€ããŒã¯ãŒã
ç¥ã£ãŠããããã«ãäžè¬çãªå ŽåãFïŒã®èå¥åã¯äœããã®å€ã§å®çŸ©ã§ããŸããããã®å€ã¯å€æŽã§ããŸããã ãã ããå€ãè¯ãåœä»€åå€æ°ã¯äŸç¶ãšããŠæçšã§ãããããFïŒã¯å€æ°ãäœæããã³äœ¿çšããããã®ã¡ã«ããºã ãæäŸããŸãã ãã®ããã«ã¯ãå€æ°åã®åã«
mutableããŒã¯ãŒããèšè¿°ããå¿
èŠããããå€ã¯
<-æŒç®åã䜿çšããŠå€æŽã§ããŸãã
let mutable i = 0
i <- i + 1
ãã ãããã®ãããªå€æ°ã®äœ¿çšã¯å¶éãããŠããŸããããšãã°ãã©ã ãã§ã®ã¯ããŒãžã£ãšåæ§ã«ãå
éšé¢æ°ã§ã¯äœ¿çšã§ããŸããã ãã®ã³ãŒãã¯ãšã©ãŒãã¹ããŒããŸãïŒ
let mainFunc () =
let mutable i = 0
let subFunc () =
i <- 1
ã¿ã€ãref
FïŒã§ã¯ã
refåã䜿çšããŠå€æ°ãå®çŸ©ããå¥ã®æ¹æ³ããããŸãã ãããè¡ãã«ã¯ãèå¥åã®å€ãè¡šãèšç®ã®åã«ããŒã¯ãŒã
refã眮ããŸãã
å¥ã®å€ãå€æ°ã«å²ãåœãŠãã«ã¯ããã¹ã¿ã«ãžãã¯ãªæŒç®åïŒ=ãçã
ããã»ã©äœ¿çšããŸããå€æ°ã®å€ãžã®ã¢ã¯ã»ã¹ã¯è¿œå ããããšã§è¡ãããŸãïŒ å€æ°åã®åã
let i = ref 0
i := !i + 1
ããããããã®è¡šèšã¯åã®è¡šèšã»ã©ãããã§ã¯ãããŸãããæå笊ã䜿çšããŠå€ãååŸããã ãã®äŸ¡å€ããããŸãïŒFã®åŠå®ã«ã¯ïŒããŒã¯ãŒãã¯ãããŸããïŒ
ãã ãã
mutableãšã¯ç°ãªãã
refåã«ã¯ã¹ã³ãŒãã«é¢ããå¶éããªãããããã¹ããããé¢æ°ãšã¯ããŒãžã£ãŒã®äž¡æ¹ã§äœ¿çšã§ããŸãã ãã®ãããªã³ãŒãã¯åäœããŸãïŒ
let i = ref 2
let lst = [1..10]
List.map (fun x -> x * !i) lst
é
å
FïŒã«ã¯ãå¯å€åã®é
åããããŸãã ãªã¹ãå
ã®å€ãšã¯ç°ãªããé
åå
ã®å€ã¯åå²ãåœãŠã§ããŸãã é
åã¯ãã®ãããªè§æ¬åŒ§[| |]ããã®äžã®èŠçŽ ã¯ã»ãã³ãã³ã§ãªã¹ããããŸãã é
åã®èŠçŽ ãžã®ã¢ã¯ã»ã¹ã¯[Ind]ãä»ããŠè¡ããã代å
¥ã¯mutableã®æäœã«æ
£ããŠãã
<-æŒç®åã«ãã£ãŠå®è¡ãããŸãã é
åãåŠçããããã®ãã¹ãŠã®é¢æ°ïŒãªã¹ããåŠçããããã®ã¡ãœããã«ã»ãŒé¡äŒŒïŒã¯ãArrayã¯ã©ã¹ã«ãããŸãã
let arr = [|1; 2; 3|]
arr.[0] <- 10 // [|10,2,3|]
é
åã¯ã..ãyieldãªã©ã䜿çšããŠããªã¹ããšãŸã£ããåãæ¹æ³ã§åæåã§ããŸãã
let squares = [| for x in 1..9 -> x,x*x |] // [| (1,1);(2,4);...;(9,81) |]
ãŸããFïŒã䜿çšãããšããã¹ããããïŒé·ãã®ç°ãªããµãé
åã䜿çšïŒãšãã¢ããªã·ãã¯ãã®äž¡æ¹ã®å€æ¬¡å
é
åãäœæã§ããŸãã
å¶åŸ¡ããžãã¯
FïŒã§ã¯ãéåžžã®åœä»€åå¶åŸ¡ããžãã¯-æ¡ä»¶æŒç®å
if ...
then ...
else ãããã³
forã«ãŒããš
whileã«ãŒã
ã䜿çšã§ããŸãã
ifã¹ããŒãã¡ã³ãã¯é¢æ°ãšèŠãªãããšãã§ããããšã«æ³šæããŠãã ãããã€ãŸããã©ã®ãããªæ¡ä»¶äžã§ãåãåã®å€ãè¿ãå¿
èŠããããŸãã ããã¯ã
elseã®äœ¿çš
ãå¿
é ã§ããããšã瀺åããŠã
ãŸã ã å®éãäŸå€ã1ã€ãããŸããèšç®ãè¡ãããæ¡ä»¶ãæºãããããšãã«ãã¿ã€ãåäœãè¿ãããŸãã
if System.DateTime.Now.DayOfWeek = System.DayOfWeek.Sunday then
printfn " !"
printfn " !"
ã·ããã¯ãã«ãŒãã«å±ããé¢æ°ãšããã§ãªãé¢æ°ãå€å¥ããããã«ã䜿çšãããŸãã ããšãã°ãäžã®äŸã§ã¯ãææ¥ã«é¢ä¿ãªã2çªç®ã®æã衚瀺ãããŸãã
FïŒã®
forã«ãŒãã¯ãŠãããåã§ãããããã«ãŒãæ¬äœã§ã®èšç®ã«ãããã®ã¿ã€ããçæãããŸããããã§ãªãå Žåãã³ã³ãã€ã©ãŒã¯èŠåãåºããŸãã
let arr = [|1..10|]
for i = 0 to Array.length arr - 1 do
printfn arr.[i]
éæ¹åã«é²ã¿ããå Žåã¯ãå€ãè¯ãæ代ã®ããã«
toã downtoã«çœ®ãæããŸãã
ãŸããããªãã¿ã®foreachã«äŒŒã
forã«ãŒãã®å¥ã®åœ¢åŒã䜿çšããããšãã§ããŸãã
for item in arr
print_any item
whileã«ãŒããéåžžã«äžè¬çã§åœä»€åããã°ã©ããŒã«éŠŽæã¿ãããããã®æ¬äœã¯ããŒã¯ãŒã
doãš
doneã®éã«ãã
ãŸããã2çªç®ã®
ã«ãŒãã¯ãªãã·ã§ã³ã§ã·ããã·ã¹ãã ã䜿çšããŠçç¥ã§ããŸãã
.NETã©ã€ãã©ãªããéçã¡ãœãããšãªããžã§ã¯ããåŒã³åºã
FïŒã§.NETããŒã«ãããå
šäœã䜿çšã§ããŸãããFïŒã§èšè¿°ãããŠããªããã¹ãŠã®ã¡ãœããã«ã¯ã«ã¬ãŒæ©èœã®ããããã£ããªããããå
¥åèŠçŽ ã®ã»ããã«å¯Ÿå¿ããã¿ãã«ã®åœ¢åŒã§åŒæ°ãæå®ããå¿
èŠããããŸãã ãã®å Žåãã³ãŒã«ã¬ã³ãŒãã¯syncharpãš1ã€ã®iotaãåºå¥ããŸããã
#light
open System.IO
if File.Exists( "file.txt" ) then
printf " !"
ãã ãã.NETã¡ãœããã«æ¬åœã«å¥œå¥å¿ãæããããå Žåã¯ã次ã®ããã«ã€ã³ããŒãããå¿
èŠããããŸãã
let exists file = File.Exists(file)
ãªããžã§ã¯ãã®äœ¿çšãåæ§ã«ç°¡åã§ãã
æ°ããããŒã¯ãŒãïŒã ããèããã§ããããïŒïŒã䜿çšããŠäœæãããã³ã³ã¹ãã©ã¯ã¿ãŒãã©ã¡ãŒã¿ãŒã®é©åãªã¿ãã«ã䜿çšããŸãã
letã䜿çšããŠãªããžã§ã¯ãã«èå¥åãå²ãåœãŠãããšãã§ããŸãã ã¡ãœããåŒã³åºãã¯éçã«äŒŒãŠããããã£ãŒã«ãã¯
<-ã䜿çšããŠå€æŽãããŸãã
#light
let file = new FileInfo( "file.txt" )
if not file.Exists then
using (file.CreateText()) (fun stream ->
stream.WriteLine( "Hello world" ))
file.Attributes <- FileAttributes.ReadOnly
FïŒã§ã¯ããªããžã§ã¯ããäœæãããšãã«ã次ã®ããã«ãã£ãŒã«ããããã«åæåã§ããŸãã
let file = new FileInfo( "file.txt" , Attributes = FileAttributes.ReadOnly)
Fã§ã€ãã³ãã䜿çšããïŒ
FïŒã®åã€ãã³ãã«ã¯ãã€ãã³ãã«ãã³ãã©ãŒé¢æ°ãè¿œå ããAddã¡ãœããããããŸãã ãã³ãã©ãŒé¢æ°ã¯ãã¿ã€ã 'a-
> unitã§ãªããã°ãªããŸããã ã¿ã€ããŒã€ãã³ãã«ãµã€ã³ã¢ããããæ¹æ³ã¯æ¬¡ã®ãšããã§ãã
#light
open System.Timers
let timer = new Timer(Interval=1000, Enabled=true)
timer.Elapsed.Add(fun _ -> printfn "Timer tick!" )
ã€ãã³ãããã®ãµãã¹ã¯ã©ã€ã解é€ã¯ãRemoveã¡ãœããã䜿çšããŠè¡ãããŸãã
æŒç®å|>
転éæŒç®å|>ã¯æ¬¡ã®ããã«å®çŸ©ãããŸãã
let (|>) fg = gf
æåã®åŒæ°ããã©ã¡ãŒã¿ãŒãšããŠ2çªç®ã®åŒæ°ã«æž¡ããŸãã ãã¡ããã2çªç®ã®åŒæ°ã¯ãå¯äžã®ãã©ã¡ãŒã¿ãŒãšããŠfåã®å€ãåãé¢æ°ã§ãªããã°ãªããŸããã ã¡ãªã¿ã«ã転éæŒç®åã䜿çšããå¯èœæ§ãããããããªã¹ãã®äžã«ãããã¹ãŠã®é¢æ°ïŒiterãmapãfoldïŒã¯ãªã¹ãèªäœãæåŸã«ååŸããŸãã 次ã«ãgãšããŠãå£æ±ºå®é¢æ°ã䜿çšã§ããŸãã
[1..10] |> List.iter (fun i -> print_int i)
ããšãã°ãiteré¢æ°ã®åœ¢åŒã¯ïŒ 'a list-
> unitïŒ
-> ' a list-
> unitã§ããæåã®ãã©ã¡ãŒã¿ãŒãã©ã ãã§èšå®ãããšã 'a list-
> unitåã®é¢æ°ãååŸãããŸããããã¯åŒæ°ãšããŠå®çŸ©ããããªã¹ãã®ã¿ãåŒæ°ãšããŠåãåããŸãã
ããã°ã©ã ã¯ãå€ãã®å Žåã転éæŒç®åã®é·ããã§ãŒã³ã䜿çšããŸããåæŒç®åã¯ãåã®æŒç®åã§ååŸããå€ãã€ãŸãäžçš®ã®ãã€ãã©ã€ã³ãåŠçããŸãã
FïŒãªããžã§ã¯ãæå
ä»æ¥ã®ããã°ã©ãã³ã°ã®äž»åã§ããã®ã¯ãªããžã§ã¯ãæåã®ãã©ãã€ã ã§ãããšäž»åŒµãã人ã¯ã»ãšãã©ããªããšæããŸãããã¡ãããFïŒã¯ããã«å«ãŸããæŠå¿µãç¡èŠã§ããŸããã§ããã 圌ãç§ãã¡ã«æäŸããŠããããã®ãèŠãŠã¿ãŸãããã
ã¿ã€ãã³ã°ã
FïŒã§ã¯ãéçãªå€ã®åãæ瀺çã«å€æŽããããšãã§ããŸãã FïŒã¯2ã€ã®ç°ãªãæŒç®åã䜿çšããŠäžäžã«ãã£ã¹ãããŸãã èµ·åãã€ãŸããéçåã«ãã®ç¥å
ã®ããããã®åã®å€ãå²ãåœãŠãããšã¯ãæŒç®åïŒ>ã«ãã£ãŠå®è¡ãããŸãã äžã®äŸã®strObjå€ã¯ããªããžã§ã¯ãåã«ãªããŸãã
let strObj = ( "-, -" :> obj)
代å
¥ãã€ãŸããåå«ã®ããããã®åã«ããå€ã®åã®æå®ã¯ãoperatorïŒïŒ>ã«ãã£ãŠå®è¡ãããŸãã
å€ã®ã¿ã€ãïŒã¢ããã°ã¯CïŒã®ãã®ïŒã確èªããã«ã¯ãæŒç®åïŒïŒã䜿çšããŸããããã¯ãè«çæ§é ã ãã§ãªãããã³ãã¬ãŒããšæ¯èŒãããšãã«ã䜿çšã§ããŸãã
match x with
|:? string -> printf " !"
|:? int -> printf " !"
|:? obj -> printf " !"
éåžžãFïŒã¯ãé¢æ°ãèšç®ãããšãã«åç¶æ¿ã®éå±€ãèæ
®ããŸãããã€ãŸããåç¶æ¿ãåŒæ°ãšããŠäœ¿çšããããšã¯ã§ããŸããã ããšãã°ããã®ãããªããã°ã©ã ã¯ã³ã³ãã€ã«ãããŸããã
let showForm (form:Form) =
form.Show()
let ofd = new OpenFileDialog();
showForm ofd
ååãšããŠãæ瀺çã«åããã£ã¹ãã§ããŸãïŒshowFormïŒofdïŒ> FormïŒããã ããFïŒã¯å¥ã®æ¹æ³ãæäŸããŸã-é¢æ°ã·ã°ããã£ã®åã®åã«ãã³ãèšå·ïŒãè¿œå ããã
let showForm (form: #Form) =
form.Show()
ãããã£ãŠãç¹å®ã®é¢æ°ã¯ãFormããç¶æ¿ããã¯ã©ã¹ã®ãªããžã§ã¯ããåŒæ°ãšããŠåãåããŸãã
ãªããžã§ã¯ããšããŠèšé²ããã³çµå
ã¬ã³ãŒããšçµåã«ã¡ãœãããè¿œå ã§ããŸãã ãããè¡ãã«ã¯ãã¬ã³ãŒããå®çŸ©ããåŸãwithããŒã¯ãŒããè¿œå ãããã¹ãŠã®ã¡ãœãããå®çŸ©ããåŸã«
endãèšè¿°ããåã¡ãœããã®èå¥åã®åã«
memberããŒã¯ãŒãã䜿çšããå¿
èŠããããŸãã
type Point ={
mutable x: int;
mutable y: int; }
with
member p.Swap() =
let temp = px
px <- py
py <- temp
end
ã¡ãœããåã®åã«æå®ããããã©ã¡ãŒã¿ãŒpã¯ããã£ãŒã«ãã«ã¢ã¯ã»ã¹ããããã«ãã®äžã§äœ¿çšãããããšã«æ³šæããŠãã ããã
ã¯ã©ã¹ãšã€ã³ã¿ãŒãã§ãŒã¹
FïŒã®ã¯ã©ã¹ã¯ãããŒã¯ãŒãçšããŠæ±ºå®ãããŠããã¿ã€ãã®ã¯ã©ã¹åãçå·ããŒã¯ãŒãã«ç¶ããŠãã¯ã©ã¹ãã¯ã©ã¹å®çŸ©ã¯endããŒã¯ãŒãã§çµäºããŸããã³ã³ã¹ãã©ã¯ã¿ãŒãæå®ããã«ã¯ãã¯ã©ã¹å®çŸ©ã«newãšããååã®ã¡ã³ããŒãå«ããå¿
èŠããããŸããtype construct = class
new () = {}
end
let inst = new construct()
ã¯ã©ã¹å®çŸ©ã«ã¯å°ãªããšã1ã€ã®ã³ã³ã¹ãã©ã¯ã¿ãŒãå«ãŸããŠããå¿
èŠããããŸããããã§ãªãå Žåãã³ãŒãã¯ã³ã³ãã€ã«ãããŸãããFïŒã¯ãCïŒã®ãããªããã©ã«ãã®ã³ã³ã¹ãã©ã¯ã¿ãŒãæäŸããŸããããã£ãŒã«ããå®çŸ©ããã«ã¯ããã®ååã®åã«ããŒã¯ãŒãvalãè¿œå ããå¿
èŠããããŸããtype File = class
val path: string
val info : FileInfo
new () = new File( "default.txt" )
new (path) =
{ path = path;
info = new FileInfo(path) }
end
let file1 = new File( "sample.txt" )
ã芧ã®ãšãããã³ã³ã¹ãã©ã¯ã¿ã¯éåžžã®æ¹æ³ã§ãªãŒããŒããŒãã§ããŸããã³ã³ã¹ãã©ã¯ã¿ãŒã¯äžéšã®ãã£ãŒã«ããåæåããã«æ®ãããšã¯ã§ããŸãããããããªããšãã³ãŒãã¯ã³ã³ãã€ã«ãããŸãããã³ã³ã¹ãã©ã¯ã¿ãŒã§ã¯ããã£ãŒã«ãã®åæåãŸãã¯ä»ã®ã³ã³ã¹ãã©ã¯ã¿ãŒã®åŒã³åºãããã§ããªãããšã«æ³šæããŠãã ãããã³ã³ã¹ãã©ã¯ã¿ã§è¿œå ã®æäœãæå®ããã«ã¯ããã®åŸã«ãè¿œå ãããã®åŸã«ãã¹ãŠã®è¿œå ã®èšç®ãèšè¿°ããå¿
èŠããããŸããnew (path) as x =
{ path = path;
info = new FileInfo(path) }
then
if not x.info.Exists then printf " !"
ããã©ã«ãã§ã¯ãã¯ã©ã¹ã®ãã£ãŒã«ãã¯äžå€ã§ã;ãã£ãŒã«ããå¯å€ã«ããã«ã¯ããã®ååã®åã«å¯å€ãè¿œå ããå¿
èŠããããŸããFïŒã®ã€ã³ã¿ãŒãã§ã€ã¹ã¯ã次ã®ããã«å®çŸ©ããã³å®è£
ãããŸããlet ISampleInterface = interface
abstract Change : newVal : int -> unit
end
type SampleClass = class
val mutable i : int
new () = { i = 0}
interface ISampleInterface with
member x.Change y = xi <- y
end
end
FïŒã¯ãã¯ã©ã¹ãå®çŸ©ããå¥ã®ãšã¬ã¬ã³ããªæ¹æ³-æé»çãªå²ãåœãŠãæäŸããŸããã¯ã©ã¹åã®çŽåŸã«å
¥åãã©ã¡ãŒã¿ãŒããªã¹ããããŸããããã§ãªããã°ãã³ã³ã¹ãã©ã¯ã¿ãŒåŒæ°ã«å«ãŸããŸããã¯ã©ã¹ã®æ§ç¯ã¯ãã¡ãœããã®å®çŸ©ã«å
è¡ããletã·ãŒã±ã³ã¹ã®å©ããåããŠããã®æ¬äœã§çŽæ¥è¡ãããŸãããã®æ¹æ³ã§å®çŸ©ããããã¹ãŠã®èå¥åã¯ãã¯ã©ã¹ã«å¯ŸããŠãã©ã€ããŒãã«ãªããŸããã¯ã©ã¹ã®ãã£ãŒã«ããšã¡ãœããã¯ãmemberããŒã¯ãŒãã䜿çšããŠèšå®ãããŸããããã«äŸãåç
§ããããšããå§ãããŸããtype Counter (start, inc, length) = class
let finish = start + length
let mutable current = start
member c.Current = current
member c.Inc () =
if current > finish then failwith "-!"
current <- current + inc
end
let count = new Counter(0, 5, 100)
count.Inc()
FïŒã¯CïŒã®ããã«ãåäžã®ç¶æ¿ã®ã¿ããµããŒãããè€æ°ã®ã€ã³ã¿ãŒãã§ãŒã¹ãå®è£
ããŸããç¶æ¿ã¯ãããŒã¯ãŒã䜿çšããŠæå®ãããç¶æ¿çŽåŸã®ã¯ã©ã¹ãïŒ
type Base = class
val state : int
new () = {state = 0}
end
type Sub = class
inherit Base
val otherState : int
new () = {otherState = 0}
end
æ瀺çã«åºæ¬çãªç©ºã®ã³ã³ã¹ãã©ã¯ã¿ãŒãåŒã³åºãå¿
èŠã¯ãããŸãããåå«ã³ã³ã¹ãã©ã¯ã¿ãŒãåŒã³åºããããšã空ã®ç¥å
ã³ã³ã¹ãã©ã¯ã¿ãŒãèªåçã«åŒã³åºãããŸããç¥å
ã«ãã®ãããªã³ã³ã¹ãã©ã¯ã¿ãŒããªãå Žåã¯ãinheritããŒã¯ãŒãã䜿çšããŠãåå«ã³ã³ã¹ãã©ã¯ã¿ãŒã§åºæ¬ã³ã³ã¹ãã©ã¯ã¿ãŒãæ瀺çã«åŒã³åºãå¿
èŠããããŸããFïŒã®ããããã£ã¯æ¬¡ã®ããã«å®çŸ©ãããŸããtype PropertySample = class
let mutable field = 0
member x.Property
with get () = field
and set v = field <- rand
end
éçãã£ãŒã«ãã決å®ããããã®éšæããŒã¯ãŒãè¿œå éçã¯ã©ã¹ã®é€å»ãã©ã¡ãŒã¿obznachayuschyã€ã³ã¹ã¿ã³ã¹ãtype StaticSample = class
static member TrimString (st:string) = st.Trim()
end
ãããã«
ä»æ¥ãç§ãã¡ã¯èšèªã®åºæ¬çãªæ©èœã®ã»ãšãã©ãç°¡åã«èª¿ã¹ãèŠããã®ã«åºã¥ããŠããã€ãã®çµè«ãå°ãåºãããšãã§ããŸããå®éãCïŒã§äœ¿çšå¯èœãªã¯ã©ã¹ãšå€æ°ã䜿çšããæäœã¯FïŒã§ãå®è¡ã§ããããããã®èšèªã¯ããå
ããã»ã©ãªããžã§ã¯ãæåã§ã¯ãããŸããããã®äžã®æ§æã¯ãããããã£ãšè€éã§ãããéèŠã§ã¯ãªããããã¯ç¿æ
£ã®åé¡ã®ããã§ããäžæ¹ãæ©èœé¢ã§ã¯ãFïŒãäºæ³éãïŒå®éãããã§èª¬æãããŠããã®ã§ãããã«äºæ³ãããã¯ãã§ãïŒ