Skip to content

maxlibin/haskell-101

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

Haskell 101

What is haskell

Haskell is a strong statically typed, purely functional, lazily evaludated, general purpose.

What is purely functional?

Everything is a function, everything is immutable, everything is an expression, no side effects.

**Side effects are in IO

What does it mean lazily evaludated

Deffered expression evaluation

Not used and is not computed

Cons:

  • Memory pitfall
  • IO and Parallelism pitfall

Pro:

  • Huge optimizations
  • Great exoressivity

Reading of function types

f::Int->Int->[Int]

functions are auto curried, each fn only takes one argument carry to next fn and takes only another one argument and so on until it returns something.

Example: map:: (a -> b) -> [a] -> [b] you can write like Haskell can create your own operator:

($) :: (a -> b) -> a -> b

This operator takes create a function, takes in ````a and return b

this is the same as creating a function for example

let example = fun (x + y)

you can write like

let example = fun $ x + y

another popular operator which is dot (.)

(.):: (b -> c) -> (a -> b) -> (a -> c)

Type alias:

type Point = (Int, Int) -- tuple

type Point = [Point] -- list

type Map k v = [(k v)] --type parameter

User data to create a new dataType and use type to alias a type

You can also declare struct inside a dataType

data User = User { userName:: String, userAge:: Int }

How to write functions in haskell, first give a type of the function then function: example

not:: Bool -> Bool -- Declare a function of not signature where takes in a Bool and returns a Bool
not True = False
not False = True -- Use pattern matching for simplify the expression

In pattern matching you can use _ as whatever this is, pattern matching can be use for deconstruction

Some good place to start haskell:

List in Haskell is represent by (x:xs), it can be [] which is empty list. with (x:xs) which means list with head and list of xs

Getting started

To get started with Haskell tools, easist way is to install from "https://www.haskell.org/ghcup/" and load up with ghcup tui to see all tools you have installed.

Create a file hello.hs

add:

main = putStrLn "hello world"

save and compile with ghc -o hello hello.hs

run the program with ./hello

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published