Đang chuẩn bị liên kết để tải về tài liệu:
Lecture Programming languages (2/e): Chapter 14d - Tucker, Noonan
Không đóng trình duyệt đến khi xuất hiện nút TẢI XUỐNG
Tải xuống
Chapter 14d - Haskell. A number of recent developments in functional programming are not well-captured by the traditional languages, Common Lisp and Scheme. In this section, we introduce a more modern functional language, Haskell, whose features signal more clearly the present and future directions in functional programming research and applications. | Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis Contents 14.1 Functions and the Lambda Calculus 14.2 Scheme 14.3 Haskell 14.3.1 Introduction 14.3.2 Expressions 14.3.3 Lists and List Comprehensions 14.3.4 Elementary Types and Values 14.3.5 Control Flow 14.3.6 Defining Functions 14.3.7 Tuples 14.3.8 Example: Semantics of Clite 14.3.9 Example: Symbolic Differentiation 14.3.10 Example: Eight Queens 14.3.8 Example: Semantics of Clite Program state can be modeled as a list of pairs. type State = [(Variable, Value)] type Variable = String type Value = Intval Integer | Boolval Bool deriving (Eq, Ord, Show) E.g., [(“x”, (Intval 1)), (“y”,(Intval 5))] Function to retrieve the value of a variable from the state: get var (s:ss) | var == (fst s) = snd s | otherwise = get var ss State transformation Function to store a new value for a variable in the state: onion :: Variable -> Value -> State -> State onion var val (s:ss) | var == (fst s) = (var, val) : ss | otherwise = s : (onion var val ss) E.g., onion ‘y’ (Intval 4) [(‘x’, (Intval 1)), (‘y’, (Intval 5))] = (‘x’, (Intval 1)) : onion ‘y’ (Intval 4) [(‘y’, (Intval 5))] = [(‘x’, (Intval 1)), (‘y’, (Intval 4))] Modeling Clite Abstract Syntax data Statement = Skip | Assignment Target Source | Block [ Statement ] | Loop Test Body | Conditional Test Thenbranch Elsebranch deriving (Show) type Target = Variable type Source = Expression type Test = Expression type Body = Statement type Thenbranch = Statement type Elsebranch = Statement Semantics of Statements A statement transforms a state to another state: m :: Statement -> State -> State Skip is easy! m (Skip) state = state Assignments aren’t too bad either: m (Assignment target source) state = onion target (eval source state) state Loops and Conditionals Loops are a bit trickier: m (Loop t b) state | (eval t state) == (Boolval . | Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis Contents 14.1 Functions and the Lambda Calculus 14.2 Scheme 14.3 Haskell 14.3.1 Introduction 14.3.2 Expressions 14.3.3 Lists and List Comprehensions 14.3.4 Elementary Types and Values 14.3.5 Control Flow 14.3.6 Defining Functions 14.3.7 Tuples 14.3.8 Example: Semantics of Clite 14.3.9 Example: Symbolic Differentiation 14.3.10 Example: Eight Queens 14.3.8 Example: Semantics of Clite Program state can be modeled as a list of pairs. type State = [(Variable, Value)] type Variable = String type Value = Intval Integer | Boolval Bool deriving (Eq, Ord, Show) E.g., [(“x”, (Intval 1)), (“y”,(Intval 5))] Function to retrieve the value of a variable from the state: get var (s:ss) | var == (fst s) = snd s | otherwise = get var ss State transformation Function to store a new value for a variable .