• I am trying to learn Haskell
    5 replies, posted
I'm more of an old Visual BASIC programmer than anything else but I've done C/C++ and others. Anyone else learning Haskell or know it to give me pointers? I've tried reading books on it, but they are not written really well. It is like they are an ESL writer and cannot form any sort of logic that makes sense. The haskell.org website Wiki is confusing as well. Learn you a Haskell and Haskell for C programmers and the rest are really poorly written, I just don't seem to get what they are saying. I feel like they are written by math geeks that don't know how to program. Any help?
I took a programming languages course this semester where we learned Haskell. I think it's a really neat language (though many of my classmates did not). I think one of the reasons for that is that it is a functional language rather than an imperative one - it focuses on function application rather than state change. You should look into function programming in general, and also look at some higher order functions such as map and fold that make Haskell and other functional languages really powerful. In all these code examples I use A => B to denote that A returns B Map: Takes a function and applies it to each element in a list, returning the new list [code]Note: (\x -> x*2) is a lambda function that takes a value x and returns x*2 map (\x -> x*2) [1,2,3] => [2,4,6][/code] Fold: Takes a function and applies it to the first 2 elements in a list, then applies it to that result and the 3rd element, that result and the 4th element, etc. There are a few variations of this (foldl and foldr, foldl1 and foldr1) [code]This does: (1 - 2) - 3 foldl1 (-) [1,2,3] => -4 This does: 1 - (2 - 3) foldr1 (-) [1,2,3] => 2[/code] Haskell also has some really powerful features like lazy evaluation (don't evaluate anything until you absolutely need to) and list comprehension (set notation-like syntax for defining lists) [code]Infinite list: [1,1,1,...] listOfOnes = 1 : listOfOnes Read this as "All of (x,y), such that x belongs to (or is an element of) [1,3,5] and y belongs to [2,4,6]" [(x,y) | x <- [1,3,5], y <- [2,4,6]] => [(1,2), (1,4), (1,6), (3,2), (3,4), (3,6), (5,2), (5,4), (5,6)][/code] Also take a look at currying, which lets you pass fewer arguments to a function and have it return you another function. [code]Note: You only pass 1 argument (2) to +, so the result is a function map (+2) [1,2,3] => [3,4,5][/code] If you haven't already I suggest downloading GHCi, an interactive Haskell environment. Also, type inferencing is your friend! In GHCI, use :t ___ to get the type of something. The syntax may be a bit confusing at first (understanding currying helps): [code]Note: + is in parenthesis to make it prefix: (+) 2 3 instead of 2 + 3 Read as "for some 'a' of type Num, (+) takes an 'a' and returns a function that takes an 'a' and returns an 'a'" :t (+) => (+) :: Num a => a -> a -> a :t (+2) => (+2) :: Num a => a -> a :t (1+2) => (1+2) :: Num a => a[/code] I think I got a bit carried away with this, and I may have been too complicated for someone new to Haskell. Let me know if you need any more help.
[QUOTE=Orion Blastar;44972141]I'm more of an old Visual BASIC programmer than anything else but I've done C/C++ and others. Anyone else learning Haskell or know it to give me pointers? I've tried reading books on it, but they are not written really well. It is like they are an ESL writer and cannot form any sort of logic that makes sense. The haskell.org website Wiki is confusing as well. Learn you a Haskell and Haskell for C programmers and the rest are really poorly written, I just don't seem to get what they are saying. I feel like they are written by math geeks that don't know how to program. Any help?[/QUOTE] Learn you a Haskell is generally considered very nice. Are you sure the problem is with the book?
I've had some problems with finding any practical usage tutorials since reading a book and just learning syntax and stuff just doesn't work for me. There's this page on the wiki [url]http://www.haskell.org/haskellwiki/Tutorials#Practical_Haskell[/url], but the only real beginner thing there is the irc bot (which is p cool btw), the rest actually is only really practical on a really low level. Does anyone have any other recommendations on tutorials that actually show you how to do some real things?
I'm the same, learning from practical examples rather than going through syntax is easier for me.
The thing that made me "get" haskell was that instead of controlling the state of the data set that your program is performing operations on, functional programming defines declaratively that govern how state flows throughout the program. As for real world/practical examples I found [URL="http://book.realworldhaskell.org/read/"]Real World Haskell[/URL] extremely useful.
Sorry, you need to Log In to post a reply to this thread.