Re: [Haskell-cafe] Re: is there any reason why Language.C.Syntax.AST.CTranslUnit doesn't derive show?
Hello Anatoly, Bear in mind that gshow behaves a bit differently from the regular show (namely regarding parenthesis and efficiency). You can also use standalone deriving [1] to derive Show for those datatypes. Cheers, Pedro [1] http://www.haskell.org/ghc/docs/latest/html/users_guide/deriving.html#stand-alone-deriving On Sat, Mar 28, 2009 at 02:07, Anatoly Yakovenko wrote: > ah, i am guessing its because you can use Data.Generics.gshow to do > the same thing. Seems like that library will come in handy when > manipulating the AST, pretty cool stuff. > > On Fri, Mar 27, 2009 at 5:53 PM, Anatoly Yakovenko > wrote: > > is there any reason why Language.C.Syntax.AST.CTranslUnit doesn't > > derive show? I would like to look at the data structure it generates. > > It's a lot easier to experiment it when i can write a template C > > file, print out the AST and then modify that data structure directly, > > instead of trying to grok the library. > > > > Thanks for your great work btw, the parser is pretty sweet. > > Anatoly > > > ___ > 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] Re: Exception handling in numeric computations
Jonathan Cast writes: >> i.e., that application's file decoding result should be an Either >> type that anticipates that the file encoding may be invalid. > This is pretty standard, I thought. Do people write Haskell file input > methods that are undefined (`throw exceptions') on invalid inputs (e.g., > do people use read to parse input from users or the file system[1])? I often write parsers that either run successfully, or abort with an exception. I could of course return an Either type, but that would mean the whole file would need to be parsed before any results could be returned at all - which is a showstopper for streaming processing of large files. Since at least my files are typically machine generated, a parse error is either a programmer error in my parser, a programmer error in the generating program, or an operator error (viz. the user running the program on a completely different file type). In any case, I want the program execution to halt and report the error as soon as possible. So the difference between an exception or an error type is mainly what you intend to do about it. There's no point in wrapping divisions in Maybe unless you actually are able to do something useful to recover from a zero denominator. -k -- If I haven't seen further, it is by standing in the footprints of giants ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Use unsafePerformIO to catch Exception?
Duncan Coutts writes: > Yes, grouping is the one where I most often find the need for head or > partial patterns. The function group produces a list of non-empty lists > but that is not reflected in the type. On the other hand, actually > reflecting it in the type would make it more awkward: > > group :: Eq a => [a] -> [(a,[a])] group :: Eq a => [a] -> [(a,Int)] ? This is often what I want anyway :-) Of course, this only works for sane Eq instances. -k -- If I haven't seen further, it is by standing in the footprints of giants ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Exception handling in numeric computations
On Sat, Mar 28, 2009 at 10:53 AM, Ketil Malde wrote: > So the difference between an exception or an error type is mainly what > you intend to do about it. There's no point in wrapping divisions in > Maybe unless you actually are able to do something useful to recover > from a zero denominator. That is exactly the point I was trying to make. When I write a code, I can't say in advance, in what way it will be used. So, for dealing with errors, I have to choose one way or another, mostly without that knowledge. When I'm using e.g. C++, it's easy: something like mantra "when in doubt, throw an exception" :-) combined with RAII, works good (but not ideal, of course). So, I'll ask again: when I program in Haskell, what mechanism should I use? Gregory ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] ANN: smartword 0.0.0.5 Web based flash card for Word Smart I and II vocabularies
Name:smartword Synopsis:Web based flash card for Word Smart I and II vocabularies Version: 0.0.0.5 Homepage:http://kyagrd.dyndns.org/~kyagrd/project/smartword/ Category:Web,Education License: BSD3 License-file:LICENSE Author: Ahn, Ki Yung Maintainer: Ahn, Ki Yung === Web based online study tool for all vocabularies in Word Smart I and II, a poular book series for studying GRE vocabularies. I typed the vocabulary data and wrote the program in 2004, because I got too boring just going over the strange English words. If you don't read Korean, you can just ignore the Korean translation. Source code is outdated, so never even think of using it as a web programming reference. However, it will still be helpful as a neat web application when one tries to squeeze GRE vocabularies into the volatile memory of human brain. Installation: You can either compile CGI binaries with GHC or use Hugs to run lhs as a CGI script. Copy all .cgi files and data directories (book1, book1.ans, book2, book2.ans) into your webserver CGI directory (usually cgi-bin). Usage: If you get it wright the flash card goes away, but if you didn't get it the flash card goes to the bottom of the deck again. So, it won't end until you get all of them right. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Definition of "tail recursive" wrt Folds
Brent Yorgey wrote: What, strictly speaking, is the definition of ”tail recursive” as opposed to just “recursive”? A recursive function is tail recursive if the final result of the recursive call is the final result of the function itself. If the result of the recursive call must be further processed (say, by adding 1 to it, or consing another element onto the beginning of it), it is not tail recursive. With that said, tail recursion is not that useful of a concept in a lazy language like Haskell. What, non-strictly speaking, is the definition of "tail call" in Haskell as opposed to an eager language? For example in the following ML code, fun eager x = if p x then f x else g x both (f x) and (g x) are clearly tail calls, even if they appear in the syntactic context of the if-then-else expression. However, (p x) is clearly not a tail call. We can understand this by taking the operational behavior of the if-then-else expression into account: (p x) is evaluated first, then either the result of evaluating (f x), or the result of evaluating (g x) is returned without further processing. Now consider the same example in Haskell: lazy x = if p x then f x else g x Haskell's if-then-else expression has the same operational behavior as ML's, so again, (f x) and (g x) are tail calls. Now consider a variant: if' a b c = if a then b else c variant x = if' (p x) (f x) (g x) I would say that if' has the same operational behavior as an if-then-else expressions, and therefore, (f x) and (g x) are still tail cails, even if they now appear in the context of another function call. I think that a definition of tail calls in Haskell should take the strictness properties of functions into account. Such Haskell tail calls would have the same nice properties as tail calls in an eager language, but they would reflect that fact that it is hard to analyse strictness in Haskell. And I think that this makes for a much more friendly migration path. Instead of telling people: "No, forget about tail calls, in Haskell, you need something else", you could tell them: "Yes, tail calls are the way to go, but in Haskell, they look different". Tillmann ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Lazy counting and printing
Hi, I have written a code like this: import Control.Monad.State countedRecords:: [Int] -> StateT Int IO [Int] countedRecords [] = return [] countedRecords (r:rs) = do modify (+1) rest <- countedRecords rs return $ r:rs printEven :: [Int] -> StateT Int IO () printEven records = do let even = filter (\n -> n `mod` 2 == 0) records lift $ mapM_ (putStrLn . show) even combine :: [Int] -> StateT Int IO () combine records = countedRecords records >>= printEven main = do let numbers = [1..100] count <- execStateT (combine numbers) 0 putStrLn $ "Processed " ++ show count ++ " numbers." My intent is that of processing a large collection of records (in this example, integers) doing two things at once: count their total number and print those that satisfy a certain condition. I would like to keep the definitions of the two functions performing such tasks independent. As a result I've used the State monad to hide intermediate counts. Unfortunately the code above breaks laziness, in that records are first completely counted (and stored in memory) and only then filtered and printed. If I redefine main as: main = do let numbers = [1..] .. i.e. I produce an unbounded number of records, I get a stack overflow. Moreover, I don't like the way countedRecords and printEven are defined, because their type signatures are too "broad". I would prefer them to be: countedRecords :: [Int] -> State Int [Int] printEven :: [Int] -> IO () and let "combine" do some sort of lifting into the StateT monad. Is there any way to perform both operations lazily? Thanks ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Haxr doesn't compile from cabal (HTTP > 4000 breaks it)
haxr's also based on a fairly old version of HaXml. I'm not sure what the performance of the library is, other than it seems acceptable, but moving to ByteStrings all the way through might do some good. On Fri, Mar 27, 2009 at 9:13 PM, Henning Thielemann wrote: > > Most breakage caused by new HTTP package is due to the renaming from > Response to Response_String and Request to Request_String. I think it was > not a good idea to do that. There could have well be two types named > Response from different modules, one with a type parameter and the other one > without. Or the parametrized variant could have be named ResponseGeneric or > so. That would have allowed packages to work both with version 3001 and > 4000. > ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Definition of "tail recursive" wrt Folds
Tillmann Rendel wrote: > Now consider a variant: > >if' a b c = if a then b else c >variant x = if' (p x) (f x) (g x) > > I would say that if' has the same operational behavior as an > if-then-else expressions, and therefore, (f x) and (g x) are still tail > cails, even if they now appear in the context of another function call. yes, and the if' is also a tail-call. I guess I would say that (f x) and (g x) are tail-calls out of if' and if' is a tail-call out of variant. > I think that a definition of tail calls in Haskell should take the > strictness properties of functions into account. sometimes the optimizer does this, and makes more functions behave like tail- call than you'd expect! But sometimes it doesn't. > And I think that this makes for a much more friendly migration path. > Instead of telling people: "No, forget about tail calls, in Haskell, you > need something else", you could tell them: "Yes, tail calls are the way > to go, but in Haskell, they look different". Sometimes you do need something different though... if you're *producing* a lazy data structure. foldr f z (x:xs) = x `f` (foldr f z xs) If "f" is ":" then this is an efficient non-tail-call (later someone will demand the later parts of the list) whereas foldl f z (x:xs) = foldl f (z `f` x) xs is tail-recursion, which means that it needs to traverse the whole input-list before producing *any* output -Isaac ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Definition of "tail recursive" wrt Folds
You are absolutely right. The concept of tail call is not quite as easy to define in Haskell as some people seem to think. It is, after all, an operational concept. In your example > if' a b c = if a then b else c > variant x = if' (p x) (f x) (g x) the variant function tail calls if', and if' tail calls b or c. So in a transitive sense variant tail calls (f x) or (g x). -- Lennart On Sat, Mar 28, 2009 at 12:40 PM, Tillmann Rendel wrote: > Brent Yorgey wrote: >>> >>> What, strictly speaking, is the definition of ”tail recursive” as opposed >>> to >>> just “recursive”? >> >> A recursive function is tail recursive if the final result of the >> recursive call is the final result of the function itself. If the >> result of the recursive call must be further processed (say, by adding >> 1 to it, or consing another element onto the beginning of it), it is >> not tail recursive. >> >> With that said, tail recursion is not that useful of a concept in a >> lazy language like Haskell. > > What, non-strictly speaking, is the definition of "tail call" in Haskell as > opposed to an eager language? > > > For example in the following ML code, > > fun eager x = if p x then f x else g x > > both (f x) and (g x) are clearly tail calls, even if they appear in the > syntactic context of the if-then-else expression. However, (p x) is clearly > not a tail call. We can understand this by taking the operational behavior > of the if-then-else expression into account: (p x) is evaluated first, then > either the result of evaluating (f x), or the result of evaluating (g x) is > returned without further processing. > > > Now consider the same example in Haskell: > > lazy x = if p x then f x else g x > > Haskell's if-then-else expression has the same operational behavior as ML's, > so again, (f x) and (g x) are tail calls. > > > Now consider a variant: > > if' a b c = if a then b else c > variant x = if' (p x) (f x) (g x) > > I would say that if' has the same operational behavior as an if-then-else > expressions, and therefore, (f x) and (g x) are still tail cails, even if > they now appear in the context of another function call. > > > I think that a definition of tail calls in Haskell should take the > strictness properties of functions into account. Such Haskell tail calls > would have the same nice properties as tail calls in an eager language, but > they would reflect that fact that it is hard to analyse strictness in > Haskell. > > And I think that this makes for a much more friendly migration path. Instead > of telling people: "No, forget about tail calls, in Haskell, you need > something else", you could tell them: "Yes, tail calls are the way to go, > but in Haskell, they look different". > > Tillmann > ___ > 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
[Haskell-cafe] putting SOE 'on the library path'
This is a beginners question, which I have posted to beginn...@haskell.org several times, but because I have gotten no answer, I thought I would try the cafe. I'm working through the "School of Expression" book, and I would like to install the code that comes with the book somewhere. Right now I have to write my exercise programs in the same directory where the SOE code is sitting so that my exercise programs can import SOE modules. My platform is Windows. I tried using the -i options to ghc and ghci, but to no avail. It wasn't clear on Windows in what form -i takes its arguments: backslashes as usual for Windows, forward slashes perhaps? How about paths with spaces in them? Does it need quotes around the filename? I tried all combinations. ghc never gave an error indicating there was something wrong with what I typed. It happily accepted all forms of -i. But it never found the libraries. Thanks, Mike ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Haxr doesn't compile from cabal (HTTP > 4000 breaks it)
On 3/12/2009 07:51, Jeff Heard wrote: The haxr cabal library dependencies seem to be off. I wonder, since haxr would benefit highly from the HTTP 4k series of performance improvements, is it trivial to make it compatible with the latest library? Hi Jeff, the required changes shouldn't be pervasive nor hard, but if it turns out non-trivial or unappealing in any way, please get in touch. thanks --sigbjorn ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Building GHC under cygwin
Has anyone had any luck building a windows version of GHC under cygwin? I followed the instruction on the building guide to build it under MinGW/msys, which works, but when I try to build it under cygwin instead of msys I get this: cabal-bin.exe: Cannot find the program 'ghc' at '/cygdrive/c/dev/haskell/ghc/ghc-6.10.1/bin/ghc' or on the path make[1]: *** [bootstrapping.conf] Error 1 make[1]: Leaving directory `/cygdrive/c/dev/haskell/ghc/ghc-6.10.1-src/libraries' make: *** [stage1] Error 2 Fairly early in the build process. Looking in that folder, there is a ghc.exe there, but not a ghc. I'm not too familiar with cygwin so I feel like this is something fairly obvious, but I didn't see it covered on the wiki anywhere. A google search indicated that I might have to override the host mode in the configure state but didn't elaborate, and also I'm not sure how to do that. I also want to make sure that I get a *windows* version of GHC out of the build, as the post I found suggested that might not be the case. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] putting SOE 'on the library path'
Michael Mossey wrote: I tried using the -i options to ghc and ghci, but to no avail. It wasn't clear on Windows in what form -i takes its arguments: backslashes as usual for Windows, forward slashes perhaps? How about paths with spaces in them? Does it need quotes around the filename? I tried all combinations. ghc never gave an error indicating there was something wrong with what I typed. It happily accepted all forms of -i. But it never found the libraries. Have you tried starting GHC with -v to see what paths it looks in? Martijn. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Haskell Weekly News: Issue 111 - March 28, 2009
--- Haskell Weekly News http://sequence.complete.org/hwn/20090328 Issue 111 - March 28, 2009 --- Welcome to issue 111 of HWN, a newsletter covering developments in the [1]Haskell community. Announcements CHP package. Neil Brown [2]announced the release of version 1.2.0 of the [3]CHP library (which supports explicit message-passing concurrency in Haskell), with various bug-fixes and a new "clock" synchronisation primitive. smartword 0.0.0.5 Web based flash card for Word Smart I and II vocabularies. Ki Yung Ahn [4]announced the release of [5]smartword 0.0.0.5, a web based flash card system for Word Smart I and II, a popular book series for studying GRE vocabularies. HackMail 0.0 -- Procmail + Monads = Awesome!. Joe Fredette [6]announced his very second Hackage upload, [7]HackMail. Hackmail is a Procmail-alike, though it doesn't (yet) support procmail syntax. It dynamically loads a haskell source file and then sits as a daemon watching a directory for new emails. The source file contains a function which sorts email and delivers it to some directory. FallingBlocks 0.1. Ben Sanders [8]announced [9]fallingblocks, a Tetris clone using SDL. io-capture-0.2 capturing std(out|err) in IO action. Yusaku Hashimoto [10]announced the release of [11]io-capture 0.2, a library to capture stdout and stderr in an IO action. It exports a function capture, which takes an IO action and a String representing the entire input, and returns Strings representing the data written to stdout and stderr. wxAsteroids 1.0. Henk-Jan van Tuyl [12]announced [13]wxAsteroids, a game demonstrating the wxHaskell GUI. The votes are in!. Eelco Lempsink [14]announced that the [15]results of the Haskell logo competition are in! Congratulations to Jeff Wheeler on his winning design. Making videos of your project. Don Stewart [16]described how to create short screencasts showing off your latest awesome Haskell project. WinGhci, a GUI for GHCI on Windows. Pepe Gallardo [17]announced the first release of [18]WinGhci, a simple GUI for GHCI on Windows. It is closely based on WinHugs, and provides similar functionality. hranker: Basic utility for ranking a list of items (e.g. for the logo poll). Robin Green [19]announced [20]hranker, a command-line utility that helps the user rank a list of items (of any type implementing Show, Eq and Ord). The hope is that the code is sufficiently clear that it could also serve as an educational piece of code, especially for people wanting to learn how to use the HCL library. salvia-0.1, salvia-extras-0.1. Sebastiaan Visser [21]announced a new version of [22]Salvia, a lightweight Haskell Web Server Framework. Changes in this release include easier dependencies, some new default handler environments that simplify setting up a server application, support for keep-alive, a great deal of additional documentation, support for Windows, and various cleanup and bug fixes. Haddock 2.4.2. David Waern [23]announced a new release of [24]Haddock, the Haskell documentation tool. This is a bug fix release only, and it's the same version that will ship with GHC 6.10.2, unless any important problems are discovered before the GHC release. Because the .haddock file format has changed, links to previously installed documentation will not work when generating documentation using this version. ansi-terminal, ansi-wl-pprint - ANSI terminal support for Haskell. Max Bolingbroke [25]announced the [26]ansi-terminal and [27]ansi-wl-pprint packages, which allow Haskell programs to produce much richer console output by allowing colorisation, emboldening and so on. Both Unix-like (OS X, Linux) and Windows operating systems are supported (via a pure Haskell ANSI emulation layer for Windows). I/O library for Windows. Felix Martini [28]announced the [29]package, an I/O library for Windows using Windows API functions with I/O completion port support. The main goal of this library is to support Simon Marlow's new Handle API once he has added that to GHC. The library also has a compatibility module for socket functions from the network-bytestring package. Discussion Grouping - Map / Reduce. Günther Schmidt [30]asked about a way to lazily group an unordered list of key/value pairs, leading to some interesting solutions and discussion of preserving laziness. about Haskell code written to be "too smart". Manlio Perillo began an [31]epic discussion about Haskell coding style, idioms, pedagogy, and much, much more. Blog noise [32]Haskell news from the [33]blogosphere. * >>> : [34]I18n and Haskell. Internationalization of Haskell programs
RE: [Haskell-cafe] Conditional compilation
Robin Green wrote: > I am writing some code for citation support in gitit, and all the > #ifdefs I'm using to do conditional compilation are a bit tiresome. > > Suppose you have the requirement that a certain feature of your > software be disable-able at compile time, to avoid having to pull in > certain dependencies (which may not be available on all platforms). > Disabling a feature may entail removing certain fields from certain > constructors (again, to avoid pulling in certain dependencies), and/or > removing certain functions from certain modules. What is the best way > to do this in Haskell? I would parameterise over the functionality (i.e. use type parameters to datatypes and HOF parameters to functions where appropriate), and instantiate the types with () or some useful type as appropriate. Some CPP would still be required but hopefully only at the top level. Ganesh === Please access the attached hyperlink for an important electronic communications disclaimer: http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html === ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] putting SOE 'on the library path'
I tried the -v, but I seem to have fixed my problem. It's working now. I don't know what I changed. Maybe I typed the paths wrong before. Anyway, it does seem to accept (on Windows) paths with backslashes and it accepts spaces in the path if you put quotes around it. I'm still interested, though, in how one "installs" packages or modules so they don't need to be on the path specified by -i. I notice there is a file called 'package.conf', which seems related to this. Martijn van Steenbergen wrote: Michael Mossey wrote: I tried using the -i options to ghc and ghci, but to no avail. It wasn't clear on Windows in what form -i takes its arguments: backslashes as usual for Windows, forward slashes perhaps? How about paths with spaces in them? Does it need quotes around the filename? I tried all combinations. ghc never gave an error indicating there was something wrong with what I typed. It happily accepted all forms of -i. But it never found the libraries. Have you tried starting GHC with -v to see what paths it looks in? Martijn. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Definition of "tail recursive" wrt Folds
Ben Franksen wrote: > Mark Spezzano wrote: > > Just looking at the definitions for foldr and foldl I see that foldl is > > (apparently) tail recursive while foldr is not. > > > > Why? > > > > Is it because foldl defers calling itself until last whereas foldr > > evaluates itself as it runs? > > > > What, strictly speaking, is the definition of ”tail recursive” as opposed > > to just “recursive”? > > An application of some function f inside another function g is in 'tail > position' (or a 'tail call') if the result of applying f is the result of > g. Operationally speaking, calling f is the very last thing g does. Tail > calls can be optimized by a compiler (or interpreter) so that the call does > not use additional stack; that is, the call can be replaced by a jump. > > A function is called ”tail recursive” (as opposed to just “recursive”) if > the recursive call to itself is in tail position. If the compiler performs > tail call optimization, tail recursive functions can work with constant > stack space, similar to a (imperative) loop. > > Looking at a definition of foldl, e.g. > > foldl f z0 xs0 = lgo z0 xs0 where > lgo z [] = z > lgo z (x:xs) = lgo (f z x) xs > > you see that lgo calls itself in tail position, thus is tail recursive. In > contrast, foldr can be defined as > > foldr k z xs = go xs where > go [] = z > go (y:ys) = y `k` go ys > > where you see that the result of go is not the recursive call to go. > Instead, the result is y `k` go ys . Thus, foldr is not tail recursive. > > So, if you are saying that "foldl defers calling itself until last whereas > foldr evaluates itself as it runs" then you got the right idea, I think. > The point is that foldr still needs to do something (namely to apply (y > `k`)) to the result of applying itself. It needs to remember to do so, and > thus the stack grows linearly with the size of the list. Sorry, but that's wrong. It would be right in a strict language. In Haskell, the 'go ys' term is not evaluated straight away; it is instead turned into a suspended evaluation (a "thunk") that is typically stored on the heap. The following discussion is implementation specific, with ghc in mind. Haskell itself has no notion of a stack. In fact both using foldl and using foldr can produce stack overflows: Prelude> foldl (+) 0 [1..10^7] *** Exception: stack overflow Prelude> foldr (+) 0 [1..10^7] *** Exception: stack overflow Let's examine why. First, consider the foldl case. For simplicity, I'll ignore the evaluation of [1..10^7]. The first few evaluation steps are foldl (+) 0 [1,2,3..100] -> lgo 0 [1,2,3..100] -> lgo (0+1) [2,3,4..100] -> lgo ((0+1)+2) [3,4,5..10] None of these steps uses the stack - foldl is indeed tail recursive. However, the ((0+1)+2) is a thunk on the heap. Continuing, -> lgo ((...(0+1)+...)+999) [1000] -> lgo ((...(0+1)+...)+1000) [] -> ((...(0+1)+...)+1000) Now we have to evaluate that huge thunk. This turns out to cause trouble because + (for Integers) is strict. So in order to find x+1000, the code needs the value of x first. And that's where the stack gets involved: the information of the pending addition, (?+1000) is pushed onto the stack, and evaluation proceeds with the first term. Denoting the stack by [[item1, item2, ...]], evaluation continues like this: -> [[(?+1000)]] ((...(0+1)+...)+999) -> [[(?+1000),(?+999)]] The stack will keep growing, until the (0+1) is reached, or the stack overflows. To make things more confusing, ghc has a strictness analyzer that sometimes manages to avoid such a thunk being built up. For an example how strictness helps see foldl' (below). Now for the foldr case. Evaluation in that case looks a bit different: foldr (+) 0 [1,2,3..1000] -> go [1,2,3..1000] -> 1 + go [2,3,4..1000] No stack was used so far; the go [2,3,4..100] is a thunk. Now, as above, we need to add two numbers. And that's where the stack gets involved again: -> [[(1+?)]] go [2,3,4..1000] -> [[(1+?)]] 2 + go [3,4,5..1000] -> [[(1+?),(2+?)]] go [3,4,5..1000] -> [[(1+?),(2+?),(3+?)]] go [4,5,6..1000] and so on, until we reach go [] or the stack overflows. Note that there is no reference to 'go' on the stack at all. If instead of (+), we had a lazy function like (:), the stack would not get involved in this way: foldr (:) [] [1,2,3..1000] -> go [1,2,3..1000] -> 1 : go [2,3,4..1000] Which is in weak head normal form, so evaluation stops here. Later, when other code examines the list, the 'go [2,3,4..1000]' thunk will get evaluated. As a final note, the stack overflow with foldl above is cured by using foldl', which is _strict_ in the accumulator. For reference, foldl' f z0 xs0 = lgo z0 xs0 where lgo z [] = z lgo z (x:xs) = let z' = f z x in z' `seq` lgo z' xs foldl' (+) 0 [1,2,3..100] -> lgo
Re: [Haskell-cafe] putting SOE 'on the library path'
Am Samstag 28 März 2009 18:46:39 schrieb Michael Mossey: > I tried the -v, but I seem to have fixed my problem. It's working now. I > don't know what I changed. Maybe I typed the paths wrong before. Anyway, > it does seem to accept (on Windows) paths with backslashes and it > accepts spaces in the path if you put quotes around it. > > I'm still interested, though, in how one "installs" packages or modules > so they don't need to be on the path specified by -i. I notice there is > a file called 'package.conf', which seems related to this. Use Cabal. a) module A.B.C should be in file A/B/C.hs, where A is a direct subdirectory of the top of your source-tree. b) above your source tree, place the file packagename.cabal, where you tell Cabal the name of the package, its version number, licence, build depends, exposed modules and other stuff (read http://www.haskell.org/haskellwiki/How_to_write_a_Haskell_program and http://www.haskell.org/ghc/docs/latest/html/Cabal/authors.html for details) and a Setup.hs, which can often consist of the two lines import Distribution.Simple main = defaultMain If you want to install a package written by somebody else, that should already be present in the bundle you got. Then runghc ./Setup.(l)hs configure with the appropriate options, whether to allow dependencies to be satisfied from the user package database, whether to install per user or globally, install prefix, whether to build the library also for profiling, ... runghc ./Setup.hs build optionally runghc ./Setup.hs haddock runghc ./Setup.hs install That registers the package in the package database, and from then on, it will be automatically found by ghc --make. If you have the cabal executable, it's even easier, just cabal install packagename if it's somebody else's package, that automatically takes care of its (Haskell) dependencies, downloads and installs them if necessary; or simply cabal install in the appropriate directory if it's a package you have just written. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] ByteStrings, FFI, iovec
Folks, I'm putting together an FFI layer to a library which uses writev for its IO, and so expects an array of iovec structs as an argument to one of its calls. for more information, check out "man writev"[1]. Since I'm already using Data.Binary, the writev call almost sounds like a perfect match for lazy bytestrings, so I thought I'd put up what I have for comments. I'm using c2hs[2]. The recursive nesting of 'withForeignPtr' put me off at first, but I thought I'd at least give it a shot. I haven't put this into practice yet, so it could be buggy. I'm more looking for comments on the approach. Antoine [1] http://linux.die.net/man/3/writev [2] http://www.cse.unsw.edu.au/~chak/haskell/c2hs/ Foreign/IOVec.chs > -- -*-haskell-*- module Foreign.IOVec (IOVec() ,withLazyByteString ) where import C2HS import qualified Data.ByteString as S import qualified Data.ByteString.Internal as S import qualified Data.ByteString.Lazy as L #include #c typedef struct iovec hs_iovec; #endc {#pointer *hs_iovec as IOVec newtype#} -- | This is intended for calling into something like writev. -- But I suppose that nothing stops you from calling into -- readv and blowing away the passed-in ByteString. withLazyByteString :: L.ByteString -> (IOVec -> Int -> IO b) -> IO b withLazyByteString b f = let bs = L.toChunks b num = length bs in allocaBytes (num * {#sizeof hs_iovec#}) $ \vecAry -> go vecAry 0 bs where go vecAry off [] = f (IOVec vecAry) off go vecAry off (b:bs) = let vec = vecAry `plusPtr` (off * {#sizeof hs_iovec#}) (fptr, bsOff, bsLen) = S.toForeignPtr b in withForeignPtr fptr $ \bsPtr -> do {#set hs_iovec->iov_base#} vec $ castPtr $ bsPtr `plusPtr` bsOff {#set hs_iovec->iov_len#} vec $ cIntConv bsLen go vecAry (off+1) bs < ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Exception handling in numeric computations
On Sat, 28 Mar 2009, Ketil Malde wrote: Jonathan Cast writes: i.e., that application's file decoding result should be an Either type that anticipates that the file encoding may be invalid. This is pretty standard, I thought. Do people write Haskell file input methods that are undefined (`throw exceptions') on invalid inputs (e.g., do people use read to parse input from users or the file system[1])? I often write parsers that either run successfully, or abort with an exception. I could of course return an Either type, but that would mean the whole file would need to be parsed before any results could be returned at all - which is a showstopper for streaming processing of large files. If you need a lazy parser with error reporting, I suggest Control.Monad.Exception.Asynchronous from the explicit-exception package. Since at least my files are typically machine generated, a parse error is either a programmer error in my parser, a programmer error in the generating program, or an operator error (viz. the user running the program on a completely different file type). That's no excuse. You can never rely on proper file formatting since the user alter the file while you process it, delete it, or remove the disk it is stored on. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Exception handling in numeric computations
On Sat, 28 Mar 2009, Gregory Petrosyan wrote: On Sat, Mar 28, 2009 at 10:53 AM, Ketil Malde wrote: So the difference between an exception or an error type is mainly what you intend to do about it. There's no point in wrapping divisions in Maybe unless you actually are able to do something useful to recover from a zero denominator. That is exactly the point I was trying to make. When I write a code, I can't say in advance, in what way it will be used. So, for dealing with errors, I have to choose one way or another, mostly without that knowledge. When I'm using e.g. C++, it's easy: something like mantra "when in doubt, throw an exception" :-) combined with RAII, works good (but not ideal, of course). In C++ you can either throw exceptions or return integer codes in order to show exceptional situations. An error is, if you write in non-allocated memory areas. So, I'll ask again: when I program in Haskell, what mechanism should I use? I think this question was enough answered in general here in the thread and on the Wiki pages. I think we can better discuss concrete situations. Do you have a function in your code where you are uncertain whether to use error or exceptions? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Exception handling in numeric computations
On Sat, 28 Mar 2009, John Lato wrote: From: Donn Cave I have never felt that I really understood that one. Honestly, me neither, until recently. I'm only barely starting to understand it, and I do think there's a great deal of overlap. Even if an error is a bug that can be fixed by the programmer, certain exceptional situations can also be fixed by the programmer by handling the exception, even if they can't be detected in advance. For example? Btw. not handling an exception is an error. I will also guess if the file is unreadable because of an external I/O problem like no read access to file or filesystem, you would similarly expect this to be treated like that - I mean, ideally, e.g., hGetLine :: Handle -> IO (Either IOError String) Not necessarily, but possibly. The big difference, of course, is that decoding can be a pure operation, while reading never is. I personally wouldn't mind if hGetLine had the type you give. The way I see it, there are two advantages to exceptions in this case. The first is that it's very easy for exceptions to trickle up and be handled at a higher level. The second 'advantage' is that the programmer doesn't need to explicitly handle exceptions, whereas an Either would require at least a pattern match to use the resulting value. I'm afraid there is some confusion about what we mean with "exception". Do you only mean the thing that is silently handled in the IO monad? Is Left in Either an exception for you, too? In explicit-exception I call the corresponding constructor Exception, because that's what it is used for. I like to call all those things exceptions, because they are intended for the same purpose: Signalling exceptional situations that we cannot avoid in advance but that must be handled when they occur. You can use IO and its exceptions, I call them IO exceptions. It does not show in its types that and which exceptions can occur. Some people consider this an advantage, I consider this an disadvantage. You can use error codes or Either or even better Exceptional from the explicit-exception package, and Haskell is strong enough to treat these like exceptions in C++/Java/Modula-3 etc. because you can use their monad transformer variants ErrorT and ExceptionalT respectively. Those monad transformers allow automatical termination of a series of actions once an exceptional result is obtained. But since ErrorT and ExceptionalT are burned into the types, you cannot miss to handle them. So the most convenient type for hGetLine would be hGetLine :: Handle -> ErrorT IOError IO String ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] ByteStrings, FFI, iovec
Quoth Antoine Latter , > ... I haven't put this into practice yet, Really? haven't even tried it? Well, this is just a comment, since I'm not a qualified guru or anything, but I have been using the foreign array functions to do some of the things you're doing by hand, which I suppose in your case means you would need to make a Storable instance for IOVec. Maybe c2hs does this for you? dunno. That done, I would have something like withIOVec :: [P.ByteString] -> ((Ptr IOVec) -> CInt -> IO a) -> IO a withIOVec ss f = wv ss [] where wv [] si = withArray (reverse si) $ \ pa -> f pa (length si) wv (a:ax) si = P.useAsCStringLen a $ \ (p, n) -> wv ax ((IOVec p n):si) writev fd ss = withIOVec ss $ c_writev fd Donn ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] ByteStrings, FFI, iovec
Hi Antoine, On Sat, Mar 28, 2009 at 7:59 PM, Antoine Latter wrote: > Folks, > > I'm putting together an FFI layer to a library which uses writev for > its IO, and so expects an array of iovec structs as an argument to one > of its calls. for more information, check out "man writev"[1]. > > Since I'm already using Data.Binary, the writev call almost sounds > like a perfect match for lazy bytestrings, so I thought I'd put up > what I have for comments. I'm using c2hs[2]. The recursive nesting of > 'withForeignPtr' put me off at first, but I thought I'd at least give > it a shot. I haven't put this into practice yet, so it could be > buggy. I'm more looking for comments on the approach. network-bytestring [1] includes a FFI wrapper for iovecs that might be useful to look at. 1. http://github.com/tibbe/network-bytestring/tree/master Cheers, Johan ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] [ANN] salvia-0.1, salvia-extras-0.1
Thanks for the massive update! Is there a new version of Orchid coming along? On 3/22/09, Sebastiaan Visser wrote: > Hi all, > > I am pleased to announce a new version of Salvia, the lightweight > Haskell Web Server Framework with modular support for serving static > files, directories indices, default error responses, connection > counting and logging, HEAD and PUT requests, keep-alives, custom > banner printing, default handler environments for parsing request and > printing responses, dispatching based on request methods, URI, paths > and filename extension, URI rewriting and redirection, virtual > hosting, cookie, session and user management and more... > > Changes since previous version: > > - Some more advanced, non-fundamental handlers have been moved to > their own package salvia-extras. This helps pruning the dependencies a > bit. > - The package now has some default handler environments that simplify > setting up a server application. > - The server now has support for keep-alive, significantly increasing > the performance. > - The library functions are now almost fully documented instead of no- > documentation-at-all in the previous versions. > - Salvia now also works on windows (I heard). > - Lots of code cleanups throughout the code. > - Lots of minor bug fixes. > > To install: use cabal. > > Thanks to the people that helped me with suggestions and bug-reports! > > -- > Sebastiaan. > ___ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe > -- Regards, Paul Liu Yale Haskell Group http://www.haskell.org/yale ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Haskell Weekly News: Issue 111 - March 28, 2009
> > conal: Recursion is the goto of functional programming > BTW, I certainly did not mean to take credit for this wonderful quote. I don't know the origin. I first heard it from Erik Meijer. - Conal ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Need help to edit the Haskell script in Winhugs and documentation.
On Sat, Mar 28, 2009 at 05:07:37PM +, RAJESH DALSANIYA wrote: > * * > > *Both sections relate to the case study: Index for a document of > text.* Hi Rajesh, this looks a lot like a homework assignment. Please see http://haskell.org/haskellwiki/Homework_help Many people are happy to help, but no one is going to do your homework for you, and you will probably get much better results if you ask short, specific questions, with evidence that you have put some effort into working it out yourself first. -Brent ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Haskell Weekly News: Issue 111 - March 28, 2009
This paper from 1994: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.36.5611 begins point 1.1 with exactly that sentence. It doesn't seem to be quoted there, so one can assume this is the original source of that sentence. I'm not sure dough. Regards Christopher Skrzętnicki 2009/3/28 Conal Elliott : >> conal: Recursion is the goto of functional programming > > BTW, I certainly did not mean to take credit for this wonderful quote. I > don't know the origin. I first heard it from Erik Meijer. > > - Conal > > > ___ > 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] Haskell Weekly News: Issue 111 - March 28, 2009
The quote has been around since at least the early 80s, but I don't know who it's from. 2009/3/28 Krzysztof Skrzętnicki : > This paper from 1994: > http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.36.5611 > begins point 1.1 with exactly that sentence. It doesn't seem to be > quoted there, so one can assume this is the original source of that > sentence. I'm not sure dough. > > Regards > > Christopher Skrzętnicki > > 2009/3/28 Conal Elliott : >>> conal: Recursion is the goto of functional programming >> >> BTW, I certainly did not mean to take credit for this wonderful quote. I >> don't know the origin. I first heard it from Erik Meijer. >> >> - Conal >> >> >> ___ >> 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 > ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] type Rational and the % operator
Hi, I've been away from Haskell Land for a while, but I think the function cf below, given a list of Ints should calculate a continuous fraction. I'm using Hugs 98 and get errors when loading and also when trying to use the % operator at the command prompt (see below). What must I do to get this to work? Michael === cf :: [Int] -> Rational cf [] = 0 cf (x:[]) = 1 % x cf (x:xs) = x + (1 % cf xs) Hugs> :load cf.hs ERROR "cf.hs":2 - Undefined variable "%" Hugs> Hugs> 1 % 5 ERROR - Undefined variable "%" Hugs> ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] type Rational and the % operator
I believe it's import Data.Ratio I found it on hayoo... http://holumbus.fh-wedel.de/hayoo/hayoo.html?query=%25#0 :%25 -- Duane Johnson On Mar 28, 2009, at 7:39 PM, michael rice wrote: Hi, I've been away from Haskell Land for a while, but I think the function cf below, given a list of Ints should calculate a continuous fraction. I'm using Hugs 98 and get errors when loading and also when trying to use the % operator at the command prompt (see below). What must I do to get this to work? Michael === cf :: [Int] -> Rational cf [] = 0 cf (x:[]) = 1 % x cf (x:xs) = x + (1 % cf xs) Hugs> :load cf.hs ERROR "cf.hs":2 - Undefined variable "%" Hugs> Hugs> 1 % 5 ERROR - Undefined variable "%" Hugs> ___ 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] type Rational and the % operator
That fixed the problem with recognizing the % operator, but what's it trying to tell me now? Michael import Data.Ratio cf :: [Integer] -> Rational cf (x:xs) = (toRational x) + (1 % (cf xs)) cf (x:[]) = toRational x cf [] = toRational 0 Data.Ratio> :load cf.hs ERROR "cf.hs":3 - Type error in application *** Expression : toRational x + 1 % cf xs *** Term : toRational x *** Type : Ratio Integer *** Does not match : Ratio (Ratio Integer) --- On Sat, 3/28/09, Duane Johnson wrote: From: Duane Johnson Subject: Re: [Haskell-cafe] type Rational and the % operator To: "michael rice" Cc: haskell-cafe@haskell.org Date: Saturday, March 28, 2009, 9:44 PM I believe it's import Data.Ratio I found it on hayoo... http://holumbus.fh-wedel.de/hayoo/hayoo.html?query=%25#0:%25 -- Duane Johnson On Mar 28, 2009, at 7:39 PM, michael rice wrote: Hi, I've been away from Haskell Land for a while, but I think the function cf below, given a list of Ints should calculate a continuous fraction. I'm using Hugs 98 and get errors when loading and also when trying to use the % operator at the command prompt (see below). What must I do to get this to work? Michael === cf :: [Int] -> Rational cf [] = 0 cf (x:[]) = 1 % x cf (x:xs) = x + (1 % cf xs) Hugs> :load cf.hs ERROR "cf.hs":2 - Undefined variable "%" Hugs> Hugs> 1 % 5 ERROR - Undefined variable "%" Hugs> ___ 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] type Rational and the % operator
On 2009 Mar 28, at 22:36, michael rice wrote: import Data.Ratio cf :: [Integer] -> Rational cf (x:xs) = (toRational x) + (1 % (cf xs)) cf (x:[]) = toRational x cf [] = toRational 0 Data.Ratio> :load cf.hs ERROR "cf.hs":3 - Type error in application *** Expression : toRational x + 1 % cf xs *** Term : toRational x *** Type : Ratio Integer *** Does not match : Ratio (Ratio Integer) Your function cf produces a Rational (Ratio Int); you're using it in the denominator of another Ratio, which makes that Ratio's type Ratio (Ratio Int). This is almost certainly not what you intended, but I couldn't say what you actually want. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allb...@kf8nh.com system administrator [openafs,heimdal,too many hats] allb...@ece.cmu.edu electrical and computer engineering, carnegie mellon universityKF8NH PGP.sig Description: This is a digitally signed message part ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] type Rational and the % operator
I may be missing something here, but this is what I intended. An expression of the form 1 a1 + -- 1 a2 + -- 1 a3 + -- a4 + ... Where the ai's are positive integers is called a continued fraction. Function cf should take [1,2,6,5] to produce 1 1 + - 1 2 + - 1 6 + -- 5 Michael --- On Sat, 3/28/09, Brandon S. Allbery KF8NH wrote: From: Brandon S. Allbery KF8NH Subject: Re: [Haskell-cafe] type Rational and the % operator To: "michael rice" Cc: "Brandon S. Allbery KF8NH" , "Duane Johnson" , haskell-cafe@haskell.org Date: Saturday, March 28, 2009, 10:39 PM On 2009 Mar 28, at 22:36, michael rice wrote:import Data.Ratio cf :: [Integer] -> Rational cf (x:xs) = (toRational x) + (1 % (cf xs)) cf (x:[]) = toRational x cf [] = toRational 0 Data.Ratio> :load cf.hs ERROR "cf.hs":3 - Type error in application *** Expression : toRational x + 1 % cf xs *** Term : toRational x *** Type : Ratio Integer *** Does not match : Ratio (Ratio Integer) Your function cf produces a Rational (Ratio Int); you're using it in the denominator of another Ratio, which makes that Ratio's type Ratio (Ratio Int). This is almost certainly not what you intended, but I couldn't say what you actually want. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allb...@kf8nh.comsystem administrator [openafs,heimdal,too many hats] allb...@ece.cmu.eduelectrical and computer engineering, carnegie mellon university KF8NH ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] type Rational and the % operator
Just use "/" for division. % is for construction of rationals from the "underlying" numeric type. For example, instead of "toRational x" you can write "x % 1". -- ryan 2009/3/28 michael rice : > I may be missing something here, but this is what I intended. > > An expression of the form > > 1 > a1 + -- > 1 > a2 + -- > 1 > a3 + -- > a4 + ... > > Where the ai's are positive integers is called > a continued fraction. > > Function cf should take [1,2,6,5] to produce > > 1 > 1 + - > 1 > 2 + - > 1 > 6 + -- > 5 > > Michael > > > --- On Sat, 3/28/09, Brandon S. Allbery KF8NH wrote: > > From: Brandon S. Allbery KF8NH > Subject: Re: [Haskell-cafe] type Rational and the % operator > To: "michael rice" > Cc: "Brandon S. Allbery KF8NH" , "Duane Johnson" > , haskell-cafe@haskell.org > Date: Saturday, March 28, 2009, 10:39 PM > > On 2009 Mar 28, at 22:36, michael rice wrote: > > import Data.Ratio > cf :: [Integer] -> Rational > cf (x:xs) = (toRational x) + (1 % (cf xs)) > cf (x:[]) = toRational x > cf [] = toRational 0 > > Data.Ratio> :load cf.hs > ERROR "cf.hs":3 - Type error in application > *** Expression : toRational x + 1 % cf xs > *** Term : toRational x > *** Type : Ratio Integer > *** Does not match : Ratio (Ratio Integer) > > Your function cf produces a Rational (Ratio Int); you're using it in the > denominator of another Ratio, which makes that Ratio's type Ratio (Ratio > Int). This is almost certainly not what you intended, but I couldn't say > what you actually want. > -- > brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allb...@kf8nh.com > system administrator [openafs,heimdal,too many hats] allb...@ece.cmu.edu > electrical and computer engineering, carnegie mellon university KF8NH > > > > ___ > 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] type Rational and the % operator
Still the same problem. The book says the answer is supposed to be an exact 98/67. Michael import Data.Ratio cf :: [Integer] -> Rational cf (x:xs) = (x % 1) + (1 % (cf xs)) cf (x:[]) = x % 1 cf [] = 0 % 1 Hugs> :load cf.hs ERROR "cf.hs":3 - Type error in application *** Expression : x % 1 + 1 % cf xs *** Term : x % 1 *** Type : Ratio Integer *** Does not match : Ratio (Ratio Integer) Data.Ratio> --- On Sat, 3/28/09, Ryan Ingram wrote: From: Ryan Ingram Subject: Re: [Haskell-cafe] type Rational and the % operator To: "michael rice" Cc: "Brandon S. Allbery KF8NH" , haskell-cafe@haskell.org Date: Saturday, March 28, 2009, 11:17 PM Just use "/" for division. % is for construction of rationals from the "underlying" numeric type. For example, instead of "toRational x" you can write "x % 1". -- ryan 2009/3/28 michael rice : > I may be missing something here, but this is what I intended. > > An expression of the form > > 1 > a1 + -- > 1 > a2 + -- > 1 > a3 + -- > a4 + ... > > Where the ai's are positive integers is called > a continued fraction. > > Function cf should take [1,2,6,5] to produce > > 1 > 1 + - > 1 > 2 + - > 1 > 6 + -- > 5 > > Michael > > > --- On Sat, 3/28/09, Brandon S. Allbery KF8NH wrote: > > From: Brandon S. Allbery KF8NH > Subject: Re: [Haskell-cafe] type Rational and the % operator > To: "michael rice" > Cc: "Brandon S. Allbery KF8NH" , "Duane Johnson" > , haskell-cafe@haskell.org > Date: Saturday, March 28, 2009, 10:39 PM > > On 2009 Mar 28, at 22:36, michael rice wrote: > > import Data.Ratio > cf :: [Integer] -> Rational > cf (x:xs) = (toRational x) + (1 % (cf xs)) > cf (x:[]) = toRational x > cf [] = toRational 0 > > Data.Ratio> :load cf.hs > ERROR "cf.hs":3 - Type error in application > *** Expression : toRational x + 1 % cf xs > *** Term : toRational x > *** Type : Ratio Integer > *** Does not match : Ratio (Ratio Integer) > > Your function cf produces a Rational (Ratio Int); you're using it in the > denominator of another Ratio, which makes that Ratio's type Ratio (Ratio > Int). This is almost certainly not what you intended, but I couldn't say > what you actually want. > -- > brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allb...@kf8nh.com > system administrator [openafs,heimdal,too many hats] allb...@ece.cmu.edu > electrical and computer engineering, carnegie mellon university KF8NH > > > > ___ > 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] type Rational and the % operator
2009/3/28 michael rice > Still the same problem. The book says the answer is supposed to be an exact > 98/67. > > Michael > > import Data.Ratio > cf :: [Integer] -> Rational > cf (x:xs) = (x % 1) + (1 % (cf xs)) > cf (x:[]) = x % 1 > cf [] = 0 % 1 Use (/) for division: cf :: [Integer] -> Rational cf (x:xs) = toRational x + 1 / cf xs cf [x] = toRational x cf [] = 0 Luke ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] type Rational and the % operator
This seems to work: import Data.Ratio cf :: [Integer] -> Rational cf (x:[]) = toRational x cf (x:xs) = toRational x + (1 / cf xs) Hugs> :load cf.hs Main> cf [1,2,6,5] 98 % 67 Main> cf [2,3] 7 % 3 Main> Thanks, guys! Michael --- On Sun, 3/29/09, Luke Palmer wrote: From: Luke Palmer Subject: Re: [Haskell-cafe] type Rational and the % operator To: "michael rice" Cc: "Ryan Ingram" , haskell-cafe@haskell.org Date: Sunday, March 29, 2009, 12:23 AM 2009/3/28 michael rice Still the same problem. The book says the answer is supposed to be an exact 98/67. Michael import Data.Ratio cf :: [Integer] -> Rational cf (x:xs) = (x % 1) + (1 % (cf xs)) cf (x:[]) = x % 1 cf [] = 0 % 1 Use (/) for division: cf :: [Integer] -> Rationalcf (x:xs) = toRational x + 1 / cf xscf [x] = toRational x cf [] = 0 Luke ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe