2010/07 33

F# 윈도우 애플리케이션에서 마우스 이벤트 감지하기

F# 프로그램으로 마우스 이벤트를 감지하는 간단한 윈도우폼 애플리케이션을 작성해 보았다. 실행시키고 라벨이 았는 곳을 클릭하면 클릭 횟수가 하나씩 증가한다. (* * Filename: TestMouseDownEvent.fs * * Compile: fsc TestMouseDownEvent.fs * Execute: TestMouseDownEvent * * Date: 2010/07/23 * Author: phkim pkim __AT__ scripts.pe.kr *) open System open System.Drawing open System.Windows open System.Windows.Forms // Creates the user interface let frm, lbl = new Form (), ne..

프로그래밍/F# 2010.07.23

30000! 빨리 계산하기 with F#

* 꼬리 재귀호출과 패턴 매칭을 이용하여 구현한 팩토리얼과 피보나치 수열 계산 (* Filename: fact.fs Rapid factorial and fibonacci seq implementations by pattern matching and tail recursive call Compile: fsc fact.fs Execute: fact Date: 2010/07/20 Author: phkim pkim __AT__ scripts.pe.kr *) #light let rec fact (n:int) : bigint = match n with | 0 | 1 -> 1I | k -> (bigint k) * (fact (k-1)) let factorial n = let rec loop acc k = match ..

프로그래밍/F# 2010.07.20

30000! 빨리 계산하기 with Haskell

* 꼬리 재귀호출과 패턴 매칭을 이용하여 구현한 팩토리얼과 피보나치 수열 계산 {- Filename: fact.hs Rapid factorial and fibonacci seq implementations by pattern matching and tail recursive call Compile: ghc fact.hs Execute: main Date: 2010/07/20 Author: phkim pkim (AT) scripts.pe.kr -} module Main where factorial :: Integer -> Integer factorial n = recfact 1 n recfact :: Integer -> Integer -> Integer recfact acc k = case k of 0 -..

int 타입과 bigint 타입 간 상호 변환 with F#

F# 언어에서 int 타입과 bigint 타입을 상호변환 하는 것은 의외로 쉽다. 명령 프롬프트> fsi Microsoft (R) F# 2.0 Interactive build 2.0.0.0 Copyright (c) Microsoft Corporation. All Rights Reserved. For help type #help;; > let a = 100I;; val a : System.Numerics.BigInteger = 100I > let b = int a;; val b : int = 100 > let c = bigint b;; val c : System.Numerics.BigInteger = 100I > b**5;; b**5;; ^ stdin(13,1): error FS0001: The type ..

프로그래밍/F# 2010.07.18

Pollard's rho method 소개: 정수의 인수분해(factorizing integers) with F#

정의 (소수와 합성수) 1보다 큰 양의 정수 n에 대하여 (i) n = a * b 를 만족하고 1보다 큰 두 양의 정수 a와 b가 존재하면, n을 합성수(合成數, composite number)라고 한다. 참고로 이 경우, a, b 중에 적어도 하나는 sqrt(n) 보다 작거나 같다. 합성수의 예로는 4, 6, 9, 24, 143 등이 있다. (ii) n = a * b 를 만족하고 1보다 큰 두 양의 정수 a와 b가 존재하지 않으면, 즉 n을 두 양의 정수의 곱으로 표현하는 방법이 1*n과 n*1 두 가지 뿐이면, n을 소수(素數, prime number)라고 한다. 소수의 예로는 2, 3, 5, 7, 11 등이 있다. n이 소수인지 아닌지 확인하려면, n을 2 보다 크거나 같고 sqrt(n) 보다 작거..

프로그래밍/F# 2010.07.17

스트링 리스트에서 스트링 찾기(find) with F#

[파일명: TestStringFindInList.fs]------------------------------------------------ #light let find arr s = List.findIndex (fun x -> x = s) arr let printList data = let rec loop l = match l with | x::[] -> printf "%O" x | x::xs -> printf "%O, " x; loop xs | [] -> printf "" printf "[" loop data printf "]" // Begin here let cmdArgs = System.Environment.GetCommandLineArgs() let words = ["하나"; "둘"; "셋"; ..

프로그래밍/F# 2010.07.16

스트링 배열 정렬(sorting)하기 with F#

printList 함수를 F#의 함수형 언어의 특징을 살려 (꼬리 재귀호출과 match 표현을 이용하여) 구현하였다. [파일명: TestSort.fs]------------------------------------------------ #light let printList data = let rec loop l = match l with | x::[] -> printf "%O" x | x::xs -> printf "%O, " x; loop xs | [] -> printf "" printf "[" loop data printf "]" // Begin here let cmdArgs = System.Environment.GetCommandLineArgs() let b = cmdArgs.[1..] |> Arr..

프로그래밍/F# 2010.07.16

손으로 계산하는 긴자리 곱셈표 만들기 with F#

초등학교 때 배우던 두 정수의 곱셈표를 만들어 주는 F# 소스이다. 이 소스는 Python 소스를 F# 소스로 변환한 것이라 F#의 명령형 언어 특징을 위주로 짜여져 있다. (* * Filename: MakeMultTable.fs * * Print a multiplication table. * * Compile: fsc MakeMultTable.fs * Execute: MakeMultTable 230 5100 * * Date: 2010/07/15 * Author: PH Kim [ pkim ((AT)) scripts.pe.kr ] *) #light exception RuntimeError of string exception ValueError of string let println s = printfn "..

프로그래밍/F# 2010.07.15

손으로 만드는 나눗셈 계산표 with F#

다음은 초등학교에서 배우는 나눗셈 계산표를 만들어주는 F# 소스 코드이다. 나눗셈 계산표를 완성하고 나서 약수, 배수 관계를 알려준다. 아래의 소스는 Python 용 소스를 F# 용 소스로 거의 일대일 변환한 것이라서, F# 언어의 명령형 언어의 특징을 위주로 짜여져 있다. (* * Filename: MakeDivisionTable.fs * * Purpose: Make a division table in a handy written form. * * Compile: fsc --codepage:949 MakeDivisionTable.fs * * Execute: MakeDivisionTable 12345 32 * MakeDivisionTable 500210 61 * MakeDivisionTable 234 5..

프로그래밍/F# 2010.07.15