Wednesday, 27 October 2010

Very Simple F# Compiler (Part 2 Revisited)

Just a quick note today to say that I have done about 30 seconds reading of some F# documentation and found out that reference cells can indeed be used in closures while local mutable values cannot. As it is a trivial exercise to re-write my character source to use local reference cells rather that global mutable values I have done so. Below is the updated code - I've also been able to remove those globals from my program.
  1 let getNextCharFromString (source : string) =
2
let sourceList = ref (List.ofSeq source)
3
let endOfString = ref (false)
4
fun () ->
5
match (!sourceList, !endOfString) with
6
| _, true -> failwith "Past End of String"
7
| [], _ ->
8
endOfString := true
9
char 0
10
| h :: t, _ ->
11
sourceList := t
12
h

No comments:

Post a Comment