Re: Haskell and the NGWS Runtime
"Erik Meijer" [EMAIL PROTECTED] wrote, I'm weird, but not a true 'CS' person. I run Linux -- do I need to move to NetBSD? The single Linux box we have in the Microsoft lab was severely hacked from the outside, they have moved to FreeBSD, which is supposed to be safer (I hope so). Morale: Never trust a Linux box at Microsoft :-) Manuel
No Subject
dear sirs, I have a problem to solve in haskell but i do not know how to go about it. the problem is write a haskell function for computing the average value of a list of numbers. I am using Hug 98 for windows and I tried the following way: average (x:xs) = s/l where s=sum(x:xs) l=(x:xs) I am a beginner studying on my own. I would appreciate it if you could explain what I should and should not do. Thank you yours sincerely Ruhi Sood
Re: Haskell and the NGWS Runtime
"Erik Meijer" [EMAIL PROTECTED] wrote, The single Linux box we have in the Microsoft lab was severely hacked from the outside, they have moved to FreeBSD, which is supposed to be safer (I hope so). Morale: Never trust a Linux box at Microsoft :-) The lab is *sponsored* by Microsoft, but definitively not *at* Microsoft. I doubt there are any Linux boxes at Microsoft :-) Now define Microsoft. There definitely are Linux boxes at Microsoft Research, as we all know :-) There have also been reports of Linux boxes in the main company, but the validity of these reports is of course not easy to verify. Manuel
RE:
There is a nice way to solve this using _folds_. Basically, a fold over a list is what you would get if you replaced every _:_by some binary operator (such as _+_) and the end of the list by some constant (such as 0). In Haskell one pre-defined fold operator is "foldr" which stands for _fold from the right_ i.e. associate from the right. To find the sum of a list of numbers, you could write: sum :: [Int] - Int sum = foldr (+) 0 To find the length of a list of number, you could use: length :: [a] - Int length = foldr ( \ x n - 1+ n) 0 The function ( \ x n - 1 + n) discards its left argument, and returns the right argument + 1. so, your average function could be composed of the sum and length functions defined like above (I haven't checked for errors). I know you are a beginner, but there a very beautiful property of folds that allows you to combine them. If you define your average using the sum and length functions, the program is very clear, but it has the drawback of traversing your list twice. Once to compute the sum and once to compute the length. Using the _fusion_ property of folds, one could write an average function that traverses the list just once. For more information on this, there is a very good paper by Graham Hutton _A tutorial on the universality and expressiveness of fold_ which probably can be found on the web or in a University Library in the Journal of Functional Programming. Enjoy. --Peter Douglass -Original Message-From: Lokesh Sood [mailto:[EMAIL PROTECTED]]Sent: Sunday, August 06, 2000 9:00 AMTo: [EMAIL PROTECTED]Subject: dear sirs, I have a problem to solve in haskell but i do not know how to go about it. the problem is write a haskell function for computing the average value of a list of numbers. I am using Hug 98 for windows and I tried the following way: average (x:xs) = s/l where s=sum(x:xs) l=(x:xs) I am a beginner studying on my own. I would appreciate it if you could explain what I should and should not do. Thank you yours sincerely Ruhi Sood
Re: Haskell and the NGWS Runtime
"Manuel" == Manuel M T Chakravarty [EMAIL PROTECTED] writes: Manuel "Erik Meijer" [EMAIL PROTECTED] wrote, The single Linux box we have in the Microsoft lab was severely hacked from the outside, they have moved to FreeBSD, which is supposed to be safer (I hope so). Morale: Never trust a Linux box at Microsoft :-) The lab is *sponsored* by Microsoft, but definitively not *at* Microsoft. I doubt there are any Linux boxes at Microsoft :-) Manuel Now define Microsoft. There definitely are Linux boxes at Manuel Microsoft Research, as we all know :-) Manuel There have also been reports of Linux boxes in the main Manuel company, but the validity of these reports is of course not Manuel easy to verify. But easy to believe... I mean, wouldn't you take a good look at your competition? (Of course, Linux is no competition to Windows... or rather the other 'round ;-) Bye, J -- Jürgen A. Erhard eMail: [EMAIL PROTECTED] phone: (GERMANY) 0721 27326 MARS: http://members.tripod.com/Juergen_Erhard/mars_index.html "We must learn from our mistakes, so we can make bigger and better ones." -- Bruce M Krawetz PGP signature
Text encodings
I am designing a framework for handling text in various encodings. Here is the current state of thoughts. Comments are welcome. An important concept is a conversion from a sequence of characters to another sequence of characters, usually in a different encoding. Conversions are usually to Unicode or from Unicode, but for simplicity all are treated equivalently. A conversion exists by itself, not by its source and target encoding. Conversions can be composed. The conversion should be done an arbitrary block of text at a time. It matters how input and output are split because encodings may be stateful (e.g. ISO-2022) and may be multi-byte. It cannot be the whole input in one go because Handle writes are separate and of course should be done immediately. It cannot be one character at a time for efficiency reasons. It follows that the conversion is generally stateful. A block of input is supplied and the conversion produces as much of output as it can, updating the state. Splitting of input should not influence the concatenation of outputs. Handling a possibly partial multibyte character at the end can be done by the conversions themselves, to simplify clients - state is needed anyway for stateful encodings. Apart from real conversion, a second operation is needed: to close a conversion, i.e. to bring the output state to the proper value (some encodings need that) and to signal an error for an incomplete input multibyte character. Errors are a complex story. Errors may result from input (invalid source bytes) or output (unavailable characters), but for simplicity they are not distinguished that way. Various reactions on errors are needed: - An error may raise an exception. I think it should be the default for functions like getChar and putChar, treated similarly to I/O errors. - An error may be ignored. This is the only possibility for readFile and getContents which have no way to signal it because they are lazy. From a WWW browser or grep I expect that encoding errors are ignored, leaving e.g. U+FFFD standard replacement character. - Finally, from a text editor I expect to read the file even in the presence of errors and warn me that there were errors so fragments may be corrupt. It follows that a generic result from a conversion is always a converted text and additionally the flag telling whether everything went fine. The text should be produced lazily in case nobody looks at the error flag: when a conversion is used directly by the programmer, he should not be forced to split the text into pieces of an efficient size, and converting a long string should run in constant memory. Since the conversion may be stateful (with a state being uninteresting fot the outside), and may signal errors, and is usually done near input and output (attached to Handles), and implementation may use foreign functions - I am putting the whole thing in the IO monad. Conversion should be an abstract type. This allows private hooks for efficient conversion of raw blocks of memory near Handle I/O, without using intermediate Strings of the official interface. The above suggests a natural representation: data Conversion = Conv { convert :: String - IO (String, Bool), flush :: IO (String, Bool)} convert and flush are public functions. A custom conversion can built from a pair of such functions. The Conversion type represents a conversion which is taking place, with current state. Another concept is a conversion that can be started on a text, starting from its initial state. Since the only thing one can do with a conversion in the second sense is to start a conversion in the first sense, a good type for the second sense is IO Conversion. This is an official interface of obtaining most conversions. The action produces a new Conversion object, ready to begin accepting input. It could be a separate abstract type with a separate start function, but it's simpler to have just IO Conversion. Other public functions: convertThrow:: Conversion - String - IO String flushThrow:: Conversion - IO String -- Unfurtunately these is no good IOError code. Something as simple -- as BadInput would be great! It could also work for readIO. convertWhole:: IO Conversion - String - IO (String, Bool) convertWholeThrow:: IO Conversion - String - IO String -- These obtain the conversion, call convert and flush. idConv:: IO Conversion composeConv:: IO Conversion - IO Conversion - IO Conversion from1CharStatelessConf:: (String - (String, Bool)) - IO Conversion -- E.g. ISO-8859-x - Unicode, Unicode - ISO-8859-x, Unicode - UTF-8. -- Blocks are converted completely independently. statelessConv:: (String - (String, String, Bool)) - IO Conversion -- E.g. UTF-8 - Unicode. This wrapper automaticelly handles a -- partial multibyte sequence at the end which is returned on the -- second element of the tuple. statefulConv:: (String - IO (String, Bool)) - IO (String, Bool) - IO Conversion -- A most general
Re:
Lokesh Sood schrieb: dear sirs,I have a problem to solve in haskell but i do not know how to go about it. the problem iswrite a haskell function for computing the average value of a list of numbers.I am using Hug 98 for windows and I tried the following way:average (x:xs) = s/l where s=sum(x:xs) l=(x:xs)I am a beginner studying on my own. I would appreciate it if you could explain what I should and should not do.Thank youyours sincerelyRuhi Sood Try the following: average :: [Int] -> Float average x | null x = 0.0 | otherwise = fromInt(sum x) / fromInt(length x) Mirko
Re:
On Sun, 6 Aug 2000, Mirko Pracht wrote: average x | null x= 0.0 What does you make thinking the average of an empty list is 0? Its' obviously _|_, thus average xs = sum xs / length xs [which is inefficient, but simple] Kili -- _|_ is pronounced 'bottom', and is the greatest lower bound of a complete lattice. Nick Williams in comp.lang.functional
Re: Haskell and the NGWS Runtime
"Jürgen A. Erhard" wrote: "Manuel" == Manuel M T Chakravarty [EMAIL PROTECTED] writes: Manuel "Erik Meijer" [EMAIL PROTECTED] wrote, [...] The lab is *sponsored* by Microsoft, but definitively not *at* Microsoft. I doubt there are any Linux boxes at Microsoft :-) Manuel [...] There have also been reports of Linux boxes in the main Manuel company, but the validity of these reports is of course not But easy to believe... I mean, wouldn't you take a good look at your competition? Having spent five years doing Windows and Win32 hacking, and 10+ years doing Unix hacking before that, I can assure you there is little evidence that Microsoft are interested in learning anything whatsoever from Linux or any other Unix work-alike. Rather than re-iterate the argument, I'll simply point interested readers towards the excellent article "Working with Win32: the good, the bad and the ugly" by David G. Korn http://www.usenix.org/publications/login/1997-11/win32.html which mostly reinforces the quote: Those who do not understand Unix are condemned to reinvent it, poorly. -- Henry Spencer Now that I've gotten MY little barb in, can we PLEASE end this tangent on OS wars, and return to our regularly scheduled language wars? :-) Thanks, -Antony (haskell list lurker) -- Antony Courtney [EMAIL PROTECTED] http://www.apocalypse.org/pub/u/antony