General Advice Needed ..

Hi, First of all, sorry if its in the wrong section.. But I'm just having trouble getting to grips with Haskell. I have my functional programming exam tommorow and I'm struggling to understand any of this. We worked through the book The Craft Of Functional Programming and Im trying to work my way through it but still no luck.. I find it really hard to put down code onto paper. I can understand the code in the book I can do the simple simple functions like cube.. cube :: Int -> Int cube n = n*n*n But after that im lost :( Is there any general advice? Just keep reading the book till it drills into my big head? You can make sarcastic n00b jokes if you like.. I'm just a very frustrated student right now! -- View this message in context: http://old.nabble.com/General-Advice-Needed-..-tp27161410p27161410.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

But after that im lost :(
Is there any general advice? Just keep reading the book till it drills into my big head?
Is it that you're having difficulty knowing how you'd solve certain classes of problems using Haskell? You're stuck in an imperative rut? The O'Reilly book "Real World Haskell" is very good for this, because as the name implies, it uses Haskell to solve actual engineering problems, rather than approach it from the theoretical angle. Martin

Pretty much yeah.. Im going through the book and things like : Define a function rangeProduct which when given natural numbers m and n, returns the product m*(m+1)*....*(n-1)*n I got the solution from my lecture notes but I still dont understand it.. rangeProduct :: Int -> Int -> Int rangeProduct m n | m > n = 0 | m == n = m | otherwise = m * rangeProduct (m+1) n Totally lost! Haha.. But thanks for the book suggestion, My exam is tommorow so I'm hoping theres an online version of this book that I can read through! And maybe by some divine miracle I'll understand it :-) Martin Coxall-2 wrote:
But after that im lost :(
Is there any general advice? Just keep reading the book till it drills into my big head?
Is it that you're having difficulty knowing how you'd solve certain classes of problems using Haskell? You're stuck in an imperative rut?
The O'Reilly book "Real World Haskell" is very good for this, because as the name implies, it uses Haskell to solve actual engineering problems, rather than approach it from the theoretical angle.
Martin _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://old.nabble.com/General-Advice-Needed-..-tp27161410p27162216.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

On Thu, 14 Jan 2010 15:38:26 +0100, Ian675
Pretty much yeah.. Im going through the book and things like :
Define a function rangeProduct which when given natural numbers m and n, returns the product m*(m+1)*....*(n-1)*n
I got the solution from my lecture notes but I still dont understand it..
rangeProduct :: Int -> Int -> Int rangeProduct m n | m > n = 0 | m == n = m | otherwise = m * rangeProduct (m+1) n
I'll try to give a clear explanation of this function:
rangeProduct :: Int -> Int -> Int rangeProduct m n A function is defined with parameters m and n, both Int; the result of the function is also an Int
| m > n = 0
If m > n, the result is 0; the rest of the function definition will be skipped
| m == n = m
If m is not larger then n, evalution continues here; if m == n, the result of the function is m
| otherwise = m * rangeProduct (m+1) n
If previous predicates were False, this branch is evaluated ("otherwise" is always True); the function calls itself with (m+1) as first parameter The boolean expressions in this function are called "guards"; the right hand side after the first guard that evaluates to True, will give the result of the function. Regards, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html --

thankyou.. that made more sense to me :) What im doing now is.. Im still working through the "Craft of Functional Programming" book but I've found a site that has solutions to some of the excercise questions. So i'm noting them down and trying to make sense of them Is that a good approach? Henk-Jan van Tuyl wrote:
On Thu, 14 Jan 2010 15:38:26 +0100, Ian675
wrote: Pretty much yeah.. Im going through the book and things like :
Define a function rangeProduct which when given natural numbers m and n, returns the product m*(m+1)*....*(n-1)*n
I got the solution from my lecture notes but I still dont understand it..
rangeProduct :: Int -> Int -> Int rangeProduct m n | m > n = 0 | m == n = m | otherwise = m * rangeProduct (m+1) n
I'll try to give a clear explanation of this function:
rangeProduct :: Int -> Int -> Int rangeProduct m n A function is defined with parameters m and n, both Int; the result of the function is also an Int
| m > n = 0
If m > n, the result is 0; the rest of the function definition will be skipped
| m == n = m
If m is not larger then n, evalution continues here; if m == n, the result of the function is m
| otherwise = m * rangeProduct (m+1) n
If previous predicates were False, this branch is evaluated ("otherwise" is always True); the function calls itself with (m+1) as first parameter
The boolean expressions in this function are called "guards"; the right hand side after the first guard that evaluates to True, will give the result of the function.
Regards, Henk-Jan van Tuyl
-- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html -- _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://old.nabble.com/General-Advice-Needed-..-tp27161410p27164433.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

Yes. An approach that I have always used that has worked well for me is to keep a list of "tricks" while I am studying. Whenever I get stuck on a practice problem but eventually figure it out (either by simply thinking harder, looking it up, or asking someone for help), I try to identify the missing link that had prevented me from seeing how to do it immediately, and then write it down on my "tricks" list so that I know that I need to keep that trick in mind while I am taking the test. Cheers, Greg On Jan 14, 2010, at 8:53 AM, Ian675 wrote:
thankyou.. that made more sense to me :)
What im doing now is.. Im still working through the "Craft of Functional Programming" book but I've found a site that has solutions to some of the excercise questions. So i'm noting them down and trying to make sense of them
Is that a good approach?
Henk-Jan van Tuyl wrote:
On Thu, 14 Jan 2010 15:38:26 +0100, Ian675
wrote: Pretty much yeah.. Im going through the book and things like :
Define a function rangeProduct which when given natural numbers m and n, returns the product m*(m+1)*....*(n-1)*n
I got the solution from my lecture notes but I still dont understand it..
rangeProduct :: Int -> Int -> Int rangeProduct m n | m > n = 0 | m == n = m | otherwise = m * rangeProduct (m+1) n
I'll try to give a clear explanation of this function:
rangeProduct :: Int -> Int -> Int rangeProduct m n A function is defined with parameters m and n, both Int; the result of the function is also an Int
| m > n = 0
If m > n, the result is 0; the rest of the function definition will be skipped
| m == n = m
If m is not larger then n, evalution continues here; if m == n, the result of the function is m
| otherwise = m * rangeProduct (m+1) n
If previous predicates were False, this branch is evaluated ("otherwise" is always True); the function calls itself with (m+1) as first parameter
The boolean expressions in this function are called "guards"; the right hand side after the first guard that evaluates to True, will give the result of the function.
Regards, Henk-Jan van Tuyl
-- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html -- _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://old.nabble.com/General-Advice-Needed-..-tp27161410p27164433.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hello Does you find this version easier to understand? rangeProduct :: Int -> Int -> Int rangeProduct m n = if m > n then 0 else if m == n then m else m * rangeProduct (m+1) n I would suspect the main point of the example is to make you think recursively - note how rangeProduct is called from within its own definition (on the last line of code). Before it, there are two stopping conditions that will halt recursion "if m > n " or "if m == n" - stopping conditions are crucial for recursive thinking and programming - without them you will recur endlessly and never produce an answer. If you exam is 'on paper' - that's to say code you can have some syntax errors because it isn't actually run - you want to be demonstrating that you can think recursively. Good luck Stephen

On Jan 15, 2010, at 3:38 AM, Ian675 wrote:
Pretty much yeah.. Im going through the book and things like :
Define a function rangeProduct which when given natural numbers m and n, returns the product m*(m+1)*....*(n-1)*n
Case analysis and recursion. If m > n, the answer is 1 (the product of an empty sequence is 1). If m <= n, the answer is m * the product of m+1 .. n. range_product m n = if m > n then 1 else m * range_product (m+1) n Try it in C: int range_product(int m, int n) { return m > n ? 1 : m * range_product(m+1, n); } Same thing. Of course the most direct way to express it in Haskell is range_product m n = product [m..n]
I got the solution from my lecture notes but I still dont understand it..
rangeProduct :: Int -> Int -> Int rangeProduct m n | m > n = 0 | m == n = m | otherwise = m * rangeProduct (m+1) n
Totally lost! Haha..
One reason you're totally lost is that the code in the book is WRONG. The product of an empty sequence is 1, not 0. There are two basic ways of "thinking functionally", and they are actually both important in practically every kind of programming. (1) programming-by-combination: look for existing functions that are "close" to what you want and plug them together. In this case, looking for "product of a sequence of numbers" leads to 'product' and looking for "sequence of consecutive numbers" leads to [ .. ] syntax, and plugging them together leads to the product [m..n] version. (2) programming-by-case-analysis (including recursion): look for a way of classifying the problem into alternative cases, at least some of which are obviously simpler to solve. Recursion is just the special case where some of the alternatives can be handled using the function you are trying to define. By the way, the problem here is ambiguous. I look at m * (m+1) * ... * (n-1) * n and think "n to the falling n-m+1" (see "Concrete Mathematics" by Graham, Knuth, and Patashnik) and this is actually defined (and not identically 1) when m > n. So there are at least two reasonable definitions of what the function should do for m > n. (Returning 0 is NOT reasonable.)

Hi, it may be a bit too late for you, but in general working through Smullyan's "To Mock a Mockingbird" (http://en.wikipedia.org/wiki/To_Mock_a_Mockingbird) may help in coming to grips with some of the theory (and intuition) behind functional programming. The "Real World Haskell" book is also a good choice, completely orthogonal to Smullyan's book, and much more practical. Matthias.

It may be a bit late but I'll try anything Thankyou, I'll have a read :-) Matthias Görgens-2 wrote:
Hi,
it may be a bit too late for you, but in general working through Smullyan's "To Mock a Mockingbird" (http://en.wikipedia.org/wiki/To_Mock_a_Mockingbird) may help in coming to grips with some of the theory (and intuition) behind functional programming.
The "Real World Haskell" book is also a good choice, completely orthogonal to Smullyan's book, and much more practical.
Matthias. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://old.nabble.com/General-Advice-Needed-..-tp27161410p27162253.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

On Thu, Jan 14, 2010 at 7:52 AM, Ian675
Is there any general advice? Just keep reading the book till it drills into my big head?
Also don't be afraid to ask specific questions on the Beginners mailing list; while Cafe is a good general resource, Beginners is specifically there for those still learning the language (like me). :-)
participants (8)
-
Gregory Crosswhite
-
Henk-Jan van Tuyl
-
Ian675
-
Martin Coxall
-
Matthias Görgens
-
Richard O'Keefe
-
Stephen Tetley
-
Tom Tobin