r/ocaml • u/InternationalFan9915 • May 02 '24
Newbie problem
A few days ago I posted about having to learn Ocaml to do a college assignment. The assignment involves creating a shopping cart system with Ocaml for the backend and Java for the frontend. The professor provided a database "store.pl" and a file "main.ml" that reads this file and returns the following (in "interpreter mode"/ocaml):
# #use "main.ml";;
val read_file : string -> string list = <fun>
val parse_discount : string -> (string * float) option = <fun>
val parse_item : string -> (int * string * string * float * int) option =
<fun>
val parse_loyalty_discount : string -> (string * float) option = <fun>
val parse_shipping_cost : string -> (string * float) option = <fun>
Discount for category 'potions': 0.100000
Discount for category 'wands': 0.200000
Discount for category 'enchanted_books': 0.300000
Discount for category 'crystals': 0.150000
Discount for category 'amulets': 0.250000
Item: 1, Potion of Healing, potions, 10.000000, 50
Item: 2, Wand of Fireball, wands, 20.000000, 30
Item: 3, Enchanted Spellbook, enchanted_books, 30.000000, 20
Item: 4, Crystal of Clairvoyance, crystals, 15.000000, 40
Item: 5, Amulet of Protection, amulets, 25.000000, 25
Loyalty discount for 1 year(s): 0.050000
Loyalty discount for 2 year(s): 0.100000
Loyalty discount for 3 year(s): 0.150000
Loyalty discount for 4 year(s): 0.200000
Loyalty discount for 5 year(s): 0.250000
Loyalty discount for >5 year(s): 0.300000
Shipping cost to district Aveiro: 5.000000
Shipping cost to district Lisboa: 7.000000
Shipping cost to district Porto: 10.000000Alright, so I started by creating a file "app.ml" and I would need to receive this list in a variable to then manipulate the strings and so on. The thing is, I couldn't even get past the third line because I don't know how to receive this list.
I have something like:
open Main
let dados = Main() ?????;How should I go about it?
P.S.: Another question: can I test this code, divided into several files, through the interpreter or do I need to compile it??
Thanks for your attention!
3
u/ComanderP May 02 '24 edited May 02 '24
Hi, I'm not an ocaml pro but I think I can try to help ya.
For your main question, it's hard for me to tell you how to do it because I don't really know how main is implemented. But reading the type definitions for your function, I assume you can just call
read_filefrom main and then use your parsing functions to convert them to something more usable.Also, to use a function from module Main in another module you'd write something like this:
Regarding the testing part, you can definitely use the interpreter to test your code (a bit more tricky for importing imo), although I'm more used to writing a separate test module for that.
Hope that helps!
P.S. If you have any other question feel free to ask! Boa sorte!