Skip to content

Commit

Permalink
learning haskell
Browse files Browse the repository at this point in the history
  • Loading branch information
Rett Berg committed Sep 11, 2020
1 parent c9c429e commit da35b05
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
33 changes: 32 additions & 1 deletion haskell/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
Stack tutorial is definitely necessary to get me up and running:
https://github.com/Originate/guide/blob/master/haskell/stack-tutorial.md

Also `ghci` is the interactive interpreter.

- `:load <file>` to reload the file
- `:type <expr>` to get the type of an expression
- `:info <type>` to get info about a type

## Chapter 1: all you need is lambda
- A calculus is a "method of calculation or reasoning" -- not necessarily
related to Calculus which is the mathematics of infinitesmal differences.
Expand Down Expand Up @@ -56,7 +62,7 @@ ordinarily converge to a beta normal form, of which divergence is the opposite.
- Example: `(\x -> x x) (\x -> x x)`
- Terms that diverge simply don't produce an answer/result and are not useful.

Haskell is a typed lambda calculus with a log of surface-level decoration to
Haskell is a typed lambda calculus with a lot of surface-level decoration to
make it easier for humans to write/read. The meaning of Haskell programs
is centered around **evaluating expressions** rather than **executing
instructions**.
Expand Down Expand Up @@ -89,3 +95,28 @@ could for simplicity.
A **value** is simply _any_ irreducible expression.



## Chapter 3
The main things learned from this is some important list operations:

- `head` get the first value
- `tail` get values after the first value
- `last` get the last value
- `concat` and `++`: join lists
- `!!` get item at an index
- `length` get the list length
- `myFun x = x + 3` declare a function.
- `print` print to the screen in `main :: IO ()`

## Chapter 4

Declare a type with `data`.

- Sumtypes: `data Bool = True | False`
- `Word` is a non-negative `Int` (0 is lowest value)

Type classes are a way of adding functionality to types that is
reusable across all types which have instances of that type class.
`Num` is the typeclass for all of the number types including
`Int Integer Word Float Double Rational Fixed Scientific`

26 changes: 26 additions & 0 deletions haskell/ch3/Ch3.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,29 @@ ex2 =
, tail [7, 8, 9]
]
)

-- Oops! This was the reverse
-- prob1a = \x -> take ((length x) - 1) x

prob1a = \x -> x ++ "!"

prob1b = \x -> x !! 4

prob1c = \x -> drop ((length x) - 7) x

thirdLetter :: String -> Char
thirdLetter x = x !! 2

letterIndex :: Int -> Char
-- letterIndex = \i -> "Curry is awesome!" !! i
letterIndex i = "Curry is awesome!" !! i

-- last x = x !! length x - 1

rvrs :: String -> String
rvrs x = if length x <= 1
then x
else [last x] ++ (rvrs $ take (length x - 1) x)

main :: IO ()
main = print (rvrs "Curry is awesome")
3 changes: 3 additions & 0 deletions haskell/ch4/Ch4.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ data Mood =
changeMood :: Mood -> Mood
changeMood Blah = Woot
changeMood _ = Blah

-- Continue at section 4.6

0 comments on commit da35b05

Please sign in to comment.