Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://www.haskell.org/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: Pattern matching over functions (Brandon Allbery) 2. What's the difference between those two solution? (Haisheng Wu) 3. Re: In Search Of a clue... (Defining and making use of a type) (Adrien Haxaire) ---------------------------------------------------------------------- Message: 1 Date: Mon, 12 Dec 2011 21:24:10 -0500 From: Brandon Allbery <allber...@gmail.com> Subject: Re: [Haskell-beginners] Pattern matching over functions To: Giacomo Tesio <giac...@tesio.it> Cc: simplex.math.servi...@gmail.com, beginners@haskell.org, Daniel Fischer <daniel.is.fisc...@googlemail.com> Message-ID: <CAKFCL4W8x1tS1x38wbA6fVypDf=ttz3thj1j45rx4imd5k9...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" On Mon, Dec 12, 2011 at 10:47, Giacomo Tesio <giac...@tesio.it> wrote: > While I got your point, I'm still wondering about functions as > constructor of other functions thus it would be possible to match to the > function name like we do for type constructors. > I can't have an insight about why this is wrong. Why can't we treat > functions as constructors? > This makes (f) and (id f) different, when referential transparency requires that they be the same. Anything capable of spotting that difference *must* be in IO. A concrete reason for this is laziness: *everything* is a function, including literals ? and I do not mean by this things like the implicit fromIntegral/fromRational on numeric literals. Everything is a function which is evaluated to WHNF when needed. So now you have an additional issue: if you're pattern-matching something ? which is the normal way to force evaluation ? you have to know whether we're expecting normal pattern matching, which does evaluation to a data constructor, or your function-pattern matching, which evaluates to some kind of distinguished "function constructor". Additionally, there is the question of where you draw the line. Do you consider a binding to be a "function constructor"? Only a top level binding? Only an imported binding? Only an import from a "system" module? Only a primop? I can see arguments for all of them, and no obvious argument for why one of them is better than the others. Moreover, the deeper you go in this stack, the greater the chance that different compilers, or different versions of the same compiler, produce different results; this is again a violation of referential transparency. -- brandon s allbery allber...@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20111212/60249477/attachment-0001.htm> ------------------------------ Message: 2 Date: Tue, 13 Dec 2011 11:49:03 +0800 From: Haisheng Wu <fre...@gmail.com> Subject: [Haskell-beginners] What's the difference between those two solution? To: Haskell Beginer <beginners@haskell.org> Message-ID: <CAFj8LZfTDZCGVBx7XONaJ7dtO9tPbfDa0Tng0ihZmMHtYDp=t...@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" Hello, I'm trying to solve Euler problem 104 with the solution "My Solution" below but it takes quite long time therefore I quite. Then I turn to haskell wiki for better solution which work well but I can not figure out why it is better than mine. I'm wondering whether more function call decrease the performance. Could you please help a little? Thank you. *-- | My Solution * main = print $ snd $ head $ dropWhile (\(x,y) -> (not . bothNinePandigit "123456789") x) (zip fibs [1..]) bothNinePandigit digits n = isFirstNinePandigit digits n && isLastNinePandigit digits n isLastNinePandigit digits n = digits == sort (lastDigits 9 n) isFirstNinePandigit digits n = digits == sort (firstDigits 9 n) firstDigits k n = take k (show n) lastDigits k n = show (n `mod` 10^k) fibs = 0 : 1 : zipWith (+) fibs (tail fibs) *-- | From Haskell Wiki * fibs = 1 : 1 : zipWith (+) fibs (tail fibs) isFibPan n = let a = n `mod` 1000000000 b = sort (show a) c = sort $ take 9 $ show n in b == "123456789" && c == "123456789" ex_104 = snd $ head $ dropWhile (\(x,y) -> (not . isFibPan) x) (zip fibs [1..]) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20111213/1cecea7c/attachment-0001.htm> ------------------------------ Message: 3 Date: Tue, 13 Dec 2011 09:13:30 +0100 From: Adrien Haxaire <adr...@haxaire.org> Subject: Re: [Haskell-beginners] In Search Of a clue... (Defining and making use of a type) To: <beginners@haskell.org> Message-ID: <ab6e692591b6579ac810f6d3d644a...@haxaire.org> Content-Type: text/plain; charset=UTF-8; format=flowed Hello, If you define your constructor like this: > data ScrConfig = ScrConfig [ ScrUple ] deriving (Show) it means that you need to give a list to the data constructor ScrConfig. It will then return the type ScrConfig, which is what you want. In GHCi: Main*>:t ScrConfig ScrConfig :: [ScrUple] -> ScrConfig This means that you do not have to declare sc1 as type ScrConfig, it is inferred by the compiler. So the declaration : > ScrConfig sc1 =ScrConfig( [s2 s1] ) ; should be: sc1 =ScrConfig( [s2 s1] ) ; The second point is that you do not give a list to ScrConfig, as list elements are separated with a comma in Haskell: [s2, s1]. So sc1 becomes: sc1 =ScrConfig( [s2, s1] ) ; and this will fix your first error. By the way, you do not need to add parens around the list, nor a semicolon at the end of the line. They do no harm here, but I find it clearer without. So I would write sc1 like this: sc1 = ScrConfig [s2, s1] Hope that helps, Adrien ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 42, Issue 18 *****************************************