:PROPERTIES: :ID: 347d2663-515b-4d9a-9ee9-7706ee86a845 :END: #+title: haskell_notes #+filetags: :index:notes:haskell:coding: * Haskell Notes ** Introduction Haskell is a purely functional programming language with strong static typing and [[id:63456626-b34e-46d9-b85e-0f1f5724aa83][lazy_evaluation]] . It was named after the logician Haskell Curry. It is widely used in academia and industry for teaching and research as well as for writing industrial applications. ** Features of Haskell - Purely Functional: Functions in Haskell are pure, meaning they don't have side effects. - Strong Static Typing: Types are checked at compile time, reducing runtime errors. - Lazy Evaluation: Expressions are not evaluated until their values are needed. - Type Inference: Haskell can automatically infer types, making the code more concise. - Concise Syntax: Haskell's syntax is clean and concise, making it easy to read and write. ** Getting Started with Haskell - Installation: To get started with Haskell, you need to install the Glasgow Haskell Compiler (GHC). You can download it from [[https://www.haskell.org/downloads/][Download]] - Basic Syntax: - Comments: Single line comments start with `--`, and multi-line comments are enclosed within `{-` and `-}`. - Modules: Code is organized into modules, which can be imported using the `import` keyword. ** Basic Concepts - Functions: Functions are first-class citizens in Haskell. A function definition looks like this: #+BEGIN_SRC haskell add :: Int -> Int -> Int add x y = x + y #+END_SRC This defines a function *add* that takes two integers and returns their sum. - Types and Type Classes: Types are a crucial part of Haskell. You define types using the `data` keyword, and type classes using the `class` keyword. ** Example Here's a simple example to demonstrate some basic features of Haskell: #+BEGIN_SRC haskell -- Define a new data type data Shape = Circle Float | Rectangle Float Float -- Define a function to calculate the area of a shape area :: Shape -> Float area (Circle r) = pi * r^2 area (Rectangle l w) = l * w -- Example usage main = do let c = Circle 10 let r = Rectangle 5 7 print (area c) print (area r) #+END_SRC This example defines a new data type Shape with two constructors, Circle and Rectangle, and a function area that calculates the area of a shape.