전체 글 725

대화형 모드의 진법(radix) 변환 예제 with F#

다음은 대화형 모드(interactive mode)에서 진법 변환(radix conversion)하는 F# 소스 코드이다. 주: 이 소스는 Python 용으로 만들어 둔 소스를 F# 언어로 바꾼 것이라서 F# 언어의 명령형 언어 특징을 위주로 짜여져 있다. 메뉴는 주메뉴 Command: (S)et radix, (A)bout, (Q)uit or E(x)it 와 부메뉴 SubCommand: 'main()' to goto Main menu, 'exit()' or 'quit()' to exit 로 구성되어 있으며, 진법 변환의 핵심은 소스 코드에 자체 작성된 함수 atoi(string, radix)와 itoa(number, radix)의 사용이다. let value = atoi(str, srcRdx) let s..

프로그래밍/F# 2010.07.14

황금비율(golden ratio) 구하기 with F#

다음은 이차방정식 x^2 - x - 1 = 0 의 양의 근 즉 황금비율(golden ratio)을 구하는 F# 소스이다. 황금비율을 구하는 비례방정식은 1 : x = x : (x+1) 이며, 이를 이차방정식으로 표현한 것이 x^2 - x - 1 = 0 이다. See: http://en.wikipedia.org/wiki/Golden_ratio (* * Filename: TestGoldenRatio.fs * 황금률(즉, 이차방정식 x^2 - x - 1 = 0 의 양의 근)을 계산한다. * * Compile: fsc testGoldenRatio.fs * Execute: testGoldenRatio * * Date: 2010/07/13 * Author: PH Kim [ pkim (AT) scripts.pe.kr..

프로그래밍/F# 2010.07.13

현재 시각 알아내기 for F#

현재 시각을 컨솔에 보여주는 간단한 F# 언어 소스 코드이다. UTC란 1970년 1월 1일 0시 0분 0초를 기준으로 하여 경과된 초 단위의 총 시간을 의미한다. * UTC(Universal Time Coordinated, 협정세계시, 協定世界時) (* * Filename: TestCTimeApp.fs * * Compile: fsc --codepage:949 TestCTimeApp.fs * * Execute: TestCTimeApp *) // namespace MyTestApp1 open System let weekNames = [| "일"; "월"; "화"; "수"; "목"; "금"; "토" |] let now = System.DateTime.Now let startOfEpoch = new DateT..

프로그래밍/F# 2010.07.13

80컬럼 컨솔에 19단표 출력하기 예제 for F#

Python용 소스파일 testForFor.py를 F# 용으로 수정한 것이다. F#의 if 구문과 for 구문이 Python의 것과 (들여쓰기하는 것 까지) 많이 닮았다. F#은 함수형 언어기도 하고 명령형 언어이기도 하므로, 여기서는 가급적 F#의 명령형 언어의 특징을 위주로 작성하였다. (* * Filename: TestForFor.fs * * Compile: fsc TestForFor.fs * Execute: TestForFor * * Date: 2010. 7. 13 *) # light let getDan dan = let t = [| for i in 1..19 -> "" |] for j in 0..(19 - 1) do let mutable sa = sprintf "%d" dan if (String..

프로그래밍/F# 2010.07.13

(최대공약수 구하기) while 반복문 없는 예제 for F#

소스 파일명: TestNoWhile.fs (* * Filename: TestNoWhile.fs * * Purpose: Example using the while loop syntax * while .... * * Compile: fsc --codepage:949 TestNoWhile.fs * Execute: TestNoWhile -200 300 *) #light // 사용법 표시 let printUsage x = printfn "Using: TestNoWhile [integer1] [integer2]" printfn "This finds the greatest common divisor of the given two integers." let cmdArgs = System.Environment.GetCo..

프로그래밍/F# 2010.07.13

(최대공약수 구하기) while... 반복문 예제 for F#

소스 파일명: TestWhile.fs (* * Filename: TestWhile.fs * * Purpose: Example using the while loop syntax * while .... * * Compile: fsc --codepage:949 TestWhile.fs * Execute: TestWhile -200 300 *) #light // 사용법 표시 let printUsage x = printfn "Using: TestWhile.py [integer1] [integer2]" printfn "This finds the greatest common divisor of the given two integers." let cmdArgs = System.Environment.GetCommandLi..

프로그래밍/F# 2010.07.13

구구단 출력 예제 for F#

F# 언어는 함수형 언어이면서도 명령형 언어의 특징을 대부분 가지고 잇다. 따라서 보통의 for 구문을 쓸 수도 있고, 재귀호출 함수릉 만들어 쓸 수도 있다. 아래의 소스에서 함수 printDan 은 보통의 for 구문을 쓴 것이고 함수 printAnotherDan 은 재귀호출 함수를 쓴 것이다. 소스 파일명: forTest.fs ------------------------------[소스 시작] let printDan dan = for i in 1..9 do printfn "%d x %d = %d" dan i (dan*i) let printAnotherDan (dan : int) = let rec loop l = match l with | x :: xs -> printfn "%d x %d = %d" d..

프로그래밍/F# 2010.07.12

명령행 인자 처리 예제 for F#

Python 언어에서 명령행 인자는 sys.argv 라는 변수로 처리한다. sys.argv는 모듈 sys에 속하는 변수 argv를 의미하며, 이는 명령행 실행시 옵션으로 입력된 인자들을 스트링 값으로 모아둔 리스트형의 변수이다. 모듈 sys을 하기 위해서는 import sys 라는 수입(import) 구문을 소스 선두 부분에 적어주어야 한다. C/C++/Java/Ruby 언어들과는 달리 리스트의 0번의 값(즉 sys.argv[0])은 python 명령 바로 다음에 입력된 (실행될) Python 소스파일명을 가리키므로, 1번부터 처리해야 한다. 즉 Python 언어에서 sys.argv[1], sys.argv[2], sys.argv[3], ... 들이 각각 C 언어의 argv[0], argv[1], argv..

프로그래밍/F# 2010.07.12

Hello 예제 for F#

컨솔에 문자 출력하는 F# 구문은 print "문자열(스트링)" 과 printfn "문자열(스트링)" 이다. 여기서 printfnd는 출력 후 개행한다는 것만 다르나. 소스 파일명: hello.fs ------------------------------[소스 시작] printfn "Hello, world!" ------------------------------[소스 끝] (한글이 있는 소스는 옵션 --codepage:949 를 사용하여 컴파일한다.) 컴파일> fsc hello.fs 실행> hello Hello, world!

프로그래밍/F# 2010.07.12