hiddenwaffle / haskellisms

Notes on Haskell

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Haskellisms

Notes from Haskell Programming from first principles

Table of Contents: Click on the hamburger menu in the upper-right corner of the GitHub display of this file.

Chapter 2 Basic expressions and functions

Start the REPL

stack ghci

Load a file

:load test.hs

-- Reload the same file
:reload
:r

Infix vs Prefix

10 `div` 4
div 10 4

100 + 100
(+) 100 100

Get info about a function

:info div
:info (+)

Variable declaration order does not matter

module Learn where

x = 10 * 5 + y
myResult = x * 5
y = 10

Some arithemtic functions: +, -, *, /, div and quot, mod and rem

div 20 (-6)
-- -4
quot 20 (-6)
-- -3

mod (-9) 7
-- 5
rem (-9) 7
-- -2
ghci>

Negative numbers are actually syntactic sugar for handled by a function

2000 + (-1234)
2000 + (negate 1234)

$ evaluates everything to its right first.

Use $ to omit parentheses in some cases

(2^) (2+2)
(2^) $ 2+2

Creating a partially applied function with an infix operator, a.k.a "sectioning"

(1/) 2
-- 0.5
(/1) 2
-- 2.0

x = 5
y = (1 -)
y x
-- -4
y = (subtract 1) -- because GHCi would interpret (- 1) as negation instead of subtraction
y x
-- 4
  • Local bindings
    • let...in is an expression
    • where is a declaration (i.e., bound to a surrounding construct)
-- with let
printInc n = let plusTwo = n + 2
             in print plusTwo

-- with where
printInc n = print plusTwo
  where plusTwo = n + 2

Chapter 3 Simple operations with text

:type 'a'
-- 'a' :: Char
--     ^^---- :: means "has the type", i.e., the type signature
:info Char
-- type Char :: *
-- data Char = GHC.Types.C# GHC.Prim.Char#
-- ...

:type "Hello!"
-- "Hello!" :: String
:info String
-- type String :: *
-- type String = [Char]
-- ...

Printing the the screen

print "hi"
-- "hi"
print 'h'
-- 'h'
putStrLn "hi"
-- hi
putStr "hi"
-- hi (without a newline)

Printing from a file requires some setup.

main :: IO()
main = do
  putStrLn "hello world"
  putStrLn "hello there"
  • main is a series of instructions to execute (not a function itself)
  • IO() is a special type that wraps the output of the module so that it can produce a side-effect
    • When using the REPL, GHCi implies this by default
  • do allows for then sequencing of actions that presumably produce side-effects

String concatenation

hello :: String
hello = "hello"

world :: String
world = "world"

main :: IO()
main = do
  putStrLn myGreeting1
  putStrLn myGreeting2
    where myGreeting1 = concat [hello, " ", world, " 1"]
          myGreeting2 = hello ++ " " ++ world ++ " 2"

About

Notes on Haskell

License:MIT License


Languages

Language:Haskell 100.0%