[Haskell-cafe] Foldr tutorial, Inspired by Getting a Fix from a Fold
I hope the following doesn't come across as condescending... The recent issue of The Monad Reader has generated some excitement, mostly to do with the time travel article. I, however, would like to discuss a simpler solution to implementing dropWhile with foldr, which is discussed in the first article of TMR. When I was taught about folds, I was told that foldr starts at the end of the list and then builds up the result from there. But this turns out to be an unhelpful way of thinking. It's better to say foldr starts at the beginning of the list, just like foldl. But how does this work? To see, we'll start by implementing a simpler function than dropWhile, one which determines whether we should cons an element onto a list or just return the list. dropHead p x xs = if p x then xs else x:xs Note that I've funnily given a separate arg to the head of the list and to its tail. That's not a typo and later we will see why. Now let's apply dropHead to each element of the list [1, 2, 3, 4, 5]: map (dropHead p) [1, 2, 3, 4 ,5] = [dropHead p 1, dropHead p 2, dropHead p 3, dropHead p 4, dropHead p 5] Now we need to figure out where to get the second argument for each of the dropHead applications. I know! If a call to dropHead comes before another call to dropHead, we'll pass the result of the second call as the second arg of the first. And if there are no calls after a call to dropHead, we'll pass our base case as the second arg to that call. Let's have a look: threadResults basec [] = basec threadResults basec [f:fs] = f $ threadResults basec fs threadResults [] [dropHead p 1 ...] = dropHead p 1 $ dropHead p 2 $ dropHead p 3 $ dropHead p 4 $ dropHead p 5 [] Let's see it in action! First let's let our predicate determine whether a value is less than three. dropHead ( 3) 5 [] = if ( 3) 5 then [] else 5:[] = [5] dropHead ( 3) 4 [5] = if ( 3) 4 then [5] else 4:[5] = [4, 5] dropHead ( 3) 3 [4, 5] = if ( 3) 3 then [4, 5] else 3:[4, 5] = [3, 4, 5] dropHead ( 3) 2 [3, 4, 5] = if ( 3) 2 then [3, 4, 5] else 2:[3, 4, 5] = [3, 4, 5] dropHead ( 3) 1 [3, 4, 5] = if ( 3) 1 then [3, 4, 5] else 1:[3, 4, 5] = [3, 4, 5] = [3, 4, 5] Do you now see how foldr works? It starts at the beginning of the list and applies the function you gave it to the first element of the list. Then to get the second argument of the first call, it applies your function to the second element of the list and passes the result of that to the first call. In other words, it /recurs/ on the next element of the list. When we finally get to the end, the base case is passed to the final call of your function and it returns, and all the previous calls return building their results from the calls after. So in other words to implement dropWhile with foldr merely requires: dropHead p x xs = if p x then xs else x:xs dropWhile p l = foldr (dropHead p) [] l Or even simpler: dropWhile p = foldr (\x l' - if p x then l' else x:l') [] take 3 $ dropWhile ( 5) [1..] = [5, 6, 7] No abtruse machinery required! __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell] [Proposal] Data type declarations are implicitly moduled
Hi, I'm not sure if something like this has been proposed already but I thought I'd bring it up. The idea is to allow multiple data declarations to share constructors by having them be implicitly declared inside a module. So, for example, data Dir = Left | Right deriving Show data Arrived = Arrived | Left deriving Show main = do print Dir.Left print Arrived.Left will work just fine. Nothing is required by this proposal that isn't already done (with the exception of adding syntactic support to the compiler of course). The above can be accomplished today by simply putting Dir and Arrived into their own modules and then importing them. There is a question of do we provide some means of abbreviating these implicit module names. If this is considered desirable, one way of accomplishing this is to add an as-clause to the data declaration syntax, which will function similary to where-clauses and deriving syntax. Ex, data Dir = Left | Right as D deriving Show This will effectively be the same as declaring Dir in its own module and then importing it using as syntax. __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
Re: [Haskell-cafe] pleac, examples, etc
--- brad clawsie [EMAIL PROTECTED] wrote: it would be great if some of the more informed posters here took a stab at filling in http://pleac.sourceforge.net/pleac_haskell/index.html a neat site for cookbook-style problem solving What I've always found funny about pleac is that none of the examples are actually Haskell, but some weird Haskell-with-oo-features. Does anyone know what language it is? Here are some examples: password = [1..8].mapM (\_ - rand (0, chars.length -1) (chars!)) -- in haskell, regexp are first class, then can be appended, sometimes easier to read m0' s = (s ==~ dec_number) __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] a rant from a stoned suicidal person
i want to start off by apologizing. im lonely. i dont know anyone. anyone at all who is liek me. im different. everyone else is content to just watch tv all the time and then on weekends go and get drunk. im not. i am not threatening anything here when i say waht i am about to say. i know that waht i am about to say is not appropriate for haskell-cafe. i know this. but this needs to be said. im going to kill myself not right now. no you dont need to do anything. this is a plea. im begging you. ok see here im on my knees. my not-stoned self is going to hate me for this. but he needs the help and i know hes going to forgive me. because im him:) isnt that funny? i have no friends. none at all. im surrounded by motherfucking idiots *IDIOTS* i dont hate them. ok, im lying i hate them. i hate you. i hate this mothierfucking list. all i want is a goddamned friend who is *like me* i like thinking about shit. i like philosophy and math and all that stuff. dont suggest going to college. im sorry but ive found this out the hard way. im a loser. im a motherfucking loser. im too lazy. i have difficulty speling nad pronoundni big words. i do drugs. i know everyone says drugs are bad, but i like them. deal with it however you wnat. sned me to jail if you feel the need to. because i dont care anymroe do you hera that? I DONT FUCKING GIvE A ShIT ANYMORE im going to kill myself. i must. do you why i must? because im going insane. im surrounded by people but im completey alone. I HAVE NO IONE TO TAlK TO I AM LOSER I MUST DIE because im a loser so i am sendign this mesage to this list even though none of you care. none of you wnat ot deal with my problems. thats fine. i respect that. but i have noone and i cant find anyone and so i am begging PLEASE PUT ME OUT OF MY FUCKI*NG MISERY SHOOT ME * chris is crying * so anyways i am sorry for bothering you with this incoherent message but hers your mesage for the day FUCK YOU! ___ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell] Re: Data.Set whishes
Alastair Reid [EMAIL PROTECTED] wrote: Haskell's module system provides a way for a module to merge multiple modules into one but provides no way to eliminate any ambiguities this may create. If we want to be able to use names like 'create' instead of 'createFont', we need to change the module system. The obvious fix would have some of the flavour of the ML module system where a module can export a structured list of names instead of exporting a flat list of names. i'm not familiar with ml's module system so i don't know if my suggestion is the same or similar, but could a good solution be to allow modules to be exported as qualified modules? for example module Graphics ( module Font qualified, module Color qualified, module Window qualified, ... ) where ... chris moline ___ Haskell mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell
[OT[ proving programs for novices
hi. this isnt a haskell question but i am hoping you will forgive it. i was wondering where i could find books/websites/tutorials on how to construct proofs and how to prove programs. preferably books that are aimed at novices with some programming experience but little math/logic experience. i have paul hudaks hsoe book which has some material on proving properties of programs. sincerely chris moline ___ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe
trying to compile a cgi library from hugs
hi. since my webhoster doesnt have haskell installed i have decided to use compiled cgi programs instead. since there doesnt seem to be a cgi library for ghc i was hoping to port the library i found at http://www.pms.informatik.uni-muenchen.de/mitarbeiter/panne/haskell_libs/CGI.html the library (CGI.lhs) seems to compile fine. but when i go to compile the programs i get the following [04:13pm 0.13 0.15 0.16 ~/CGI]$ mv Counter.cgi Counter.lhs Counter.cgi - Counter.lhs [04:14pm 0.11 0.15 0.16 ~/CGI]$ ghc Counter.lhs compilation IS NOT required Counter.o: In function `__stginit_Main': Counter.o(.text+0x16): undefined reference to `__stginit_CGI' Counter.o: In function `Main_main_srt': Counter.o(.text+0x288): undefined reference to `CGI_h1_closure' Counter.o(.text+0x2a8): undefined reference to `CGI_submit_closure' Counter.o(.text+0x2ac): undefined reference to `CGI_gui_closure' Counter.o(.text+0x2b8): undefined reference to `CGI_page_closure' Counter.o(.text+0x2c0): undefined reference to `CGI_zdfShowCgiOut_closure' Counter.o(.text+0x2c4): undefined reference to `CGI_zdfMimeHTML_closure' Counter.o(.text+0x2c8): undefined reference to `CGI_wrapper_closure' Counter.o: In function `s13m_entry': Counter.o(.text+0x35e): undefined reference to `CGI_h1_closure' Counter.o: In function `s13Z_entry': Counter.o(.text+0x689): undefined reference to `CGI_submit_closure' Counter.o: In function `s144_entry': Counter.o(.text+0x6fe): undefined reference to `CGI_gui_closure' Counter.o: In function `s14f_entry': Counter.o(.text+0x7e2): undefined reference to `CGI_page_closure' Counter.o: In function `s14v_ret': Counter.o(.text+0x81a): undefined reference to `CGI_Content_con_info' Counter.o: In function `s13d_entry': Counter.o(.text+0x898): undefined reference to `CGI_zdfMimeHTML_closure' Counter.o(.text+0x89d): undefined reference to `CGI_zdfShowCgiOut_closure' Counter.o: In function `Main_main_entry': Counter.o(.text+0x91e): undefined reference to `CGI_wrapper_closure' [04:14pm 0.15 0.15 0.16 ~/CGI]$ i do not know what is happening here. any hints on how to proceed? sincerly, chris moline ___ Glasgow-haskell-users mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Re: trying to compile a cgi library from hugs
On Wed, 03 Jul 2002 00:31:25 +0200, Wolfgang Thaller [EMAIL PROTECTED] wrote: Your invocation of the ghc command is telling GHC to just compile Counter.lhs to Counter.o and then create an executable (link) with just Counter.o, not CGI.o. The linker then complains about undefined references to the CGI module. Solution 1: GHC works just like an ordinary UNIX-style C compiler. Specify CGI.o on the command line. Solution 2: use ghc --make Counter.lhs GHC will automatically look for all required modules and compile them if necessary (for GHC versions = 5). ah, thanks. if anyone wants to know the cgi library works without any changes except youll have to change the .cgi's to .lhs so ghc will handle them right. sincerly, chris moline ___ Glasgow-haskell-users mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
compiling libraries with ghc
hi, i wrote some utilities to make my coding a little easier and i put them in some libraries. then to use them i made a package conf file and added it with ghc-pkg -a. i then tried compiling them with ghc -package-name extensions -c Foo.hs but i am not sure i am compiling it right as i get linker errors of the form /usr/libexec/elf/ld: cannot find -lDirectoryAndExtensions whenever i try to compile a program that uses my libraries. i can fix this by copying all the .o files to libFoo.a files. what is the right way to compile libraries? sincerly, chris moline ___ Glasgow-haskell-users mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
problems figuring out what the type system is telling me
hi. i am really stuck on this problem and i cant seem to figure it out and so i am posting this in the hopes someone will help me. i realize you prolly get this kind of question alot but the wiki and the papers i have been reading arent helping to clarify things. i am trying to write a little utility to make it easier to get rid of ports on my machine. the function that is giving me trouble is getDepends. it takes the name of a port and opens the appropriate +CONTENTS file and returns the dependencies it finds. here is the function and its helper. getDepends :: String - [String] getDepends p = do handle - openFile (portsDir ++ p) ReadMode fetchDepends handle fetchDepends :: Handle - [String] fetchDepends handle = do l - hGetLine handle e - hIsEOF handle case (not e) of -- ifs keep giving indent errors so ill just use case True - case (matchRegex (mkRegex ^@pkgdep) l) of Just [a] - [drop 8 l] ++ (fetchDepends handle) _ - fetchDepends handle False - here is ghci's error messages. Compiling Pheobe ( Phoebe.hs, interpreted ) Phoebe.hs:19: Couldn't match `[]' against `IO' Expected type: [t] Inferred type: IO Handle In the application `openFile (portsDir ++ p) ReadMode' In a 'do' expression pattern binding: handle - openFile (portsDir ++ p) ReadMode Phoebe.hs:24: Couldn't match `[]' against `IO' Expected type: [t] Inferred type: IO String In the application `hGetLine handle' In a 'do' expression pattern binding: l - hGetLine handle Failed, modules loaded: none. could someone be so kind as to explain what the problem is? sincerly, chris moline ___ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: problems figuring out what the type system is telling me
alternative: [drop 8 l] ++ (show (fetchDepends handle)) great another error message that is meaningless to me. one last attempt. -- try adding the type decs back into the above code getDepends :: String - [String] getDepends p = do handle - openFile (portsDir ++ p ++ /+CONTENTS) ReadMode fetchDepends handle fetchDepends :: Handle - [String] fetchDepends handle = do l - hGetLine handle e - hIsEOF handle case (not e) of True - case (matchRegex (mkRegex ^@pkgdep) l) of Just _ - [drop 8 l] ++ (show (fetchDepends handle)) _ - fetchDepends handle False - [] Phoebe.hs:19: Couldn't match `[]' against `IO' Expected type: [t] Inferred type: IO Handle In the application `openFile (portsDir ++ (p ++ /+CONTENTS)) ReadMode' In a 'do' expression pattern binding: handle - openFile (portsDir ++ (p ++ /+CONTENTS)) ReadMode Phoebe.hs:24: Couldn't match `[]' against `IO' Expected type: [t] Inferred type: IO String In the application `hGetLine handle' In a 'do' expression pattern binding: l - hGetLine handle i am back to the old message none the wiser. please help me. i am going insane. sincerly, chris moline ___ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe