'프로그래밍/F#'에 해당되는 글 31건

  1. 2010.07.08 F# 프로그래밍에서 String.split 가 없다고 불평할 때

목적:
http://geekswithblogs.net/MarkPearl/archive/2010/04/02/creating-my-first-f-program-in-my-new-ldquoexpert-f.aspx 에 올려진 (실행되지 않는) F# 예제가 실행되도록 하자.


// 파일명: testString.fs (에러가 많은 원래 소스)

#light

  let wordCount text =
    let words = Split [' '] text
    let wordset = Set.ofList words
    let nWords = words.Length
    let nDups = words.Length - wordSet.Count
    (nWords, nDups)

let showWordCount text =
    let nWords,nDups = wordCount text
    printfn "--> %d words in text" nWords
    printfn "--> %d duplicate words" nDups



1. 우선 위의 소스를 그대로  컴파일하였다.

컴파일하니 아래와 같이 에러가 2개 뜬다.
 
D:\test\f#>fsc testStringSplit.fs
Microsoft (R) F# 2.0 Compiler build 2.0.0.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

testStringSplit.fs(5,17): error FS0039: The value or constructor 'Split' is not
defined

testStringSplit.fs(8,32): error FS0039: The namespace or module 'wordSet' is not
 defined



2. String.split 해결하기

우선 String.split 가 없다고 하는 문제를 해결하기 위해 #r 디렉티브를 써서 소스 첫 부분에
#r "FSharp.PowerPack.dll";;
#r "FSharp.PowerPack.Compatibility.dll";;
릉 추가하여 다시 컴파일하였다. (여기도 FSharp.PowerPack.Compatibility.dll 도 꼭 있어야 함이 중요하다. 이것을 모르면 엉뚱한 곳에서 삽질만 하게 된다.)


// 1차 수정된 파일명: testString.fs

#r "FSharp.PowerPack.dll";;
#r "FSharp.PowerPack.Compatibility.dll";;

#light

let
wordCount text =
    let words = Split [' '] text
    let wordset = Set.ofList words
    let nWords = words.Length
    let nDups = words.Length - wordSet.Count
    (nWords, nDups)

let showWordCount text =
    let nWords,nDups = wordCount text
    printfn "--> %d words in text" nWords
    printfn "--> %d duplicate words" nDups



이제 1차 수정된 소스를 컴파일하였다.
아래 처럼 #r 디렉티브에 관련된 에러가 뜬다.

D:\test\f#>fsc testStringSplit.fs
Microsoft (R) F# 2.0 Compiler build 2.0.0.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

testStringSplit.fs(1,1): error FS0076: #r directives may only occur in F# script
 files (extensions .fsx or .fsscript). Either move this code to a script file, a
dd a '-r' compiler option for this reference or delimit the directive with '#if
INTERACTIVE'/'#endif'.

testStringSplit.fs(2,1): error FS0076: #r directives may only occur in F# script
 files (extensions .fsx or .fsscript). Either move this code to a script file, a
dd a '-r' compiler option for this reference or delimit the directive with '#if
INTERACTIVE'/'#endif'.


3. 파일 확장명 바꾸기

#r 디렉티브가 있는 소스의 확장명은 fsx라야 한다고 하니 그렇게 바꾸었다.

// 2차 수정된 파일명: testString.fsx (소스는 그대로이고 파일 확장명만 fsx로 변경)

#r "FSharp.PowerPack.dll";;
#r "FSharp.PowerPack.Compatibility.dll";;

#light

let
wordCount text =
    let words = Split [' '] text
    let wordset = Set.ofList words
    let nWords = words.Length
    let nDups = words.Length - wordSet.Count
    (nWords, nDups)

let showWordCount text =
    let nWords,nDups = wordCount text
    printfn "--> %d words in text" nWords
    printfn "--> %d duplicate words" nDups



2차 수정된 소스를 컴파일 하였다.
컴파일하기 위해서는 2개의 파일 FSharp.PowerPack.dll 과 Fharp.PowerPack.Compatibility.dll 이 현재 디렉토리에 있어야 한다. 이는  http://fsharppowerpack.codeplex.com/  에서 구할 수 있다.

D:\est\f#>fsc testStringSplit.fsx
Microsoft (R) F# 2.0 Compiler build 2.0.0.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

testStringSplit.fsx(7,17): error FS0039: The value or constructor 'Split' is not
 defined

testStringSplit.fsx(10,32): error FS0039: The namespace or module 'wordSet' is n
ot defined


4, Split가 아니라 String.split

// 3차 수정된 파일명: testString.fsx (소스의 7째 줄 Split을 String.split로 수정)

#r "FSharp.PowerPack.dll";;
#r "FSharp.PowerPack.Compatibility.dll";;

#light

let
wordCount text =
    let words = String.split [' '] text
    let wordset = Set.ofList words
    let nWords = words.Length
    let nDups = words.Length - wordSet.Count
    (nWords, nDups)

let showWordCount text =
    let nWords,nDups = wordCount text
    printfn "--> %d words in text" nWords
    printfn "--> %d duplicate words" nDups



3차 수정된 소스를 컴파일 하였다.


D:\test\f#>fsc testStringSplit.fsx
Microsoft (R) F# 2.0 Compiler build 2.0.0.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

testStringSplit.fsx(10,32): error FS0039: The namespace or module 'wordSet' is n
ot defined


5. 4차 수정 (오타를 바로 잡다)

// 4차 수정된 파일명: testString.fsx (소스의 8째 줄 wordset을 wordSet으로 수정)

#r "FSharp.PowerPack.dll";;
#r "FSharp.PowerPack.Compatibility.dll";;

#light

let
wordCount text =
    let words = String.split [' '] text
    let wordSet = Set.ofList words
    let nWords = words.Length
    let nDups = words.Length - wordSet.Count
    (nWords, nDups)

let showWordCount text =
    let nWords,nDups = wordCount text
    printfn "--> %d words in text" nWords
    printfn "--> %d duplicate words" nDups


 

4차 수정된 소스를 컴파일 하였다.
컴파일이 잘된다. 그런데  Main 모듈이 없다는 경고(warning)가 뜬다.

D:\test\f#>fsc testStringSplit.fsx
Microsoft (R) F# 2.0 Compiler build 2.0.0.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

testStringSplit.fsx(18,1): warning FS0988: Main module of program is empty: noth
ing will happen when it is run


Main 모듈이 없다는 위의 경고를 무시하고 실행해보았다.

D:\test\f#>TestFSharpMatrix

실행은 되지만 역시 아무 출력이 없다.



6. 실행을 위한 Main 모듈 추가하기

// 5차 수정된 파일명: testString.fsx (소스의 끝 부분에 Main 모듈 추가)

#r "FSharp.PowerPack.dll";;
#r "FSharp.PowerPack.Compatibility.dll";;

#light

let
wordCount text =
    let words = String.split [' '] text
    let wordSet = Set.ofList words
    let nWords = words.Length
    let nDups = words.Length - wordSet.Count
    (nWords, nDups)

let showWordCount text =
    let nWords,nDups = wordCount text
    printfn "--> %d words in text" nWords
    printfn "--> %d duplicate words" nDups

[<EntryPoint>]
let main args =
    showWordCount "let wordCount text let showWordCount text"
    0        // Return 0. This indicates success.



마지막으로 수정된 위의 소스를 컴파일한다.

D:\test\f#>fsc testStringSplit.fsx
Microsoft (R) F# 2.0 Compiler build 2.0.0.0
Copyright (c) Microsoft Corporation. All Rights Reserved.


컴파일된 실행파일(testStringSplit.exe)을 실행한다.

D:\test\f#>testStringSplit
--> 6 words in text
--> 2 duplicate words



* 이로서 이 글의 선두에 적어 놓은 목적이 해결되었다.



Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,