Skip to content

libby/haskell_scheme_48

Repository files navigation

Playing around with some Haskell 🐭

Notes where I got confused

data type of ParsecT

data ParsecT s u m a 

so here 's u m a' are any types, like generics in Scala/Java 🍱

trait Parsec[S,U,M,A]
(<|>) :: Monad m => ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a

Here you are constraining m to the type Monad. It helps to read the types from right to left:

<|> goesParsecT takes type parameters s u m a,

Functions in Haskell are automatically curried, i.e if a funct

 val addTwoNums: Int Int => Int = (x, y) => x + y

Enter Haskell REPL ghci

 Prelude> let addTwoNums x y = x + y :: Integer -> Integer 	
 Prelude> :t addTwoNums
 addTwoNums :: Num a => a -> a -> a
 Prelude> let incNum = addTwoNums 1
 Prelude> :t incNum
 incNum :: Integer -> Integer
 Prelude> addTwoNums 3 3
 6
 Prelude> incNum 3
 4

In Haskell, like in Scala, declaring the type is optional

 addTwoNums :: Int -> Int  //optionally declare the type when not inside an .hs file
 addTwoNums x y = x + y

Ways to the monad and monad and monad... 🙀

Combining monads

  1. the do sugar, mmmm 🍧

// a stream seems to be passed implicitly into this 'monad'

do 
  x <- many (noneOf "\"") 
return x

desugars to ... -- many (noneOf "") >>= \x -> ...

  1. >> for chaining without passing along the internal value, will short circuit when one monad in the chain fails.
  monadA >> monadB >> monadC // if one fails, then the chain stops and returns the `failed` monad representation.
  1. >>= for passing the internal value on one monad to the next monad in the chain, like flatMap in Scala.
  monadA >>= \a -> (nextMonad a) >>= \b -> (nextMonad b) -> combineThemAll a b c
  1. The $ is like () and is used for passing arguments to a function it has the lowest precedence and is right associative.
   Prelude> incNum $ 3

// a where ParsecT is a type with a constrain on 'a' that it must be a Char see: data ParsecT s u m a
letter :: Stream s m Char => ParsecT s u m Char

Haskell allows you to make these "anonymous types"

instance Functor (ParsecT s u m) where fmap f p = parsecMap f p are instances like scala implicit type classes? and like that! Found the perfect blog post "Typeclasses define behaviour for a particular type and typeclass instances implement that behaviour. In this sense, typeclasses are interfaces and typeclass instances are implementations of those interfaces. You do not instantiate the implementations, the compiler does, and it does so by matching the types"

Useful things to learn quick

  • cabal is the the package manager for Haskel
  • Hoogle
  • System.io.unsafeIO!
  • debug, trace.
  • data types, instance (implicit TC scala?)
  • monadic chaining:
	 do
	   x <- myMonad
	 return x

syntatic sugar...desugars to >>= like Scala >>= when you need to pass along the value >> when you don't need to pass along a value


Fun facts

  • Haskell (Glasgow '90s) hence ghc [Glasgow Haskell compiler] vs ML (University of Edinburgh 1973 Robert Milner )

About

Write yourself a Scheme in 48 hours

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published