dialect "rtobjectdraw"

// Create list of vowels and extract first and last
var vowels: List⟦String⟧ := list⟦String⟧ ["a","e","i","o","u"]
def first: String = vowels.at(1)
def last: String = vowels.at(5)

// print out all the vowels
for (vowels) do {letter: String -> 
    print (letter) 
}

// Find where new should be inserted into aList that is ordered, 
// using a while loop and eager "and" operator
method bestSlotWhile (new:String) in (aList:List⟦String⟧) → Number {
    var slotForS: Number := 1
    while {(slotForS ≤ aList.size) && {(new > aList.at(slotForS))} } do {
        slotForS := slotForS + 1
    }
    return slotForS
}

// Find where new should be inserted into aList that is ordered, 
// order using a for loop
method bestSlotFor (new:String) in (aList:List⟦String⟧) → Number {
    for (1 .. aList.size) do {slotForS: Number →
        if (new ≤ aList.at(slotForS)) then {
            return slotForS
        }
    }
    aList.size + 1 
}

// Insert new into aList at slot where, moving elements over to the
// right to make room.
method insert(new: String) at (where: Number) in (aList:List⟦String⟧) → Done {
    var index: Number := aList.size
    while {index ≥ where} do {
        aList.at(index + 1) put (aList.at(index))
        index := index - 1
    }
    aList.at (where) put (new)
}

// insert new in order into sorted aList.
method insertInOrder (new:String) in (aList: List⟦String⟧) → Done {
    var slot: Number := bestSlotFor (new) in (aList)
    insert (new) at (slot) in (aList)
}

// Test the methods defined above
print(bestSlotFor("c") in (vowels))

insert("c") at (2) in (vowels)
print "New vowels: {vowels}"

insertInOrder ("y") in (vowels)
print "yvowels: {vowels}"

// Adds "w" at end of vowels -- expanding it by one
vowels.at(8) put ("w")
print (vowels)


