
-----------------------------------
Planet_Fall
Wed Dec 25, 2013 11:40 pm

Dictionary Variables??
-----------------------------------
Is there a dictionary variable for Turing like Ruby or Python? I'm trying to make a Periodic Table for Chemistry Class. 



/*  Programmer: Planet_Fall
 File Name:  Mole Mass
 */

%Variables
var H, Li, Be, B, C, N, O, F, Na, Mg, Al, Si, P, S, Cl, K, Ca : real

H := 1.00794
Li := 6.941
Be := 9.012182
B := 10.811
C := 12.0107
N := 14.0067
O := 15.994
F := 18.99840
Na := 22.989770
Mg := 24.3050
Al := 26.981538
Si := 28.0855
P := 30.97376
S := 32.065
Cl := 35.453
K := 39.0983
Ca := 40.078

put  H, Li, Be, B, C, N, O, F, Na, Mg, Al, Si, P, S, Cl, Na, Mg


Please specify what version of Turing you are using
Turing 4.1.1

-----------------------------------
Raknarg
Thu Dec 26, 2013 12:15 am

RE:Dictionary Variables??
-----------------------------------
No such thing as dictionaries here. However because you have an ordered set of information, it would make more sense anyways just to use arrays, like so:


type elementData:
    record
        symbol : string
        mass : real
    end record

var element : array 1 .. numElements of elementData

element (1).symbol := "H"
element (1).mass := 1.00794

element (2).symbol := "Li"
%etc, etc


You could simplify it further by doing this:


function set_element (symbol : string, mass : real) : elementData
    var newElement : elementData
    newElement.symbol := symbol
    newElement.mass := mass

    result newElement
end set_element

element (1) := set_element ("H", 1.0079)


-----------------------------------
Tony
Thu Dec 26, 2013 12:24 am

RE:Dictionary Variables??
-----------------------------------
One of the advantages of a dictionary / hashmap, is that one can get the value 1.00794 by referencing element("H"), instead of element(1).

As Raknarg points out, hashmaps are not build into Turing.

-----------------------------------
Raknarg
Thu Dec 26, 2013 1:42 am

RE:Dictionary Variables??
-----------------------------------
I understand, but unless you want to build a hash table for Turing, that's what you get.

Unless you wanted to build a data structure that worked like a very slow dictionary.

-----------------------------------
evildaddy911
Thu Dec 26, 2013 9:26 am

RE:Dictionary Variables??
-----------------------------------
i feel like i should point out your missing helium...

-----------------------------------
np_123
Sun Dec 29, 2013 2:22 pm

RE:Dictionary Variables??
-----------------------------------
Neon (Ne) and Argon (Ar) are missing from that list too...
Unless you're purposely not including the noble gases

-----------------------------------
scholarlytutor
Thu Sep 24, 2020 11:31 pm

Re: Dictionary Variables??
-----------------------------------
I stumbled upon this old thread and I wanted to point out two workarounds:

If you were just writing a simple program, which I once did, to output a chemical name when the user types a key like H, a case block works:

var element: string

case element of
label "H": 
    put "Hydrogen"
  label "He":
    put "Helium"
end case

And so on. See my attachment.


A more clever workaround uses enumerations:

type Element : enum(H, He, Li)

var PeriodicTable: array Element of string := init("Hydrogen", "Helium", "Lithium")

put PeriodicTable (Element.H)


It is more inconvenient because you have to write the 'Element.' each time before a key, but when you really need something that imitates a dictionary in Turing, this is the best solution I could think of.
