← 返回 openai 的题目列表Implement Node and Function Classes with Type Inference
类型:online_judge
You need to implement two classes for a toy language to represent primitives, tuples, and functions. The language supports primitives (e.g., char, int, float), generics (like T1, T2), and tuples (consisting of primitives, generics, or other tuples).
Requirements:
Implement a Node class to represent primitives and tuples.
Constructor takes value (optional str) and children (optional list of Node).
Pass value if primitive, children if tuple.
Implement a Function class to represent functions.
Constructor takes params (list of Node) and return (Node).
Implement a to_str method for Function and Node.
Implement infer_return(function, param) -> return.
Given function and param values, return actual return type after substituting out generics (e.g., T1, T2).
Must raise error if there is type mismatch or conflict.
Example:
Function: [T1, T2, int, T1] -> [T1, T2]
Params: [int, char, int, int]
Should return: [int, char]
If params was [int, int, int, int] then raise error for type mismatch (int vs char). If params was [int, int, int, char] then raise error for type conflict.
Example
Input
Params: [int, char, int, int], Function: [T1, T2, int, T1] -> [T1, T2]