LEARN PHYSICS WITH FUNCTIONAL PROGRAMMING
A Hands-on Guide to Exploring Physics with Haskell
by Scott N. Walck
San Francisco
LEARN PHYSICS WITH FUNCTIONAL PROGRAMMING. Copyright 2023 by Scott N. Walck.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
First printing
26 25 24 23 22 1 2 3 4 5
ISBN-13: 978-1-7185-0166-9 (print)
ISBN-13: 978-1-7185-0167-6 (ebook)
Publisher: William Pollock
Managing Editor: Jill Franklin
Production Manager: Sabrina Plomitallo-Gonzlez
Production Editor: Miles Bond
Developmental Editor: Alex Freed
Cover Illustrator: Gina Redman
Interior Design: Octopod Studios
Technical Reviewer: Gregory Wright
Copyeditor: George Hale
Proofreader: Bart Reed
Indexer: Sanjiv Kumar Sinha
For information on distribution, bulk sales, corporate sales, or translations, please contact No Starch Press, Inc.
directly at or:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1.415.863.9900
www.nostarch.com
Library of Congress Cataloging-in-Publication Data
Names: Walck, Scott N., author.
Title: Learn physics with functional programming : a hands-on guide to exploring physics with Haskell / by Scott N. Walck.
Description: San Francisco : No Starch Press, [2023] Includes bibliographical references and index.
Identifiers: LCCN 2022018706 (print) LCCN 2022018707 (ebook) ISBN 9781718501669 (print) ISBN 9781718501676 (ebook)
Subjects: LCSH: PhysicsData processing. Functional programming (Computer science)
Classification: LCC QC52 .W34 2023 (print) LCC QC52 (ebook) DDC 530.0285/5133dc23/eng20220722
LC record available at https://lccn.loc.gov/2022018706
LC ebook record available at https://lccn.loc.gov/2022018707
No Starch Press and the NoStarch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
To Peggy, Carl, Dan, and Jodi
About the Author
Scott N. Walck has a PhD in physics from Lehigh University. He has taught physics, including computational physics, to undergraduates (physics majors and non-majors) for 20 years at Lebanon Valley College, where he has been recognized with a Distinguished Teaching Award. Walck is a three-time NSF grant recipient for research in quantum information and is the author of more than 30 peer-reviewed research articles in physics.
About the Technical Reviewer
Gregory Wright received his PhD in physics from Princeton University, where he built receivers for studying cosmic microwave background radiation. He did postdoctoral studies at Bell Laboratories and was later hired as a Member of Technical Staff there. He was introduced to Haskell when he was a Visiting Industrial Fellow at the University of Californias Berkeley Wireless Research Center, where he used it to model wireless communication systems. For many years he maintained MacPorts version of the Glasgow Haskell Compiler (GHC) and contributed to porting GHC to the AMD64 architecture on both macOS and FreeBSD. He was (probably) the first person to build GHC from source on the continent of Antarctica and encourages others to build their tools from source.
CONTENTS IN DETAIL
PART I
A HASKELL PRIMER FOR PHYSICISTS
1
CALCULATING WITH HASKELL
2
WRITING BASIC FUNCTIONS
3
TYPES AND ENTITIES
4
DESCRIBING MOTION
5
WORKING WITH LISTS
6
HIGHER-ORDER FUNCTIONS
7
GRAPHING FUNCTIONS
8
TYPE CLASSES
9
TUPLES AND TYPE CONSTRUCTORS
10
DESCRIBING MOTION IN THREE DIMENSIONS
11
CREATING GRAPHS
12
CREATING STAND-ALONE PROGRAMS
13
CREATING 2D AND 3D ANIMATIONS
PART II
EXPRESSING NEWTONIAN MECHANICS AND SOLVING PROBLEMS
14
NEWTONS SECOND LAW AND DIFFERENTIAL EQUATIONS
15
MECHANICS IN ONE DIMENSION
16
MECHANICS IN THREE DIMENSIONS
17
SATELLITE, PROJECTILE, AND PROTON MOTION
18
A VERY SHORT PRIMER ON RELATIVITY
19
INTERACTING PARTICLES
20
SPRINGS, BILLIARD BALLS, AND A GUITAR STRING
PART III
EXPRESSING ELECTROMAGNETIC THEORY AND SOLVING PROBLEMS
21
ELECTRICITY
22
COORDINATE SYSTEMS AND FIELDS
23
CURVES, SURFACES, AND VOLUMES
24
ELECTRIC CHARGE
25
ELECTRIC FIELD
26
ELECTRIC CURRENT
27
MAGNETIC FIELD
28
THE LORENTZ FORCE LAW
29
THE MAXWELL EQUATIONS
ACKNOWLEDGMENTS
A book is never a solo endeavor, and I am grateful to have the opportunity to thank some of the many people who helped me write this book and helped me become the person who could write this book.
I thank Beall Fowler for teaching me physics, Lester Rubenfeld for showing me what mathematics could really be, and Gerald Sussman for conversations about physics and functional programming. I thank Scott Pilzer for being my friend and conversation partner about the most interesting parts of physics.
Lebanon Valley College supported me by granting a sabbatical leave to work on this book.
I thank my colleagues David Lyons, Mike Day, Barry Hurst, Keith Veenhuizen, and Dan Pitonyak for interesting discussions on a number of issues addressed in the book. I thank Brent Yorgey for helping me learn Haskell, and David Thrane Christiansen for conversations about functional programming and publishing.
I thank Alex Heilman, Ben Lippmeier, Greg Horn, and John Kearney for reading sections of the manuscript and providing valuable comments. Nick Durofchalk and Michel Malda helped with Windows installation issues. Ben Gordon gave helpful feedback. I thank Gregory Wright for being the technical reviewer. He looked carefully at the exercises, ran the code, and came up with a number of ideas for making the book better.
I thank Bill Pollock for believing in this project enough to put real resources behind it. I am grateful for the work of Alex Freed, my developmental editor at No Starch. Alex saw the possibility of coolness in using a functional programming language to write about physics and was the principal shepherd of this project through publication, at least from the perspective of the author. She quickly pinpointed the essential weaknesses in my writing, and made substantial edits that improved the exposition without changing the sense of what I was trying to convey.
I am grateful to my medical caregivers, especially Witold Rybka, Seema Naik, Giampaolo Talamo, Mitzi Lowe, and all the nurses at the Penn State Cancer Institute, who kept me alive and well enough to keep working until the project was finished.