Lesson 30 of 55
9 mins readPython Record Syntax & Named Fields
In Plain English
Record syntax names the fields of a data type, automatically creating getter accessor functions and enabling clean named field instantiation and updates.
Deep Dive: How It Works
Record Definition: data Person = Person { name :: String, age :: Int } deriving (Show).
Automatic Accessors: Haskell creates name :: Person -> String and age :: Person -> Int automatically.
Record Updates: person { age = 30 } creates a new Person with updated age without modifying the original.
Core Rules to Remember

Auto-Generated Getters: Named fields create accessor functions automatically.

Immutable Record Update: p { age = 21 } returns a copy with updated field values.
Live Interactive Example
Hit Run Code to see it livePerson Record and Non-Destructive Update
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Output Console
Click "Run Code" to view the rendered output.
How it works: server2 is a new record with portNum updated to 443.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Define Book Record
Define `data Book = Book { title :: String, pages :: Int } deriving (Show)`.
Create `b = Book { title = "Learn Haskell", pages = 350 }`.
Print `"Book: "` followed by `title b`.
1
2
3
4
5
6
7
8
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.