i have a few questions about pop() standard function and myPop() function that i have defined and related return type.
1.
fun myPop(L)= if null L then raise EmptyList else (hd L,tl L)
val myPop = fn:'a list ->'a*'a list
if i want to retrive the value and the list i can write:
val c=myPop([1,2,3]);
val val1=#1(c);
val val2=#2(c);
So val1=1 and val2=[2,3].
2.When i use standard pop() I get:
val L=[1,2,3]
pop(L);
val it = SOME (1,[2,3]) : (int * int list) option
The question is,is myPop() a right function?How can I get the single values of pop() function?
To get the single values of pop() function i tried this:
val L=[1,2,3];
val c=pop(L);
val it = SOME (1,[2,3]) : (int * int list) option // is this like a couple?
val c1=#1(c);
But I got this error:
operator domain: {1:'Y; 'Z}
operand: (int * int list) option
in expression:
(fn {1=1,...} => 1) c```
Reworking your code to be a little bit more idiomatic:
A list is not a mutable type. Nor is the
optiontype value you're getting from yourmyPopfunction.If you want to operate on what's left of the list after
myPopis called, you need to explicitly pass that list to a function call. The original list has the same value aftermyPopis called.Using
myPopto implementiter, for example:Of course, we'd really just write:
Your specific error is coming from the fact that
SOME (..., ...)is not a tuple and its contents cannot be accessed with#1,#2, etc.However, if we pattern match with the
SOMEconstructor, we can get that tuple and then use those accessors on it.E.g.
A note that if
myPopreturns an option type, then you likely wantmyPop []to returnNONErather than raise an exception.