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-González 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 [email protected] 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: Physics–Data processing. — Functional programming (Computer science) Classification: LCC QC52 .W34 2023 (print) — LCC QC52 (ebook) — DDC 530.0285/5133–dc23/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 California’s Berkeley Wireless Research Center, where he used it to model wireless communication systems. For many years he maintained MacPort’s 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. BRIEF CONTENTS Acknowledgments Introduction PART I: A HASKELL PRIMER FOR PHYSICISTS Chapter 1: Calculating with Haskell Chapter 2: Writing Basic Functions Chapter 3: Types and Entities Chapter 4: Describing Motion Chapter 5: Working with Lists Chapter 6: Higher-Order Functions Chapter 7: Graphing Functions Chapter 8: Type Classes Chapter 9: Tuples and Type Constructors Chapter 10: Describing Motion in Three Dimensions Chapter 11: Creating Graphs Chapter 12: Creating Stand-Alone Programs Chapter 13: Creating 2D and 3D Animations PART II: EXPRESSING NEWTONIAN MECHANICS AND SOLVING PROBLEMS Chapter 14: Newton’s Second Law and Differential Equations Chapter 15: Mechanics in One Dimension Chapter 16: Mechanics in Three Dimensions Chapter 17: Satellite, Projectile, and Proton Motion Chapter 18: A Very Short Primer on Relativity Chapter 19: Interacting Particles Chapter 20: Springs, Billiard Balls, and a Guitar String PART III: EXPRESSING ELECTROMAGNETIC THEORY AND SOLVING PROBLEMS Chapter 21: Electricity Chapter 22: Coordinate Systems and Fields Chapter 23: Curves, Surfaces, and Volumes Chapter 24: Electric Charge Chapter 25: Electric Field Chapter 26: Electric Current Chapter 27: Magnetic Field Chapter 28: The Lorentz Force Law Chapter 29: The Maxwell Equations Appendix: Installing Haskell Bibliography Index CONTENTS IN DETAIL ACKNOWLEDGMENTS INTRODUCTION Who This Book Is For Why Functional Programming, and Why Haskell? About This Book PART I A HASKELL PRIMER FOR PHYSICISTS 1 CALCULATING WITH HASKELL A Kinematics Problem The Interactive Compiler Numeric Functions Operators Precedence and Associativity The Application Operator Functions with Two Arguments Numbers in Haskell Negative Numbers in Haskell Decimal Numbers in Haskell Exponential Notation Approximate Calculation Errors Getting Help and Quitting More Information Summary Exercises