0

I need to make a trie to calculate the winning prize of lottery tickets with a fixed function. For the function I need to know how many children each node has. First I need to create the trie and then search it to find how many tickets won. Note that I am new to SML thus I really dont have much experience. I found a similar code (link below) in an other post that I am trying to modify. Inserting values in a Trie

The problem with this code is that I cannot alter the weight of a node as I am passing through the trie. For example: If i had already insert the number 1234 (starting from left to right) each node will have a weight of 1. If i want to add the number 1255 I would like to modify the weight of nodes 1 and 2 that are the same to 2 (+1 basically).

PS I have a lot of time limits for this exercise so I am trying to do this as efficient and fast as possible.

I tried to call again the insert function with the node I want to increase its weight by 1 but I get an error referring to the data types. I tried to change the data types of the node but I get more errors

signature DICT = sig
  type key = string                 (* concrete type *)
  type 'a entry = key * 'a          (* concrete type *)

  type 'a dict                      (* abstract type *)

  val empty : 'a dict
  val lookup : 'a dict -> key -> 'a option
  val insert : 'a dict * 'a entry -> 'a dict
  val toString : ('a -> string) -> 'a dict -> string
end;  (* signature DICT *)

exception InvariantViolationException

structure Trie :> DICT = 
struct
  type key = string
  type 'a entry = key * 'a

  datatype 'a trie = 
    Root of 'a option * 'a trie list
  | Node of 'a option * char * 'a trie list

  type 'a dict = 'a trie

  val empty = Root(NONE, nil)

  (* val lookup: 'a dict -> key -> 'a option *)
  fun lookup trie key =
    let
      (* val lookupList: 'a trie list * char list -> 'a option *)
      fun lookupList (nil, _) = NONE
        | lookupList (_, nil) = raise InvariantViolationException
        | lookupList ((trie as Node(_, letter', _))::lst, key as letter::rest) =
            if letter = letter' then lookup' (trie, rest)
            else lookupList (lst, key)
        | lookupList (_, _) =
            raise InvariantViolationException
      (*
        val lookup': 'a trie -> char list
      *)
      and lookup' (Root(elem, _), nil) = elem
        | lookup' (Root(_, lst), key) = lookupList (lst, key)
        | lookup' (Node(elem, _, _), nil) = elem
        | lookup' (Node(elem, letter, lst), key) = lookupList (lst, key)
    in
      lookup' (trie, explode key)
    end

  (*
    val insert: 'a dict * 'a entry -> 'a dict
  *)
  fun insert (trie, (key, value)) = 
    let
      (*
        val insertChild: 'a trie list * key * value -> 'a trie list
        Searches a list of tries to insert the value. If a matching letter 
        prefix is found, it peels of a letter from the key and calls insert'. 
        If no matching letter prefix is found, a new trie is added to the list.
        Invariants:
          * key is never nil.
          * The trie list does not contain a Root.
        Effects: none
      *)
      fun insertChild (nil, letter::nil, value) =[ Node(SOME(value), letter, nil) ]
        | insertChild (nil, letter::rest, value) = [ Node(SOME(value), letter, insertChild (nil, rest, value)) ]
        | insertChild ((trie as Node(SOME(oldvalue), letter', _))::lst, key as letter::rest, value) = 
            if letter = letter' then
                let
                    val newvalue = oldvalue+1
                    val ltrie=insertChild(trie::lst,letter'::nil, newvalue)
                    val trie = hd ltrie
                in
                    insert' (trie, rest, value) :: lst
                end
            else
              trie :: insertChild (lst, key, value)
        | insertChild (Root(_,_)::lst, letter::rest, value) = raise InvariantViolationException
        | insertChild (_, nil, _) = (* invariant: key is never nil *)raise InvariantViolationException

      (*val insert': 'a trie * char list * 'a -> 'a trie
Invariants:* The value is on the current branch, including potentially the current node we're on.* If the key is nil, assumes the current node is the destination.Effects: none*)

      and insert' (Root(_, lst), nil, value) = Root(SOME(value), lst)
        | insert' (Root(elem, lst), key, value)=Root(elem,insertChild(lst,key, value))
        | insert' (Node(_, letter, lst), nil, value)=Node(SOME(value),letter,lst)
        | insert' (Node(elem, letter, lst), key, value)=Node(elem,letter,insertChild (lst, key, value))
    in
      insert'(trie, explode key, value)
    end

The error I got was in this lines:

val newvalue = oldvalue+1
val ltrie = insertChild (trie::lst, letter'::nil, newvalue)

Result:

Error:
lott.sml:72.10-72.31 Error: operator and operand do not agree [overload conflict]
  operator domain: [+ ty] * [+ ty]
  operand:         'Z option * [int ty]
  in expression:
  oldvalue + 1
Drup
  • 3,679
  • 13
  • 14

0 Answers0