스트링 리스트에서 스트링 찾기(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 = ["하나"; "둘"; "셋"; "넷"; "다섯"; "여섯"]
printf "list: "
printList words
printfn ""
let where1 = find words "셋"
if where1 >= 0 then
printf "발견! "
printf "Next word of 셋 in list: %O" (words.[where1+1])
printfn ""
printfn "Sorting..."
let otherwords = words |> List.sort |> List.sort
printf "list: "
printList otherwords
printfn ""
let where2 = find otherwords "셋"
if where2 >= 0 then
printf "발견! "
printf "Next word of 셋 in list: %O" (otherwords.[where2+1])
printfn ""
------------------------------------------------
컴파일> fsc --codepage:949 TestStringFindInList.fs
실행> TestStringFindInList
list: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견! Next word of 셋 in list: 넷
Sorting...
list: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견! Next word of 셋 in list: 여섯