Loading ...
Sorry, an error occurred while loading the content.

trying to understand the use of keyword "with"

Expand Messages
  • Jean Saint-Remy
    Hi, I m having difficulties understanding the keyword with in the following code. I have searched in the manual and came upon exception syntax. It appears
    Message 1 of 2 , Jul 17, 2013
    • 0 Attachment
      Hi,

      I'm having difficulties understanding the keyword "with" in the following code. I have searched in the manual and came upon exception syntax. It appears the force of "with" is the same as in matching. Would you be so kind and explain the syntax in greater detail?

      Thanks,

      jean



      type position = { symbol : string; holding : int; pprice : float; } ;;
      type account = { name : string; max_ind_holding : float; pos : position list; } ;;

      let cur_prices =

      [
        ("IBM", 82.48);
        ("GOOG", 406.16);
        ("MMM", 81.03);
      ] ;;
      let gen_account acdb accname max_holding init_pos =

      if (Hashtbl.mem acdb accname) then raise (Invalid_argument "Account name already in use")
      else
        Hashtbl.add accname
        {

           name = accname;
           max_ind_holding = max_holding;
          pos = init_pos;
        } ;;
      let account_buy (symb, price) quant acc =

      (* here's the problem line *)
      {acc with pos =

        ({ symbol = symb; holding = quant; pprice = price } :: acc.pos )} ;;
      let account_sell (symb, price) acc =

      let rec seller sym prc pos_list soldq soldv newposlst = match pos_list with
      | [] -> ((soldq, soldv), (* again here *) {acc with pos = newposlst})
      | h :: t -> if (h.symbol = sym) then
         seller sym prc t (soldq + h.holding )(
             (( float_of_int h.holding) *. (prc -. h.pprice)) +. soldv) newposlst
      else
         seller sym prc t soldq soldv (h :: newposlst)
      in

      seller symb price acc.pos 0 0. [] ;;
      let buy db acount_name (symbol_name, price) quantity =

        let acc = Hashtbl.find db account_name in

          Hashtbl.replace db account_name (account_buy (symbol_name, price) quantity acc) ;;
      let sell db account_name (symbol_name, price) =

        let acc = Hashtbl.find db account_name in

        let (( quant, profit), newacc) =

           account_sell (symbol_name, price) acc in

             Hashtbl.replace db account_name newacc; (quant, profit) ;;

      The key to understanding the code is the use of tuple (string, float) for symbol and price of a stock and using cons to create a list of positions under account "name". What is confusing for me is that lists should consist of like objects, but we have records to accommodate string, int, and float.


      [Non-text portions of this message have been removed]
    • Lukasz Stafiniak
      On Thu, Jul 18, 2013 at 12:09 AM, Jean Saint-Remy ... all of: a string, an int and a float. Using the syntax {acc with pos = {symbol= x ; holding=42;
      Message 2 of 2 , Jul 17, 2013
      • 0 Attachment
        On Thu, Jul 18, 2013 at 12:09 AM, Jean Saint-Remy
        <jeansaintremy@...>wrote:

        > **
        >
        >
        > Hi,
        >
        > I'm having difficulties understanding the keyword "with" in the following
        > code. I have searched in the manual and came upon exception syntax. It
        > appears the force of "with" is the same as in matching. Would you be so
        > kind and explain the syntax in greater detail?
        >
        > Thanks,
        >
        > jean
        >
        > type position = { symbol : string; holding : int; pprice : float; } ;;
        > type account = { name : string; max_ind_holding : float; pos : position
        > list; } ;;
        >
        > The field "pos" stores a list of records, each record in the list stores
        all of: a string, an int and a float. Using the syntax

        {acc with pos = {symbol="x"; holding=42; pprice=3.14}::acc.pos}

        you create a value almost the same as "acc" but with "pos" field containing
        a list with one additional record in front. The record contains all of "x",
        42 and 3.14 (as it must contain exactly three fields).


        [Non-text portions of this message have been removed]
      Your message has been successfully submitted and would be delivered to recipients shortly.