[Haskell-cafe] do we have something like isDefined or isNull in Haskell?
Suppose there is a data definition in Haskell: data MyType = MyType { date :: Double, weight :: Double, height:: Double } deriving (Eq, Ord, Show) Is it possible to check if the field height, for example, is filled in(defined)? Can we give default values in Haskell? Many thanks and sorry fro so sily questions. Vladimir _ The new MSN Search Toolbar now includes Desktop search! http://join.msn.com/toolbar/overview ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] do we have something like isDefined or isNull in Haskell?
On 6/15/06, Vladimir Portnykh [EMAIL PROTECTED] wrote: Suppose there is a data definition in Haskell: data MyType = MyType { date :: Double, weight :: Double, height:: Double } deriving (Eq, Ord, Show) Is it possible to check if the field height, for example, is filled in(defined)? Can we give default values in Haskell? Many thanks and sorry fro so sily questions. Vladimir You could make date, weight, and height Maybe Doubles, then isDefined = isJust and isNull = isNothing. Bryan ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] do we have something like isDefined or isNull in Haskell?
On Thu, 2006-06-15 at 12:43 +0100, Vladimir Portnykh wrote: Suppose there is a data definition in Haskell: data MyType = MyType { date :: Double, weight :: Double, height:: Double } deriving (Eq, Ord, Show) Is it possible to check if the field height, for example, is filled in(defined)? Can we give default values in Haskell? There is no implicit null, standard technique is to use an explicit null using the Maybe type: data MyType = MyType { date :: Maybe Double, weight :: Maybe Double, height :: Maybe Double } deriving (Eq, Ord, Show) The Maybe type is defined like this: data Maybe a = Nothing | Just a so a value of type 'Maybe a' can be either Just x or Nothing. So you can represent your lack of a value using Nothing. You can get something similar to default values in a record by using the ordinary record update syntax. Suppose we start with a default record value: default :: MyType default = MyType { date = Nothing, weight = Nothing, height = Nothing } then you can construct your records using: foo = default { weight = 3.2 } so foo will get all the default values except for the ones you explicitly set. Duncan ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Rotating matrices
Dear list, I'm currently engaged in an attempt to wrap my head around type arithmetic. To make sure that I've understood, I plan to write a few operations on matrices a la Oleg's Number Parameterised Types. Before I get down to it, I've been making sure I know how to implement the operations (so that all I need to think about later is the type arithmetic). Today I've been looking at rotating matrices, i.e: taking a column- wise matrix and making it row-wise and, in the process, swapping the dimensions (thus a 3*2 matrix becomes a 2*3 matrix). I've been working with lists of lists for simplicity and have come up with: rotate' :: [[a]] - [[a]] rotate' [] = [] rotate' xs = (map (head) xs ):(rotate' $ filter (not . null) $ map (tail) xs) which seems to work just fine. While this solution is adequate (it seems to work for infinite structures as well, which is good), I originally set out to solve this problem with a fold or a mapAccum of some sort (I don't really care if it doesn't work with infinite structures). Can anyone suggest a way to write this with a fold (or tell me why it isn't a good idea)? Cheers, Thomas Sutton ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Rotating matrices
On Thu, 15 Jun 2006, Thomas Sutton wrote: Today I've been looking at rotating matrices, i.e: taking a column-wise matrix and making it row-wise and, in the process, swapping the dimensions (thus a 3*2 matrix becomes a 2*3 matrix). You mean 'matrix transposition' which is available as Data.List.transpose. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Rotating matrices
Thomas, rotate' :: [[a]] - [[a]] rotate' [] = [] rotate' xs = (map (head) xs ):(rotate' $ filter (not . null) $ map (tail) xs) which seems to work just fine. While this solution is adequate (it seems to work for infinite structures as well, which is good), I originally set out to solve this problem with a fold or a mapAccum of some sort (I don't really care if it doesn't work with infinite structures). Can anyone suggest a way to write this with a fold (or tell me why it isn't a good idea)? transpose = foldr (zipWith (:)) (repeat []) HTH, Stefan ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Receiving multicasts
Hello, Has anyone figured out a way to receive multicasts in a Haskell program? It doesn't appear that Network.Socket.setSocketOption provides enough information to join a multicast address. Any information would be appreciated. -- Rich AIM : rnezzy ICQ : 174908475 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] what do you think of haskell ? (yes, it's a bit general ...:)
hi all folks, i'm diving into haskell for more than one year now. the reason for that is just that i like haskell. (i'm a computer science student) but i consider to move back to c/c++. here are my thoughts. i've no specific question but i'd like to have your opinion. here we go: haskell is really nice : short phrased, declarative, math-feeling, the type system ensure low bug amount (i surely forget other nice things). but haskell is quite ugly in some way : * array : if i want to write something involving array, i could use list, and a lot (too much!) of array types (io/st, mutable/immutable, c-friendly (storable). worst, code involving one type can need to be rewritten for another type. * laziness / array (again) (i am biased since i have no knowledge of haskell profiling / performance seeking) always with array code, i was forced (maybe it's because i dont know enough) to use iouarray : if not, performance|memory consumption were low|high. but with iouarray, i cant use an array of MyType; i have to use an array of (say) float (it's ok if i have only float in MyType). that kind of thing is what i think is *really* low-level. in c, i can have an array of what-i-want. * randomIO side-effect is nicely resolved with monad. and you have to thread your state. if you're writing your monad or use a transformer, things are quite explicitly (even if it's implicit in the do notation) threaded. but the threading of the randomIO argument is really not explicit for me : it just means that the underlying/threaded state in the IO monad can encapsulated a lot of things. it feels exactly like a c function. but usually, you cant design a c-like function in haskell (i.e. a function with state). * more things i dont remember... * generally my general feeling for haskell vs c is: in haskell i always have to learn new things to get my work done ; although haskell is really easy to learn in the first step, it's becoming increasingly hard to get what's the *trick* to do what i want. e.g. writing myfunction x1 .. xn | x1 `seq` ... False = undefined is not declarative (and i still have to learn to identify where it helps and where it doesn't) the c language take some more time to learn at the beginning but that's it! what you can learn by after is better c programming. there are memory management, pointers, side-effects, but you can do what you want, it behaves as expected and you dont have to learn (in a academic paper) how to use an array or how to do io. :) please don't tell me you're stupid, go back to your c and leave this glorious mailing list did you had the same feeling ? does it disappear ? how ? thanks a lot, vo minh thu ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Fibonacci numbers generator in Haskell
Fibonacci numbers implementations in Haskell one of the classical examples. An example I found is the following: fibs :: [Int] fibs = 0 : 1 : [ a + b | (a, b) - zip fibs (tail fibs)] To get the k-th number you do the following: Result = fibs !! k It is elegant but creates a list of all Fibonacci numbers less than k-th, and the code is not very readable :). I wrote my own Fibonacci numbers generator: fib :: Int - [Int] fib 0 = [0,0] fib 1 = [1,0] fib n = [sum prevFib, head prevFib] where a = fib (n - 1) To get the k-th number you do the following: result = head (fib k) It does not generate full list of Fibonacci numbers, but keeps only 2 previous numbers, and has only one recursive call. Because the list always has only 2 elements using the functions head and sum is a bit overkill. Can we do better? _ Are you using the latest version of MSN Messenger? Download MSN Messenger 7.5 today! http://join.msn.com/messenger/overview ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] what do you think of haskell ? (yes, it's a bit general ...:)
Hi Minh, When I write Haskell, its because I want to write the code quickly, not because I want it to run quickly. GHC is a wonderful compiler and makes things go fast, but Hugs is faster at compiling, so I always use Hugs (WinHugs in fact). If your focus is on things going fast, then with Haskell you have to think harder to get this - but its certainly possible, see the shootout benchmarks. * array Why do you want to write things with Array's - in C the default type of everything is an Array, and you occasionally use a Linked List. In Haskell its the opposite - linked lists are very nice and natural - I never use arrays. * laziness / array (again) Laziness often makes my code go faster - because I am lazy and Haskell's laziness means that when I combine things in just lazy ways Haskell drops the things I did that were useless. * randomIO side-effect is nicely resolved with monad. and you have to thread your state. if you're writing your monad or use a transformer, things are quite explicitly (even if it's implicit in the do notation) threaded. Yes, C is nicer for this kind of thing, in my opinion - nicer from a practical view, even if it is pretty horrid in the end. * generally my general feeling for haskell vs c is: in haskell i always have to learn new things to get my work done ; although haskell is really easy to learn in the first step, it's becoming increasingly hard to get what's the *trick* to do what i want. I think I know very few tricks, and have never felt the need to know more. If you stick to simple Haskell it keeps your brain for other things. I think what you seem to be saying is that to write fast Haskell programs requires more effort and more work than C programming? Exactly what Haskell programs were you trying to write, and did you try just writing them in a naive way and checking that just simple and stupid doesn't give you the performance you need? Maybe if you learnt the FFI aspects of Haskell, you could write your code in Haskell, and then either optimise the Haskell or just give up and move that portion to C, keeping the rest in Haskell. Thanks Neil ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Fibonacci numbers generator in Haskell
Here's some code I wrote a while back for computing the nth Fibonacci number. It has O(log n) time complexity rather than O(n). It isn't the most elegant example, but it should be one of the fastest approaches. import Data.Bits (shiftR, xor, (.|.), (..)) import Data.Word (Word32) fibo :: Word32 - Integer fibo n = loop (highestBitMask n) 1 0 where loop :: Word32 - Integer - Integer - Integer loop i a b | i == 0 = b | n .. i /= 0 = (loop (shiftR i 1) $! a*(2*b + a)) $! a*a + b*b | otherwise= (loop (shiftR i 1) $! a*a + b*b) $! b*(2*a - b) highestBitMask :: Word32 - Word32 highestBitMask x = case (x .|. shiftR x 1) of x - case (x .|. shiftR x 2) of x - case (x .|. shiftR x 4) of x - case (x .|. shiftR x 8) of x - case (x .|. shiftR x 16) of x - (x `xor` (shiftR x 1)) Cheers, Spencer Janssen On 6/15/06, Vladimir Portnykh [EMAIL PROTECTED] wrote: Fibonacci numbers implementations in Haskell one of the classical examples. An example I found is the following: fibs :: [Int] fibs = 0 : 1 : [ a + b | (a, b) - zip fibs (tail fibs)] To get the k-th number you do the following: Result = fibs !! k It is elegant but creates a list of all Fibonacci numbers less than k-th, and the code is not very readable :). I wrote my own Fibonacci numbers generator: fib :: Int - [Int] fib 0 = [0,0] fib 1 = [1,0] fib n = [sum prevFib, head prevFib] where a = fib (n - 1) To get the k-th number you do the following: result = head (fib k) It does not generate full list of Fibonacci numbers, but keeps only 2 previous numbers, and has only one recursive call. Because the list always has only 2 elements using the functions head and sum is a bit overkill. Can we do better? _ Are you using the latest version of MSN Messenger? Download MSN Messenger 7.5 today! http://join.msn.com/messenger/overview ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] what do you think of haskell ? (yes, it's a bit general ...:)
hi, thanks for your answer. the kind of thing i want to do : computer graphics programming. so array is better than list (no ?) to represent images ... bye vo minh thu (hey, my last name is VO, and my first name is Thu, not Minh :) 2006/6/15, Neil Mitchell [EMAIL PROTECTED]: Hi Minh, When I write Haskell, its because I want to write the code quickly, not because I want it to run quickly. GHC is a wonderful compiler and makes things go fast, but Hugs is faster at compiling, so I always use Hugs (WinHugs in fact). If your focus is on things going fast, then with Haskell you have to think harder to get this - but its certainly possible, see the shootout benchmarks. * array Why do you want to write things with Array's - in C the default type of everything is an Array, and you occasionally use a Linked List. In Haskell its the opposite - linked lists are very nice and natural - I never use arrays. * laziness / array (again) Laziness often makes my code go faster - because I am lazy and Haskell's laziness means that when I combine things in just lazy ways Haskell drops the things I did that were useless. * randomIO side-effect is nicely resolved with monad. and you have to thread your state. if you're writing your monad or use a transformer, things are quite explicitly (even if it's implicit in the do notation) threaded. Yes, C is nicer for this kind of thing, in my opinion - nicer from a practical view, even if it is pretty horrid in the end. * generally my general feeling for haskell vs c is: in haskell i always have to learn new things to get my work done ; although haskell is really easy to learn in the first step, it's becoming increasingly hard to get what's the *trick* to do what i want. I think I know very few tricks, and have never felt the need to know more. If you stick to simple Haskell it keeps your brain for other things. I think what you seem to be saying is that to write fast Haskell programs requires more effort and more work than C programming? Exactly what Haskell programs were you trying to write, and did you try just writing them in a naive way and checking that just simple and stupid doesn't give you the performance you need? Maybe if you learnt the FFI aspects of Haskell, you could write your code in Haskell, and then either optimise the Haskell or just give up and move that portion to C, keeping the rest in Haskell. Thanks Neil ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] what do you think of haskell ? (yes, it's a bit general ...:)
On 6/15/06, minh thu [EMAIL PROTECTED] wrote: hi, thanks for your answer. the kind of thing i want to do : computer graphics programming. so array is better than list (no ?) to represent images ... This may not be very helpful, but I would say that an Image is neither a list nor an array - it's a function! :-) /S -- Sebastian Sylvan +46(0)736-818655 UIN: 44640862 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] what do you think of haskell ? (yes, it's a bit general ...:)
On Jun 15, 2006, at 6:18 PM, Sebastian Sylvan wrote: This may not be very helpful, but I would say that an Image is neither a list nor an array - it's a function! :-) How exactly do you manipulate the bits and bytes of a function? -- http://wagerlabs.com/ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] what do you think of haskell ? (yes, it's a bit general ...:)
right :) i had to say a discrete image ... or a raster, or an *array* of pixels ! sylvan, it must be nice to talk with you (an funny) about haskell and cg :) mt 2006/6/15, Sebastian Sylvan [EMAIL PROTECTED]: On 6/15/06, minh thu [EMAIL PROTECTED] wrote: hi, thanks for your answer. the kind of thing i want to do : computer graphics programming. so array is better than list (no ?) to represent images ... This may not be very helpful, but I would say that an Image is neither a list nor an array - it's a function! :-) /S -- Sebastian Sylvan +46(0)736-818655 UIN: 44640862 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] what do you think of haskell ? (yes, it's a bit general ...:)
On 6/15/06, Joel Reymont [EMAIL PROTECTED] wrote: On Jun 15, 2006, at 6:18 PM, Sebastian Sylvan wrote: This may not be very helpful, but I would say that an Image is neither a list nor an array - it's a function! :-) How exactly do you manipulate the bits and bytes of a function? Well I suppose by returning a new function which produces the required changes, and perhaps refers back to the old function in certain cases. It's a neat abstraction, anyway, even if you at the very bottom have some low level representation using pedestrian concepts such as bytes and bits :-) /S -- Sebastian Sylvan +46(0)736-818655 UIN: 44640862 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] do we have something like isDefined or isNull in Haskell?
Duncan Coutts wrote: On Thu, 2006-06-15 at 13:11 +0100, Duncan Coutts wrote: then you can construct your records using: foo = default { weight = 3.2 } Oops, as David House pointed out to me that should of course be foo = default { weight = Just 3.2 } I think the correct response in these cases is: That was left as an exercise for the type checker. On another note, who picked the word `Just' for this type and how did we end up with Some x | None in O'Caml and Just x | Nothing in Haskell? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] what do you think of haskell ? (yes, it's a bit general ...:)
On Thu, 15 Jun 2006, minh thu wrote: * randomIO side-effect is nicely resolved with monad. and you have to thread your state. if you're writing your monad or use a transformer, things are quite explicitly (even if it's implicit in the do notation) threaded. You know that random generators are not bounded to IO? 'random' and related functions work just like the random generators in object oriented designs. In OO languages the random generator is usually an object which hides a state and there can be several instances of random generators. In Haskell the random generator can be viewed as a State monad. But you are right, you have to carry the monad with you all the time. Alternatively it is sometimes simpler to consume values from a list generated by randomRs. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Fibonacci numbers generator in Haskell
On Thu, 15 Jun 2006, Vladimir Portnykh wrote: Fibonacci numbers implementations in Haskell one of the classical examples. An example I found is the following: fibs :: [Int] fibs = 0 : 1 : [ a + b | (a, b) - zip fibs (tail fibs)] To get the k-th number you do the following: Result = fibs !! k It is elegant but creates a list of all Fibonacci numbers less than k-th, and the code is not very readable :). The garbage collector will free storage for values that are no longer needed. So if the garbage collector is hard working there will be no more than 2 previous values stored per computation of a new value. I wrote my own Fibonacci numbers generator: fib :: Int - [Int] fib 0 = [0,0] fib 1 = [1,0] fib n = [sum prevFib, head prevFib] where a = fib (n - 1) To get the k-th number you do the following: result = head (fib k) It does not generate full list of Fibonacci numbers, but keeps only 2 previous numbers, and has only one recursive call. Because the list always has only 2 elements using the functions head and sum is a bit overkill. If you want to do it that way, better use pairs of numbers instead of lists. Lists can have any number of elements, pairs have exactly two members. So the latter is more type safe. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Everything but the lazyness - idea for force/delay lists
minh thu wrote: hi in fact, i think that for a while ... moreover, i thought to translate to some kind of readable c (because there s so much c libs all around). i think it's even possible to retain laziness where it's ok ( for data structure essentially). Hi Thu - Someone pointed out (offlist) a useful paper by Phil Wadler How to add laziness to a strict language without even being odd which shows how laziness can be used without needing a lifted type system. Once you've understood the subtleties of the FFI, it's relatively easy to use it link to C libs. is-it possible to know what you're doing at metamilk ? Well I'd tell you if I knew myself! :-) Seriously though, at the moment my aim is to develop an integrated programming environment for a language similar to Haskell, either Haskell itself or a non-lazy version of it, also with some syntactic modifications to make it easier to use. It's in very early stages though. I'm trying as much as possible to write everything from scratch (including the GUI) in Haskell so I can see what the advantages/disadvantages of Haskell are and where improvements in the syntax would be useful. My experience so far is that the typeclasses and existentials that Haskell supports are an advantage over OCaml or SML, but that the lazyness is a real nusiance but with some extra effort it's possible to mostly get rid of it. Regards, Brian. mt 2006/6/13, Brian Hulley [EMAIL PROTECTED]: Hi - I've been thinking about how to get an extremely fast language with all the benefits of Haskell ie completely pure with no side effects, but with monads, higher order functions, type classes etc, but without the lazyness. -- Logic empowers us and Love gives us purpose. Yet still phantoms restless for eras long past, congealed in the present in unthought forms, strive mightily unseen to destroy us. http://www.metamilk.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] what do you think of haskell ? (yes, it's a bit general ...:)
hi, yes i know about that, but i was talking about randomIO which breaks that view; and i find that quite weird for a 'clean' language. * another thing i remember now : binary io. there are some libraries but it's not really standard. and it's weird to learn all (well, or just one) that libraries just to do io. cheers, mt 2006/6/15, Henning Thielemann [EMAIL PROTECTED]: On Thu, 15 Jun 2006, minh thu wrote: * randomIO side-effect is nicely resolved with monad. and you have to thread your state. if you're writing your monad or use a transformer, things are quite explicitly (even if it's implicit in the do notation) threaded. You know that random generators are not bounded to IO? 'random' and related functions work just like the random generators in object oriented designs. In OO languages the random generator is usually an object which hides a state and there can be several instances of random generators. In Haskell the random generator can be viewed as a State monad. But you are right, you have to carry the monad with you all the time. Alternatively it is sometimes simpler to consume values from a list generated by randomRs. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] do we have something like isDefined or isNull in Haskell?
Vladimir, I think you forgot to put Haskell-cafe as a recipient of this email, so first I'll repost what you wrote. On 6/15/06, Vladimir Portnykh [EMAIL PROTECTED] wrote: many thanks. i have the follwoing code: module MyType (DataContainer(..)) where import Maybe data DataContainer = MyType { date :: [Maybe Double], weight ::[Maybe Double], height ::[Maybe Double] } deriving (Eq, Ord, Show) myType = MyType { date = [38548,38562,38576,38590,38604,38618,38632,38646,38660,38674], weight = [], height = [] } reportProblem = if isJust (height myType !! (length (date myType) - 1)) then 0 else 1 it failed to compiled saying No instance for (Num (Maybe Double)) arising from the literal 38674 . After reading your code for a while, I still don't know if I quite understand. Can you explain exactly what you are trying to do? My thought is that maybe you just want a list of objects, since you are indexing height at the same index as date. Then you just want a list of the structures that Duncan described. myObjects = [default {weight = 3.2}, default {weight = 7.9, date=38458},...] If not, hopefully somebody a little more knowledgable than I can help you. Bryan ___ 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
Re: [Haskell-cafe] what do you think of haskell ? (yes, it's a bit general ...:)
minh thu wrote: hi all folks, i'm diving into haskell for more than one year now. the reason for that is just that i like haskell. (i'm a computer science student) but i consider to move back to c/c++. There is also OCaml and SML, both of which have freely available compilers to generate fast native code (SML has MLton - a whole program optimizing compiler), and which use side effects instead of monads. here are my thoughts. i've no specific question but i'd like to have your opinion. here we go: haskell is really nice : short phrased, declarative, math-feeling, the type system ensure low bug amount (i surely forget other nice things). but haskell is quite ugly in some way : * array : if i want to write something involving array, i could use [snip] Bulat's written a new array library - see http://www.haskell.org//pipermail/haskell/2006-June/018044.html but with iouarray, i cant use an array of MyType; i have to use an array of (say) float (it's ok if i have only float in MyType). that kind of thing is what i think is *really* low-level. in c, i can have an array of what-i-want. There is a compromise between using IOArray versus IOUArray. IOArrays might even be more efficient than unboxed arrays in some situations: 1) When (large) elements are shared between different arrays or different data structures, since only the pointer needs to be copied 2) Compared to C++ templates, only one piece of code needs to be generated to handle them, so there might be fewer cache misses at runtime * randomIO side-effect is nicely resolved with monad. and you have to thread your state. if you're writing your monad or use a transformer, things are quite explicitly (even if it's implicit in the do notation) threaded. but the threading of the randomIO argument is really not explicit for me : it just means that the underlying/threaded state in the IO monad can encapsulated a lot of things. it feels exactly like a c function. but usually, you cant design a c-like function in haskell (i.e. a function with state). You can instead write a function that returns an action in whatever monad you're using. For example using a Reader monad to hold IORefs, you can easily update the state of these IORefs. Of course this is more work than just declaring some global variable in C and updating it in the function, but it gives you more flexibility in the long run - you now have full control over the environment that the function sees, just by running it in another Reader containing different IORefs. * more things i dont remember... * generally my general feeling for haskell vs c is: in haskell i always have to learn new things to get my work done ; although haskell is really easy to learn in the first step, it's becoming increasingly hard to get what's the *trick* to do what i want. Maybe you are expecting too much of your code. A while back I was agonizing over the perfect way to write something using higher order functions, then a comment by Bulat ( http://www.haskell.org//pipermail/haskell-cafe/2006-May/015540.html ) helped me put things in perspective: imho, it's typical functional style, but without using higher-level functions So now I just concentrate on getting code to work and leave the highly obfuscated cleverness for later... ;-) e.g. writing myfunction x1 .. xn | x1 `seq` ... False = undefined is not declarative (and i still have to learn to identify where it helps and where it doesn't) Bang patterns would at least make the syntax easier to write. Strictness annotations work both ways - perhaps the question should be: where does *lazyness* help? I don't think anyone really has a clear answer to this at the moment. the c language take some more time to learn at the beginning but that's it! what you can learn by after is better c programming. I think (as someone who has spent at least 16 years programming in C++) that there are millions of complicated things there as well. The syntax and semantics of a language is only the very start. C and C++ have idioms to learn too, and like anything, it takes a lot of time and experience to know which trick to use in a given situation. Just as the object oriented community has spent many years slowly trying to understand how to design object oriented programs and made major mistakes at the beginning (eg using implementation inheritance instead of interfaces and composition (exactly what is fully supported in Haskell by classes and instances)). there are memory management, pointers, side-effects, but you can do what you want, it behaves as expected and you dont have to learn (in a academic paper) how to use an array or how to do io. Haskell has a difficult learning curve. I remember the first few times I looked at the type signatures for the methods of Data.IArray and it was certainly not at all clear! :-) In the worst case, you can always use the FFI to link with some C if there is some especially
Re: [Haskell-cafe] what do you think of haskell ? (yes, it's a bit general ...:)
minh thu wrote: but i consider to move back to c/c++. I'm led to believe that you just haven't got the hang of the things that just aren't there in C, such as Monads and higher order functions. So you cannot yet see what you would miss in C. (And I guess, you're not feeling at home in C++ either, since there is no language named C/C++.) Whatever, if you believe a person can only master a single programming language, it might as well be C for you... * array : if i want to write something involving array, i could use list, and a lot (too much!) of array types (io/st, mutable/immutable, c-friendly (storable). I've never understood peoples preoccupation with arrays. You lose all flexibility just for O(1) lookup and O(1) destructive(!) update. Most of the time you're better served with a finite map. worst, code involving one type can need to be rewritten for another type. Huh? It doesn't, that's the point of the overloaded IArray/MArray interface! * laziness / array (again) always with array code, i was forced (maybe it's because i dont know enough) to use iouarray : if not, performance|memory consumption were low|high. You're putting unevaluated thunks into your data structure, probably accumulating them there. Bringing out the sledge hammer of IOUArray only obscures the problem. You should 'seq' data before writeArray'ing it. * randomIO but the threading of the randomIO argument is really not explicit for me : it just means that the underlying/threaded state in the IO monad can encapsulated a lot of things. Duh, don't use IO if you neither like nor need it. Most random functions are no IO actions for a reason. e.g. writing myfunction x1 .. xn | x1 `seq` ... False = undefined is not declarative But it isn't all that bad either... (and i still have to learn to identify where it helps and where it doesn't) ...while this is the real problem. You have to understand lazy evaluation to make beneficial use of 'seq'. It really helps to reproduce some reductions on paper. the c language take some more time to learn at the beginning but that's it! Oh come on, you cannot honestly believe that. If so, please send me some chunk of nontrivial C code, I send you back at least one location where it produces undefined behaviour. Yes, I'm confident that I'll find some. did you had the same feeling ? does it disappear ? how ? Never had that feeling, because C is just too ugly. It will disappear once you really understand lazy evaluation. Udo. signature.asc Description: Digital signature ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] do we have something like isDefined or isNullin Haskell?
On Thursday, June 15, 2006 8:07 PM Clifford Beshers wrote: On another note, who picked the word `Just' for this type and how did we end up with Some x | None in O'Caml and Just x | Nothing in Haskell? I've always thought this is one of the most charming things about Haskell, along with the use of the quaint word otherwise. It's so friendly and conversational compared to the cold logicality of OCaml or the shoutyness of SML's SOME and NONE eg a phone call to a relative: callee: Who's that? caller: Don't worry, it's Just me! at school: teacher: What did you just say? pupil: Nothing in a shop: Haskeller: I'll just buy some crisps OCamler: I'll buy some packet of crisps SMLer: I'll buy SOME packet of crisps Regards, Brian. -- Logic empowers us and Love gives us purpose. Yet still phantoms restless for eras long past, congealed in the present in unthought forms, strive mightily unseen to destroy us. http://www.metamilk.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Fibonacci numbers generator in Haskell
G'day all. Quoting Vladimir Portnykh [EMAIL PROTECTED]: I wrote my own Fibonacci numbers generator: fib :: Int - [Int] fib 0 = [0,0] fib 1 = [1,0] fib n = [sum prevFib, head prevFib] where a = fib (n - 1) To get the k-th number you do the following: result = head (fib k) [...] Can we do better? Sure we can. We can use a more efficient algorithm: fib :: Integer - Integer fib k | k 0 = error fib | otherwise = fst (fib' k) fib' :: Integer - (Integer,Integer) fib' 0 = (0,1) fib' k | k `mod` 2 == 0 = let (a,b) = fib' (k `div` 2 - 1) c = a + b c2 = c*c in (c2 - a*a, c2 + b*b) | otherwise = let (a,b) = fib' ((k-1) `div` 2) c = a+b a2 = a*a in (b*b + a2, c*c - a2) Cheers, Andrew Bromage ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Fibonacci numbers generator in Haskell
How about the closed form ;) -- fib x returns the x'th number in the fib sequence fib :: Integer - Integer fib x = let phi = ( 1 + sqrt 5 ) / 2 in truncate( ( 1 / sqrt 5 ) * ( phi ^ x - phi' ^ x ) ) Seems pretty quick to me, even with sqrt and arbitrarily large numbers. On 6/15/06 9:33 AM, Vladimir Portnykh [EMAIL PROTECTED] wrote: Fibonacci numbers implementations in Haskell one of the classical examples. An example I found is the following: fibs :: [Int] fibs = 0 : 1 : [ a + b | (a, b) - zip fibs (tail fibs)] To get the k-th number you do the following: Result = fibs !! k It is elegant but creates a list of all Fibonacci numbers less than k-th, and the code is not very readable :). I wrote my own Fibonacci numbers generator: fib :: Int - [Int] fib 0 = [0,0] fib 1 = [1,0] fib n = [sum prevFib, head prevFib] where a = fib (n - 1) To get the k-th number you do the following: result = head (fib k) It does not generate full list of Fibonacci numbers, but keeps only 2 previous numbers, and has only one recursive call. Because the list always has only 2 elements using the functions head and sum is a bit overkill. Can we do better? _ Are you using the latest version of MSN Messenger? Download MSN Messenger 7.5 today! http://join.msn.com/messenger/overview ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Fibonacci numbers generator in Haskell
Mathew Mills [EMAIL PROTECTED] writes: -- fib x returns the x'th number in the fib sequence fib :: Integer - Integer fib x = let phi = ( 1 + sqrt 5 ) / 2 phi' = ( 1 - sqrt 5 ) / 2 in truncate( ( 1 / sqrt 5 ) * ( phi ^ x - phi' ^ x ) ) -- Seems pretty quick to me, even with sqrt and arbitrarily large numbers. You don't actually need the part with phi'^x. Since |phi'| 1, phi'^x gets small fast as x increases. In fact |phi'^x| is always smaller than 1/2, so -phi'^x in the expression above can be replaced by +0.5. Unfortunately with arbitrarily large numbers it gets the answer wrong. Arbitrarily large in this case is smaller than 100. fib 100 354224848179261800448 The correct answer is 354224848179261915075 The relative error is very small, so it is a good approximation. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe