functional programming - How can I convert my data type? -
i define data type multi list: datatype intnest= int of int | list of intnest list;
i'm trying write function can convert type main type.
for example: [int 1, int 2, list[int 6, int 8]] => [1,2, [6,8]]
how can this? in advance help
with reference your previous question, can't!
as tried explain: not possible have such value in sml!
just try out in interpreter
- val _ = [1,2, [6,8]]; stdin:28.9-28.21 error: operator , operand don't agree [literal] operator domain: int * int list operand: int * int list list in expression: 2 :: (6 :: 8 :: nil) :: nil
if construct list basic constructs instead of syntactic sugar, list [1,2,3]
becomes 1 :: 2 :: 3 :: nil
now, if @ types of 2 "building blocks" cons , nil
- op::; val = fn : 'a * 'a list -> 'a list - nil; val = [] : 'a list
then see infix operator cons, takes argument of type 'a
on left hand side , list of 'a
on right hand side.
can see if build list of ints, must give int on left hand side , list of ints on right hand side (as example above). should clear can't this:
[1, 2] :: 3 :: nil
which equivalent (1 :: 2 :: nil) :: 3 :: nil
Comments
Post a Comment