Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
CSE4102-HW2/env1.sml
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
16 lines (12 sloc)
688 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(*Used when trying to look up a name that doesn't exist*) | |
exception NameNotBound of string; | |
(*Used to store the list of current bindings, each with a var name and value*) | |
type Env = (string * int) list; | |
(*Creates an empty environment*) | |
fun env_new () : Env = nil; | |
(*Adds a new element with the given name and value to the front of the given Env list*) | |
fun env_bind (e : Env) name num : Env = (name, num) :: e; | |
(*Looks up the value of a given name in the given Env | |
Raises NameNotBound exception if name not found in the given Env*) | |
fun env_lookup (nil : Env) name = raise NameNotBound name | |
| env_lookup (e: Env) name = if name = #1 (hd e) then #2 (hd e) else env_lookup (tl e) name; |