Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Haskell-tut]Haskell Basics
Index -> Programming, General Programming -> Functional Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Thu Nov 18, 2004 1:17 am   Post subject: [Haskell-tut]Haskell Basics

What is Haskell?

Haskell is an inferred statically-typed, lazily evaluated, purely functional programming language.

What do these things mean?

  • Inferred typing means that the compiler or interpreter can figure out what type a piece of data is without an explicit notation. Consider that in Turing we'd write a "increment" function as:

    code:
    function inc (n : int) : int
       result n + 1
    end inc


    Notice that twice I had to specify types. First I specified the argument "n" as an int, then I had to specify that the "inc" function returns an int.

    Now, in Haskell we'd write:

    code:
    inc n = n + 1


    Notice that we did no such thing. The interpreter or compiler in use figures out that "n" must be on a type that allows addition.

  • Static typing means that the compiler or interpreter knows the type of every function and variable at compile time, before the program ever runs. This doesn't eliminate all possible errors, but it eliminates a large category of them.

  • Lazy evaluation is a peculiar thing, especially for many programmers used to more "conventional" languages. Haskell compilers and interpreters don't actually evaluate anything until it's absolutely necessary.

    One of the best examples of this is:

    code:
    foo = 1 / 0
    bar = "hello"


    Compiling this should yield an error, right? After all, dividing 1 by zero is strictly verboten in the world of math and programming. It is in Haskell as well. The catch is that since I never use "foo", "1 / 0" never gets evaluated, and never causes an error.

  • Functional? What good is that?

    Functions in Haskell are first-order things. You can freely manipulate them. Perhaps the most useful example of this is partial application. Partial application is a wonderful idea embodied by many functional languages. Consider our previous example of "inc":

    code:
    inc n = n + 1


    Instead, we could more succinctly write:

    code:
    inc = (+ 1)


    "+" is a function which takes two numbers and adds them. Since we only gave it one argument (1), what we got back was another function which takes one argument and adds 1 to it.


How does someone get started?

Well, you'll need:

  • A good text editor.
  • Either Hugs, or the Glasgow Haskell Compiler. For learning, Hugs is probably sufficient. It's available for download at the Haskell website, under Compilers and Interpreters.


Now, Hugs takes a bit of getting used to. Unlike interpreters for languages like Ruby, Python, and O'Caml, you can't define anything new at the prompt. What you can do is load in source files and test functions.

Consider our "inc" function in a source file "Main.hs". To run it, we launch Hugs, then enter ":load Main".

code:
prelude> :load Main
Main>


The new prompt indicates we're in a new module. Now, to run the inc function:

code:
prelude> :load Main
Main> inc 1
2
Main>


Stuff that should look familiar

Haskell contains data types that should look quite familiar.


  • Int (Integer for big numbers)
  • Char
  • Float
  • String


These look like:

code:
foo = 42
bar = 'z'
baz = 2.43
qux = "Hello"


And we can do familiar operations on them:

code:
prelude> 2 + 2
4
prelude> 2 / 2
1
prelude> "hello " ++ "world"
"hello world"
prelude> 'h' : "ello"
"hello"
prelude> "worl" ++ 'd' : ""
"world"
prelude>


And that's it for now

Go, play with Hugs until the next tutorial. Smile
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, General Programming -> Functional Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: