Skip to content

Instantly share code, notes, and snippets.

@dino-
Last active March 20, 2024 05:38
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dino-/62afaafbb2c5e7aaf6fd8d151be6b149 to your computer and use it in GitHub Desktop.
Save dino-/62afaafbb2c5e7aaf6fd8d151be6b149 to your computer and use it in GitHub Desktop.
Haskell symbols

Haskell symbols

When starting out with Haskell, I found it difficult to read a lot of the symbols. I made this document explaining the names of some symbols and how I read them.

:: "has type"

x :: Int

x has type Int

-> "to"

Int -> String

Int to String

The type Int -> String takes an Int as an argument and evaluates to a String. We say this type is "Int to String".

Putting :: and -> together

foo :: a -> b -> c

foo has type a to b to c

=> "for all"

This symbol is for declaring type class constraints. A type class lets us define common methods across types. It limits a type.

foo :: (Bar a) => a -> b

for all a of type class Bar, foo has type a to b

[a] "list of a"

: "cons"

List cons operator. Adds the first argument to the front of the second. In module Data.List. I believe the name comes from Lisp, for "constructing" or "construction"

(:) :: a -> [a] -> [a]
1 : [2, 3] == [1, 2, 3]

!! "element at"

List index operator

(!!) :: [a] -> Int -> a
[1, 2, 3] !! 1 == 2

Element in the list [1, 2, 3] at index 1

<- "drawn from"

Used in list comprehensions (and called a generator there) and monadic do notation

x <- something

x drawn from something

| "such that" in list comprehensions

Also used for function guards.

Putting | and <- together in a list comprehension:

[ x * x | x <- [1, 2, 3], x > 1 ]

A list of x multiplied by x such that x is drawn from the list [1, 2, 3], where x is greater than 1.

>>= "bind"

Monadic bind operator. Part of type class Monad.

(>>=) :: Monad m => m a -> (a -> m b) -> m b

>> "then"

Monadic sequence operator. Part of type class Monad.

(>>) :: Monad m => m a -> m b -> m b

x >> y

x then y

<$> "fmap"

An infix synonym for fmap

@kdchaires
Copy link

I believe, for this function !! you could read it as "element at" or something along the lines ...

@dino-
Copy link
Author

dino- commented Apr 28, 2021

I believe, for this function !! you could read it as "element at" or something along the lines ...

Thanks, I changed it to this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment