프로그래밍/F#
스트링 배열 정렬(sorting)하기 with F#
Scripter
2010. 7. 16. 17:54
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..] |> Array.toList |> List.sort
printList b
------------------------------------------------
컴파일> fsc TestSort.fs
실행> TestSort one two three four five
[five, four, one, three, two]
실행> TestSort 하나 둘 셋 넷 다섯
[넷, 다섯, 둘, 셋, 하나]