In the book "Expert F# 3.0", there is a text-parsing example to 'lex' and 'parse' polynomial expressions. I was trying to understand it a bit (there was no explanation for the code written) and I came across functions such as this:
let parseIndex src =
match tryToken src with
| Some(Hat,src) ->
match tryToken src with
| Some(Int num,src) -> (num,src)
| _ -> failwith "expected an int after ^"
| _ -> (1,src)
which uses the function
let tryToken (src:TokenStream) =
match src with
| head::rest -> Some(head, rest)
| _ -> None
the function parseIndex
uses the parameter src
and as the code progresses by using tryToken
multiple times,every time, the returned src
is somehow something else but the function still uses that name!
my question is: what is parseIndex
really doing here with src
? because on the second pattern-matching, it uses src
as if it was a deffirent value given by tryToken
, but looking at tryToken, I see that it should give the same result on every use with pattern-matching.
the Hat
and Int
you see are union cases of Token
, as type TokenStream = Token list