Well I decided to look into learning Haskell, since I found myself growing more and more interested about functional programming instead of imperative programming.  So I finally found a tutorial that made sense to me and went slow enough, so I'm happy Smile.  The thing is, it doesn't explain everything pitch-perfect, or it doesn't explain certain entities until much, much, much later in the tutorial along with complicated stuff I don't understand yet, and basically tells you to "just go with the flow, don't bother understanding them now".  I'm not that type of learner, if I learn a new concept I HAVE to know everything about how it's used, how it works, how it's useful, etc.  And in that one respect the tutorial leaves me craving for a knowledge supplement.

I have a few questions about some apparently simple features of Haskell (these questions are originated from the last section of a chapter called "Haskell Basics", which comes before "Elementary Haskell" and "Intermediate Haskell", so I consequently feel dumb for not understanding them immediately Sad).  Just so that I'm on the same page as everyone so no one needs to do additional explaining, here IS what I know:

- what functions are
  - how parameters work
    - currying
  - recursion (at least, simple forms of it)
  - function typing
    - abstract types
    - simple pattern matching with things like tuples
    - functions as first class values
      - higher order functions
- variables
  - apparently immutable
  - simply stand-ins for equations, not same as imperative variables
  - scoping within functions and function definitions

Here's a few questions:

What is an Action compared to a function?  It seems as though they actually perform imperative actions like IO or the like, but still are treated mostly like functions, except can pass values to variables with the <- operator.  What makes them special that they have their own operator to do this?  what are do blocks exactly? all I know is that they combine Actions and end with expressions.

How can you treat variables as mutable?  Is it true that there is no way to use mutable values in Haskell?  In Tari's Haskell implementation of Merthese as an interpreter, he seemed to pass a value to a variable via <- from an action.  Though he used recursion.  Can someone explain why he was able to seemingly mutably affect the variable?  Is it because he was affecting a variable local to that call of the function, and was redefined locally on the next recursive call?

Thanks in advance.
I haven't done much studying of Haskell myself, but I'll do what I can here. I've found LYHGG and Real-World Haskell to be good resources.

Ashbad wrote:
What is an Action compared to a function?  It seems as though they actually perform imperative actions like IO or the like, but still are treated mostly like functions, except can pass values to variables with the <- operator.  What makes them special that they have their own operator to do this?  what are do blocks exactly? all I know is that they combine Actions and end with expressions.

My understanding is that do {...} blocks enforce ordering of operations, which you need to do when operations have side-effects (eg, I/O of any kind). You'll notice that most anything in the Prelude that does I/O reduces to something that includes 'IO', like getChar :: IO Char.
I'm not entirely clear on monads, but monads are behind that- the 'IO' component of that type is an IO monad which yields a character. The trick is that you can't directly evaluate monads, only chain them. That's what the <- operator is doing. See Real-World Haskell chapter 7 for an intro to monads in I/O and chapter 15 for the guts.

Ashbad wrote:
How can you treat variables as mutable?  Is it true that there is no way to use mutable values in Haskell?  In Tari's Haskell implementation of Merthese as an interpreter, he seemed to pass a value to a variable via <- from an action.  Though he used recursion.  Can someone explain why he was able to seemingly mutably affect the variable?  Is it because he was affecting a variable local to that call of the function, and was redefined locally on the next recursive call?

I wasn't changing the value of anything within the scope of the function. I got a random value and prepended that to the result of a recursive call to form a list ([Char] in that case, a String), returning that.
My understanding is that, being a purely functional language, everything is immutable. Considering your favorite mathematical formula, it would suddenly stop making sense if you were to redefine, say, x half-way through. Everything that changes is the result of reduction to base cases and putting things together into return values of functions.

In many cases the compiler can figure out your recursion and convert it to a tail-recursive function on the machine, avoiding the potentially huge overhead (stack space and accumulating huge thunks) of actual recursion, but sometimes it won't. It's a good case for using standard functions (esp. foldr, etc) when they make sense in recursive operations. The wiki has a good explanation of ways to ensure your recursive functions are actually tail-recursive.

Note once again that my grasp of Haskell is far from complete, but I tried to offer useful explanations. Refer to the resources I linked at the top of this post for more definitive answers.
thanks for the links and the somewhat good description if the concepts Smile I still don't fully understand them, but that "Real World Haskell" seems as if it'll be very helpful to me, much better then the other tutorial.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement