Visual Studio 2010 을 설치하면 Visual F# 도 함께 설칟히기 때문에 Visual Studio 2008 때 처럼 Visual F# 을 별도로 설치하지 않아도 된다.
2011년 3월 3일에 Visual Studio 2010 Service Pack 1 이 출시되면서 Visual F# 도 업데이트되었다.

FSharp 홈페이지: http://www.fsharp.net/

다음은 간단한 F# Hello 예제이다. 컨솔 출력을 위해 printfn 함수를 쓰고 았다


printfn "Hello"
printfn "안녕하세요?"



저 소스를 파일명 hello.fs (또는 hello.fsx) 로 저장하였다면,

fsc --codepage:949 hello.fs

명령으로 컴파일한다, 그러면 실행파일 hello.exe 가 생성된다,
이를 실핼하면 컨솔에 원하는 글다가 출력된다. (한글도 잘 출력된다.)
컴파일하기 위해서는 일반 명령창 대신 윈도우의

"시작메뉴" -> "Microsoft Visual Studio 2010"
-> "Visual Studio Tools"
-> "Visual Studio 명령 프롬프트(2010)"

하여 열리는 을 열어서 Visual Studio 명령 프롬프트 에서 컴파일하고 실행하는 것이 좋다.

이제 GUI Hello 예제(그래픽 사용자 인터페이스()를 갖는 Hello 예제)를 작성해 보자.
(파일명 HelloWorldFSharpForm.fs 로 저장하였다.)

(* 윈도우 7에서는 폰트, 위젯의 사이즈가 윈도우 XP와 맞지 않아 다소 수정하였다. *)

// Filename: HelloWorldFSharpForm.fs
//
// Compile: fsc --codepage:949 --target:winexe --platform:x86 HelloWorldFSharpForm.fs
// Execute: HelloWorldFSharpForm

open System
open System.Drawing
open System.Windows.Forms

// Create the form and a label objects
let frm = new Form(Text = "Hello world by F#", Width = 400, Height = 170)      // Windows 7 을 위해 수정
let btn = new Button(Text = "인사하기", Width=120, Height=30,                          // Windows 7 을 위해 수정
                    TextAlign = ContentAlignment.MiddleCenter)
let displaybox = new Label(Width=320, Height=35,
                    TextAlign = ContentAlignment.MiddleLeft)
let editbox = new TextBox(Width=200, Height=35,
                    TextAlign = HorizontalAlignment.Left)

// Set ‘Font’ property of the label, textbox, button
btn.Font <- new Font("궁서체", 14.0f)
btn.BackColor <- Color.FromArgb(32, 216, 32)
btn.Location <- new System.Drawing.Point(240, 76)
displaybox.BackColor <- Color.FromArgb(255, 255, 255)
displaybox.Font <- new Font("Vernada", 14.0f, FontStyle.Bold)
displaybox.Location <- new Point(15, 20)
editbox.BackColor <- Color.FromArgb(255, 255, 255)
editbox.Location <- new Point(15, 80)
frm.Controls.Add(btn)
frm.Controls.Add(displaybox)
frm.Controls.Add(editbox)
frm.Show()
let dummy = editbox.Focus()  // Set focus to the textbox


// Function that changes the text of the label for button pressed event
let sayHello (e:EventArgs) =
    let msg = editbox.Text
    let dum = editbox.Focus()  // Set focus to the textbox
    // Set the text at the label
    displaybox.Text <- String.Format("Hello, {0}!", msg)

// Function that changes the text of the label for textbox key pressed event
let greet(ev:KeyEventArgs) =
    if ev.KeyCode = Keys.Enter  then
         let msg = editbox.Text
         let dum = editbox.Focus()  // Set focus to the textbox
         // Set the text at the label
         displaybox.Text <- String.Format("Hello, {0}!", msg)

btn.Click.Add(sayHello)
editbox.KeyDown.Add(greet)

do
  Application.Run(frm)





컴파일은 명령

fsc --codepage:949 --target:winexe HelloWorldFSharpForm.fs

으로 한다다. 생성된 실행파일 HelloWorldFSharpForm.exe 를 실행한다.


다음은 윈도우 7에서 실행한 결과를 캡쳐한 그림이다.



 

Posted by Scripter
,