Re: [Haskell-cafe] Learning C after Haskell
Chad Scherrer wrote: > My question is, as I learn C, are there any particular Haskell concepts I > should keep in the back of my mind, or is it better to approach C from > scratch? One thing from Haskell I'd try keep in mind is to minimize side effects and keep the scope of side effects as contained and local as possible. So avoid mutating global variables, try not to write to the same file from multiple different subroutines, etc. And if you start getting seg-faults, you'll probably want a tool to help you, since reasoning and debug by printf on pointers can only take you so far in a language like C. http://www.gnu.org/software/libc/manual/html_node/Allocation-Debugging.html http://perens.com/FreeSoftware/ElectricFence/ http://valgrind.org/ Greg Buchholz ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Learning C after Haskell
On 6/12/06, Chad Scherrer <[EMAIL PROTECTED]> wrote: Ok, so I'm doing things somewhat backward. I've been using Haskell for a while now, whenever I get a chance to. But in order to become more involved in high-performance computing projects at my work, I need to learn C. [snip] My question is, as I learn C, are there any particular Haskell concepts I should keep in the back of my mind, or is it better to approach C from scratch? Sounds like you would be an almost ideal user for Jekyll. Too bad it's only in the alpha stages: http://jekyllc.sourceforge.net/ You might find it useful to study how Haskell's familiar product and sum types would be represented in unfamiliar C. If I recall correctly you can find examples of the translation in the Jekyll presentations/documentation. So even if you never used it, it would be a worthwhile read. Good luck, Jason ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Learning C after Haskell
Thanks, Minh. So are things like recursion and memory sharing typically out the window? Also, I don't see how thinking about type classes will help, without the benefits of polymorphism. -Chad-- Forwarded message --From: minh thu <[EMAIL PROTECTED]>Date: Jun 12, 2006 12:23 PM Subject: Re: [Haskell-cafe] Learning C after HaskellTo: Chad Scherrer <[EMAIL PROTECTED]>hi,C is very different from Haskell.* you'lle have to manage explicitly memory de/allocation. * c programming is a bit like haskell io monad programming (butwithout the functionnal part) : you'lle use "=" in place of "<-" and the left hand side can bereassigned multiple times. * imperative programming (c is imperative) involve states (a lot !). (see how to write the equivalent of mapM print [1..10]); imperative programmers use loops, not much recursion* things you can keep in your mind : the way you organize things (module, type classes (but there is no polymorphism in c), havefunctions and not a huge amount of code lines...), the way you useself documenting names.well, not sure it helps, but here you are :)mt 2006/6/12, Chad Scherrer <[EMAIL PROTECTED]>:> Ok, so I'm doing things somewhat backward. I've been using Haskell for a> while now, whenever I get a chance to. But in order to become more involved > in high-performance computing projects at my work, I need to learn C.>> I've heard a lot of people say that experience in Haskell can improve one's> abilities in other languages, but I also wonder how different the C "way of > doing things" is different from Haskell's.>> My question is, as I learn C, are there any particular Haskell concepts I> should keep in the back of my mind, or is it better to approach C from > scratch?>> Thanks in advance!>> Preparing for a foot-shooting,> Chad>> ___> Haskell-Cafe mailing list> Haskell-Cafe@haskell.org> http://www.haskell.org/mailman/listinfo/haskell-cafe>>>-- Chad Scherrer "Time flies like an arrow; fruit flies like a banana" -- Groucho Marx ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Learning C after Haskell
Thanks, Minh. So are things like recursion and memory sharing typically out the window? Recursion works in C, but every function call pushes stack, so recursive depth is limited by RAM (compare to tail call optimization in many functional programming languages where the stack frame is reused if code is written iteratively with an accumulator). Most of the time for performance (and to be idiomatic) in C you will just write for and while loops and modify state in place. Welcom to C. Jared. -- http://www.updike.org/~jared/ reverse ")-:" ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Learning C after Haskell
On Mon, 2006-06-12 at 14:48 -0700, Jared Updike wrote: > > Thanks, Minh. So are things like recursion and memory sharing typically out > > the window? > > Recursion works in C, but every function call pushes stack, so > recursive depth is limited by RAM (compare to tail call optimization > in many functional programming languages where the stack frame is > reused if code is written iteratively with an accumulator). Most of > the time for performance (and to be idiomatic) in C you will just > write for and while loops and modify state in place. Welcom to C. GCC can do a limited tail-call optimisation these days. It covers the obvious self tail call case and I believe some less trivial cases but it's not as comprehensive as in a typical functional language compiler. Duncan ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Learning C after Haskell
I did the transition the other way, and even now the real-world keeps me using more C-like languages (Java, Python). Unlike the transition from imperative languages to Haskell, I don't think there's much you have to unlearn or rethink. But I suspect you may feel a degree of frustration at how incredibly primitive C may seem after learning to use the power that Haskell makes available. Where you can map Haskell idioms into C, I think they may serve you well (I've found this to be the case with Python, but the gap from Haskell to C is somewhat greater). You mention "high performance" computing. I think there are two ways of achieving this: the C way might be described as "micro optimization" -- getting the small but important things to run as efficiently as possible The Haskell was is more a case of "macro optimization" -- getting the overall algorithmic approach to be as smart as possible. There's a place for both, but once you get involved in micro-optimization it can be very difficult to go back and fix the macro performance issues. So you might do well to first code algorithms in Haskell first, and experiment with the algorithms, if only as a way of specifying the solution to be implemented in C. #g -- Chad Scherrer wrote: > Ok, so I'm doing things somewhat backward. I've been using Haskell for a > while now, whenever I get a chance to. But in order to become more > involved in high-performance computing projects at my work, I need to > learn C. > > I've heard a lot of people say that experience in Haskell can improve > one's abilities in other languages, but I also wonder how different the > C "way of doing things" is different from Haskell's. > > My question is, as I learn C, are there any particular Haskell concepts > I should keep in the back of my mind, or is it better to approach C from > scratch? > > Thanks in advance! > > Preparing for a foot-shooting, > Chad > > > > > ___ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe -- Graham Klyne For email: http://www.ninebynine.org/#Contact ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe