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: (Integral a) => a vs Integer (Michael Snoyman) 2. Re: (Integral a) => a vs Integer (Dan Ross) 3. safetail problem (Roelof Wobben) 4. Re: safetail problem (Tom Murphy) 5. Re: safetail problem (Roelof Wobben) 6. Re: safetail problem (Tom Murphy) 7. Monads in javascript (Martin Drautzburg) 8. Re: safetail problem (David Place) 9. Re: Monads in javascript (Ertugrul Soeylemez) 10. Re: safetail problem (Ertugrul Soeylemez) ---------------------------------------------------------------------- Message: 1 Date: Tue, 12 Jul 2011 19:57:38 +0300 From: Michael Snoyman <mich...@snoyman.com> Subject: Re: [Haskell-beginners] (Integral a) => a vs Integer To: d...@rosspixelworks.com Cc: beginners@haskell.org Message-ID: <CAKA2Jg+dJ85=iy26uf3n97c2klzsjagsc-+cakhoanr0+yo...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 Precisely. On Tue, Jul 12, 2011 at 7:44 PM, Dan Ross <d...@rosspixelworks.com> wrote: > Ah. Because Integer is an instance of Integral right? > > So using Integer would be more restrictive. > > On Tue, 12 Jul 2011 19:22:36 +0300, Michael Snoyman wrote: >> >> On Tue, Jul 12, 2011 at 7:20 PM, Dan Ross <d...@rosspixelworks.com> wrote: >>> >>> Hi all- >>> >>> I'm going through Learn You a Haskell for Great Good and I don't >>> understand >>> the advantage of using (Integral a)=> a vs just Integer as I show below. >>> >>> Could someone explain this to me? >>> >>> Thanks, >>> >>> Dan >>> >>> lucky :: (Integral a) => a -> String >>> lucky 7 = "LUCKY NUMBER SEVEN!" >>> lucky x = "Sorry, you're out of luck, pal!" >>> >>> lucky :: Integer -> String >>> lucky 7 = "LUCKY NUMBER SEVEN!" >>> lucky x = "Sorry, you're out of luck, pal!" >> >> Hi Dan, >> >> The first version is polymorphic, and will work on *any* instance of >> Integral. This would allow people to use Int, Int32, Int64, etc. The >> second version requires the user to pass in specifically an Integer. >> >> Michael > > ------------------------------ Message: 2 Date: Tue, 12 Jul 2011 12:27:28 -0500 From: Dan Ross <d...@rosspixelworks.com> Subject: Re: [Haskell-beginners] (Integral a) => a vs Integer To: Michael Snoyman <mich...@snoyman.com> Cc: beginners@haskell.org Message-ID: <467863c0c21b8ca64ebdff3b9a54d...@rosspixelworks.com> Content-Type: text/plain; charset=UTF-8; format=flowed Cool. Thanks. On Tue, 12 Jul 2011 19:57:38 +0300, Michael Snoyman wrote: > Precisely. > > On Tue, Jul 12, 2011 at 7:44 PM, Dan Ross <d...@rosspixelworks.com> > wrote: >> Ah. Because Integer is an instance of Integral right? >> >> So using Integer would be more restrictive. >> >> On Tue, 12 Jul 2011 19:22:36 +0300, Michael Snoyman wrote: >>> >>> On Tue, Jul 12, 2011 at 7:20 PM, Dan Ross <d...@rosspixelworks.com> >>> wrote: >>>> >>>> Hi all- >>>> >>>> I'm going through Learn You a Haskell for Great Good and I don't >>>> understand >>>> the advantage of using (Integral a)=> a vs just Integer as I show >>>> below. >>>> >>>> Could someone explain this to me? >>>> >>>> Thanks, >>>> >>>> Dan >>>> >>>> lucky :: (Integral a) => a -> String >>>> lucky 7 = "LUCKY NUMBER SEVEN!" >>>> lucky x = "Sorry, you're out of luck, pal!" >>>> >>>> lucky :: Integer -> String >>>> lucky 7 = "LUCKY NUMBER SEVEN!" >>>> lucky x = "Sorry, you're out of luck, pal!" >>> >>> Hi Dan, >>> >>> The first version is polymorphic, and will work on *any* instance >>> of >>> Integral. This would allow people to use Int, Int32, Int64, etc. >>> The >>> second version requires the user to pass in specifically an >>> Integer. >>> >>> Michael >> >> ------------------------------ Message: 3 Date: Tue, 12 Jul 2011 19:16:06 +0000 From: Roelof Wobben <rwob...@hotmail.com> Subject: [Haskell-beginners] safetail problem To: <beginners@haskell.org> Message-ID: <snt118-w137b31eff4f48d7dc5c008ae...@phx.gbl> Content-Type: text/plain; charset="iso-8859-1" Hello, As a exercise I need to rewrite the tail function into a safetail function by using conditions. So I made this : http://codepad.org/bKcCUdqy But as you can see there is a error message. Can anyone explain me how to solve this ? Roelof ------------------------------ Message: 4 Date: Tue, 12 Jul 2011 15:21:46 -0400 From: Tom Murphy <amin...@gmail.com> Subject: Re: [Haskell-beginners] safetail problem To: Roelof Wobben <rwob...@hotmail.com> Cc: beginners@haskell.org Message-ID: <CAO9Q0tW-xjQn6KaHs5BiAVh0mpsL0xM=a+bosbmjijwpu_d...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 _ isn't a value: it's a wildcard character for pattern matching. Also, if x = [1,2,3], [0,x] is the wrong way to make a list. The right way is (0:x) (":" is "cons") What resource are you learning from? On 7/12/11, Roelof Wobben <rwob...@hotmail.com> wrote: > > > Hello, > > > > As a exercise I need to rewrite the tail function into a safetail function > by using conditions. > > > > So I made this : http://codepad.org/bKcCUdqy > > > > But as you can see there is a error message. > > > > Can anyone explain me how to solve this ? > > > > Roelof > > > > > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > ------------------------------ Message: 5 Date: Tue, 12 Jul 2011 20:45:08 +0000 From: Roelof Wobben <rwob...@hotmail.com> Subject: Re: [Haskell-beginners] safetail problem To: <beginners@haskell.org> Message-ID: <snt118-w5060131748b3b9c9e89138ae...@phx.gbl> Content-Type: text/plain; charset="iso-8859-1" Hello, I use Programming in Haskell from Graham Hutton. But I can't find anything in the first 4 chapters from that book why I must use (0,x) en what that means. Roelof ---------------------------------------- > Date: Tue, 12 Jul 2011 15:21:46 -0400 > Subject: Re: [Haskell-beginners] safetail problem > From: amin...@gmail.com > To: rwob...@hotmail.com > CC: beginners@haskell.org > > _ isn't a value: it's a wildcard character for pattern matching. > > Also, if x = [1,2,3], [0,x] is the wrong way to make a list. The right > way is (0:x) (":" is "cons") > > What resource are you learning from? > > On 7/12/11, Roelof Wobben <rwob...@hotmail.com> wrote: > > > > > > Hello, > > > > > > > > As a exercise I need to rewrite the tail function into a safetail function > > by using conditions. > > > > > > > > So I made this : http://codepad.org/bKcCUdqy > > > > > > > > But as you can see there is a error message. > > > > > > > > Can anyone explain me how to solve this ? > > > > > > > > Roelof > > > > > > > > > > > > > > _______________________________________________ > > Beginners mailing list > > Beginners@haskell.org > > http://www.haskell.org/mailman/listinfo/beginners > > ------------------------------ Message: 6 Date: Tue, 12 Jul 2011 16:53:31 -0400 From: Tom Murphy <amin...@gmail.com> Subject: Re: [Haskell-beginners] safetail problem To: Roelof Wobben <rwob...@hotmail.com> Cc: beginners@haskell.org Message-ID: <CAO9Q0tUuohpfHkTdB+anHYk7u4nJBjDgFZQrEN=afjdxxd8...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 If: x = [1,2,3] :: [Int] y = [0] :: [Int] z = 0 then: [y,x] == [[0],[1,2,3]] :: [[Int]] - a list of lists. It doesn't get simplified into [0,1,2,3]. (z,x) == (0,[1,2,3]) :: (Int, [Int]) - a tuple, whose first element is an Int, and whose second element is a list of Ints ([Int]) z:x == [0,1,2,3] :: [Int] The third one is what you're looking for. It's called "cons." Tom On 7/12/11, Roelof Wobben <rwob...@hotmail.com> wrote: > > > Hello, > > > > I use Programming in Haskell from Graham Hutton. > > > > But I can't find anything in the first 4 chapters from that book why I must > use (0,x) en what that means. > > > > Roelof > > > > > > > ---------------------------------------- >> Date: Tue, 12 Jul 2011 15:21:46 -0400 >> Subject: Re: [Haskell-beginners] safetail problem >> From: amin...@gmail.com >> To: rwob...@hotmail.com >> CC: beginners@haskell.org >> >> _ isn't a value: it's a wildcard character for pattern matching. >> >> Also, if x = [1,2,3], [0,x] is the wrong way to make a list. The right >> way is (0:x) (":" is "cons") >> >> What resource are you learning from? >> >> On 7/12/11, Roelof Wobben <rwob...@hotmail.com> wrote: >> > >> > >> > Hello, >> > >> > >> > >> > As a exercise I need to rewrite the tail function into a safetail >> > function >> > by using conditions. >> > >> > >> > >> > So I made this : http://codepad.org/bKcCUdqy >> > >> > >> > >> > But as you can see there is a error message. >> > >> > >> > >> > Can anyone explain me how to solve this ? >> > >> > >> > >> > Roelof >> > >> > >> > >> > >> > >> > >> > _______________________________________________ >> > Beginners mailing list >> > Beginners@haskell.org >> > http://www.haskell.org/mailman/listinfo/beginners >> > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > ------------------------------ Message: 7 Date: Tue, 12 Jul 2011 23:27:42 +0200 From: Martin Drautzburg <martin.drautzb...@web.de> Subject: [Haskell-beginners] Monads in javascript To: beginners <beginners@haskell.org> Message-ID: <201107122327.42509.martin.drautzb...@web.de> Content-Type: Text/Plain; charset="us-ascii" Hello all, I found some implementations of Monads in javascript, but they all do not allow capturing the intermediate results. My question is: "what is the ultimate cause for this"? In haskell the second argument to (>>=) is a function a->Mb which can be written as a lambda expression, e.g. \x->[x]. In javascript such a function would be written as function(x) {return[x]}. Did I get this right: in haskell the chain of >>= is constructed from the end? So chaining a->Mb and b->Mc gives you a->Mc, which is again suitable as a second argument to (>>=), right? So why can't I do this is javascipt? Or can I? The reason I am asking this is because I am trying to beautify callbacks. An asynchronous ajax call needs a function argument, which will be executed once the call completes. But what if I want to take the results of the first call, do something with them and pass it to a second ajax call, where I would again have to pass another function argument. I would end up with a deeply nested structure of lambdas, something like f(a,b,...function(...){ ... g(... function(...) I had some hopes that chaining functions monad-style would ease my pain. I might be on the wrong track though, feel free to tell me so. -- Martin -- Martin ------------------------------ Message: 8 Date: Tue, 12 Jul 2011 17:48:37 -0400 From: David Place <d...@vidplace.com> Subject: Re: [Haskell-beginners] safetail problem To: Roelof Wobben <rwob...@hotmail.com> Cc: beginners@haskell.org Message-ID: <f64be813-17f2-4051-a3fa-5ca1d953a...@vidplace.com> Content-Type: text/plain; charset=us-ascii On Jul 12, 2011, at 4:45 PM, Roelof Wobben wrote: > But I can't find anything in the first 4 chapters from that book why I must > use (0,x) en what that means. I think lists can be confusing for someone who is trying to learn Haskell and has only used imperative languages before. Typically, those languages use arrays to manipulate lists of things. The meaning of lists in Haskell is hidden by the confusing but convenient syntactic sugar used to write them. Really, lists are a recursive data type with two cases. The basis case: the empty list (written as "[]", close square bracket then open square bracket) The inductive case: a : List (here ":" is the constructor) So, if we see a list written as [1,2,3,4] it really means (1 : (2 : (3 : (4 : [])))) All the programs that work on lists use the underlying representation, so you really need to understand it. This confusion goes all the way back to 1960. Blame it on John McCarthy, inventor of Lisp! I hope that is a little helpful. Cheers, David ____________________ David Place Owner, Panpipes Ho! LLC http://panpipesho.com d...@vidplace.com ------------------------------ Message: 9 Date: Wed, 13 Jul 2011 00:03:44 +0200 From: Ertugrul Soeylemez <e...@ertes.de> Subject: Re: [Haskell-beginners] Monads in javascript To: beginners@haskell.org Message-ID: <20110713000344.6f83d...@angst.streitmacht.eu> Content-Type: text/plain; charset=US-ASCII Martin Drautzburg <martin.drautzb...@web.de> wrote: > I found some implementations of Monads in javascript, but they all do > not allow capturing the intermediate results. My question is: "what is > the ultimate cause for this"? > > In haskell the second argument to (>>=) is a function a->Mb which can > be written as a lambda expression, e.g. > \x->[x]. > In javascript such a function would be written as > function(x) {return[x]}. > > Did I get this right: in haskell the chain of >>= is constructed from > the end? Composition by (>>=) is by definition associative, so it really doesn't matter how you set your parentheses. > So chaining a->Mb and b->Mc gives you a->Mc, which is again suitable > as a second argument to (>>=), right? Yes, but beware that you may be confusing (>>=) with (>=>). > So why can't I do this is javascipt? Or can I? I don't see why that should be a problem. Note that there are many different ways to represent monads. Haskell represents them in terms of 'fmap', 'return' and (>>=). > The reason I am asking this is because I am trying to beautify > callbacks. An asynchronous ajax call needs a function argument, which > will be executed once the call completes. But what if I want to take > the results of the first call, do something with them and pass it to a > second ajax call, where I would again have to pass another function > argument. I would end up with a deeply nested structure of lambdas, > something like > > f(a,b,...function(...){ > ... > g(... function(...) > > I had some hopes that chaining functions monad-style would ease my > pain. I might be on the wrong track though, feel free to tell me so. You probably are, because the usefulness of monads depends a good deal on syntax. ECMA-based languages like JavaScript, ActionScript, Haxe, etc. have a terrible syntax for anonymous functions. To give you an alternative, this really sounds like you're looking for a continuation-based solution. Continuation passing style could really solve your problem elegantly even in JavaScript. Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/ ------------------------------ Message: 10 Date: Wed, 13 Jul 2011 00:25:15 +0200 From: Ertugrul Soeylemez <e...@ertes.de> Subject: Re: [Haskell-beginners] safetail problem To: beginners@haskell.org Message-ID: <20110713002515.49dbb...@angst.streitmacht.eu> Content-Type: text/plain; charset=US-ASCII Roelof Wobben <rwob...@hotmail.com> wrote: > As a exercise I need to rewrite the tail function into a safetail > function by using conditions. > > So I made this : http://codepad.org/bKcCUdqy > > But as you can see there is a error message. > > Can anyone explain me how to solve this ? Haskell is all about types. To understand Haskell, you really must understand how types work. The square brackets are a syntactic construct to easy constructing lists. As David pointed out, the underlying representation is different. If the types don't make sense, the whole program makes no sense. Let's see: [1, [2, 3, 4]] This cannot be right, because lists in Haskell are homogenous, so all elements have the same type, but here you have a list of two elements, one being 1 (a number) and the other being [2, 3, 4] (a list of numbers). The types obviously don't fit, so the program cannot be compiled. You have this: x = 1 y = [2, 3, 4] To put the element x in front of the list of elements y, you need a function of the following type: a -> [a] -> [a] The (:) function has exactly that type, so you can write: (:) x y or to use the usually more convenient infix syntax: x : y This is about list construction. What you need here however is the other direction: destruction. This is done by pattern matching. Again as David pointed out a list is exactly a chain of (:) applications. The list y would have the following real representation: y = 2 : (3 : (4 : [])) Since (:) is right-associative you can leave out the parentheses to have a slightly more readable equation. You can decompose this list by pattern matching, which gives you access to the building blocks of the list: safeTail (x:xs) = xs The (:) function is the "cons" or "prepended to" function. So safeTail applied to x prepended to xs is equal to xs. One more equation and your safeTail function is done. One further note: I have intentionally left out almost all of the type signatures here, but I strongly advise you to write them. In fact write type signatures before writing the actual functions. Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/ ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 37, Issue 22 *****************************************