Files
org_roam/20241212013207-haskell_notes.org
2026-01-24 18:28:53 +00:00

2.3 KiB
Raw Blame History

Haskell Notes

Haskell Notes

Introduction

Haskell is a purely functional programming language with strong static typing and 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 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:

    add :: Int -> Int -> Int
    add x y = x + y

    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:

-- 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)

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.