0

I'm working on an assignment and was given the following function:

fun label (lb,ub) =
let val s = callcc (fn k =>let fun branch c = if (c < ub)
                                              then (pushCP (k,branch,c+1);c)
                                              else ub
                           in (pushCP(k,branch,lb+1);lb)
                           end)
in {value=s,dom=(lb,ub)}
end;

If you put a lower and upper bound of let's say 1 and 3into this function it would print

val it = {dom=(1,3), value=1}

What I am trying to figure out is if it is at all possible to get the value. In my notes it says two possible ways of doing this would be through #value(x) or by doing val {value=a,...} = x, but I get errors both ways with this. Any clue what I am doing wrong?

oihnkj
  • 1
  • 2

1 Answers1

2

It isn't clear what you are doing wrong since you haven't reproduced what you actually tried, but your notes are correct:

- val x = {dom=(1,3), value=1};
val x = {dom=(1,3),value=1} : {dom:int * int, value:int}

The first method is to use #value to extract the value field and #dom to extract the dom field:

- #value x;
val it = 1 : int
- #dom x;
val it = (1,3) : int * int

The second method is to use pattern matching. You can extract individual fields:

- val {value = a,...} = x;
val a = 1 : int

Or you can extract both fields at once:

- val {dom = a, value = b} = x;
val a = (1,3) : int * int
val b = 1 : int

In order for the above to work, x needs to be bound to the value. Perhaps you are trying to use this with an x which hasn't been given a val binding. You would need to have something like this:

val x = label(1,3)

I don't have all the relevant code so I can't test. If the above isn't enough to answer your question, you need to provide more details.

John Coleman
  • 51,337
  • 7
  • 54
  • 119