Tuesday, January 13, 2009

Learning Haskell -1

I started learning Haskell as part of "learning a new programming language every year". Here's the frist post:
Am using Real World Haskell, first ed, as that's whats available on Safari books.

Notes:
  • Installed Hugs, then ghci cos the book uses that, and there're sufficient differences in the output of the interpreters for me to not bother about it.
  • Have started reading about types.
  • They are strong, static and inferred.
  • Type names are Capitalized
  • ints are signed, fixed width integers dependent on the machine word size.
  • Integers are signed integers of unbound size
  • Add :: TypeName after a declaration to set the type. Mostly not required
  • function args are written after the name without ()'s or ,'s. Eg fn arg1 arg2
  • lists are written as [item,item,..]
  • head and tail work similar to lisp car cdr.
  • Strings are character lists. So tail "abcd" returns "bcd"
  • Tuple is a collection of items of different types. Witten as (itemofType1, itemofType2, ..)
  • () is the type and value of a tuple of zero elements, called unit.
  • take, drop work on lists
  • fst, snd work on tuples. due to strict typing, we cannot define a fst fn for all kinds of tuples, only pairs.
  • Side effect explained: If a function is dependent on a global variable, each run of the function will differ based on the current value of that variable, which alters the way it executes. And therefore, even if it doesnt ever change a value, it has a side effect.
  • Pure functions dont have side effects. That is, they're idempotent - every execution of such functions return the same value. Kinda like GET.
  • Impure functions have side effects. They are identified by the Monad tag before their type. Eg:Prelude> :type readFile
    readFile :: FilePath -> IO String
Next week:

2.9. Haskell Source Files, and Writing Simple Functions

No comments: