From jan.snajder at fer.hr Mon Nov 3 17:45:17 2014 From: jan.snajder at fer.hr (Jan Snajder) Date: Mon, 03 Nov 2014 18:45:17 +0100 Subject: [Haskell-beginners] Lazy state + IO not lazy? Message-ID: <5457BF2D.6000002@fer.hr> Dear all, On 10/27/14, Kim-Ee Yeoh wrote: Kim-Ee, many thanks for your reply and sorry for my delayed reaction. > Consider > > let u = return () :: IO () in > expr >> u > > where expr ranges over (return undefined), (undefined), and (error "urk"). Ok, so this is what we get: (1) return undefined >> return () :: IO () ==> evaluates to 'IO ()' (2) > undefined >> return () *** Exception: Prelude.undefined (3) > error "urk" >> return () *** Exception: ur > Are their executions surprising? In my understanding, in a strict state monad, the '>>' operator will evaluate the (value,state) pair of the left expression up to the (,) constructor. Assuming this is correct, in (1) 'return undefined' gives a '(undefined,state)', which is not evaluated further, hence the computation continues. In (2), there is no 'return' to create a (value,state) pair, so when '>>' tries to evaluate the left expression, it evaluates directly to undefined. The same holds for error, which (I guess) stops the execution immediately upon being evaluated. Is my reasoning correct? > Compare to > > let u = return () :: State Int () in > evalState (expr >> u) 0 > > for both strict and lazy state. Ok, here we go: > Strict.evalState (return undefined >> return () :: Strict.State Int ()) 0 () > Strict.evalState (undefined >> return () :: Strict.State Int ()) 0 *** Exception: Prelude.undefined > Strict.evalState (error "urk" >> return () :: Strict.State Int ()) 0 *** Exception: urk > Lazy.evalState (return undefined >> return () :: Lazy.State Int ()) 0 () > Lazy.evalState (undefined >> return () :: Lazy.State Int ()) 0 () > Lazy.evalState (error "urk" >> return () :: Lazy.State Int ()) 0 () So, strict-state monad matches the behavior of 'IO', which is what I expected because I know 'IO' is a strict-state monad. Furthermore, lazy-state monad never inspects the left argument of '>>', so all three cases compute. > IO above matches one of them. Suppose > IO's behavior matches the other, what happens? I guess the IO monad would behave in an undesirable way because we would not be able to raise an exception nor catch undefined computations. Is that what you meant? > Now with the definition of mapM in mind, consider the difference > between your original: > > Lazy.evalStateT (head `fmap` mapM return (1:undefined)) 0 > > and > > Lazy.evalStateT (head `fmap` mapM return [1,undefined]) 0 > > Did you mean the latter? No, that's not what I meant. But I assume that in the latter case we won't get a bottom because we don't evaluate beyond the spine of the list: > Lazy.evalState (head `fmap` mapM return [1,undefined]) 0 1 > Lazy.evalStateT (head `fmap` mapM return [1,undefined]) 0 1 Is my interpretation correct? But what confuses me why we get different behavior here: > Lazy.evalState (head `fmap` mapM return (1:undefined)) 0 1 > Lazy.evalStateT (head `fmap` mapM return (1:undefined)) 0 *** Exception: Prelude.undefined From this I conclude that the second monad is strict, whereas the first is not. Is that correct? Assuming that is correct, does this mean that every monad stacked on top of an IO monad becomes strict?? Here's another example that illustrates this point: http://pastebin.com/0fAFmufA Why is (4) not computed in constant space, whereas (3) is? Finally, what ultimately bothers me is the following: is it true that if my state monad is built on top of an IO monad I cannot lazily consume a result of a mapM computation? (Here I am assuming, perhaps wrongly, that if '>>=' is non-strict in its left argument, then 'sequence' is tail recursive modulo cons. But I'm actually not convinced why this should be the case.) Many thanks for any input! Cheers, Jan From ky3 at atamo.com Tue Nov 4 09:27:27 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Tue, 4 Nov 2014 16:27:27 +0700 Subject: [Haskell-beginners] Lazy state + IO not lazy? In-Reply-To: <5457BF2D.6000002@fer.hr> References: <5457BF2D.6000002@fer.hr> Message-ID: Hey Jan, Your answers are fine. You're reasoning at a high-level which is okay, and if you want to get into the details, you'll have to inline the StateT abstraction and reason directly with the low-level code. > But what confuses me why we get different behavior here: > > > Lazy.evalState (head `fmap` mapM return (1:undefined)) 0 > 1 > > Lazy.evalStateT (head `fmap` mapM return (1:undefined)) 0 > *** Exception: Prelude.undefined Both expressions are identical save for one letter. But what's hidden is a third: head `fmap` mapM return (1:undefined) :: IO Int The gotcha is that expr no. 2, by virtue of operating in StateT over IO, must match the behavior of no. 3, because the monadic space contains all of IO. Put another way, no. 1 and 2 are so similar syntactically, you'd think their values must match. But no, the monadic semantics of s -> IO (a, s) are such that it's no. 3 that no. 2 must match with. > From this I conclude that the second monad is strict, whereas the first > is not. Is that correct? So we've seen that Lazy.StateT is strict /in the effects/. Which prompts the question, well, how is it lazier than Strict.StateT then, for which this five-year-old posting helps answer: http://marc.info/?l=haskell-cafe&m=123618429420650 > Assuming that is correct, does this mean that > every monad stacked on top of an IO monad becomes strict?? In the sense of strict in the effects, I believe so but I haven't checked all cases. > Here's another example that illustrates this point: > http://pastebin.com/0fAFmufA > > Why is (4) not computed in constant space, whereas (3) is? For the same reason that the traversable functions sequence and mapM diverge on infinite lists when working in IO. Unless you take the lazy I/O way out. > Finally, what ultimately bothers me is the following: > is it true that if my state monad is built on top of an IO monad I > cannot lazily consume a result of a mapM computation? An IO mapM on an infinite list results in an /infinitely IO-effectful/ monadic value. Which is why mapM return diverges, even when each of those infinity of effects is null. Like why sum [0,0..] doesn't terminate either. Watch what happens when you replace "mapM return" by the moral equivalent of "return". The triple combo of traversable + IO + infinite list is bound to fail. Without recourse to lazy I/O, that is. -- Kim-Ee From r.wobben at home.nl Sun Nov 9 09:42:26 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Sun, 09 Nov 2014 10:42:26 +0100 Subject: [Haskell-beginners] invalid type signature last2::: [a] -> a In-Reply-To: <545F2F89.1080901@home.nl> References: <545F2F89.1080901@home.nl> Message-ID: <545F3702.9070203@home.nl> An HTML attachment was scrubbed... URL: From akaberto at gmail.com Sun Nov 9 09:48:59 2014 From: akaberto at gmail.com (akash g) Date: Sun, 9 Nov 2014 15:18:59 +0530 Subject: [Haskell-beginners] invalid type signature last2::: [a] -> a In-Reply-To: <545F3702.9070203@home.nl> References: <545F2F89.1080901@home.nl> <545F3702.9070203@home.nl> Message-ID: The naming convention for variables and functions is that they should start with a lower case letter. Types have capitalized names. On Sun, Nov 9, 2014 at 3:12 PM, Roelof Wobben wrote: > > > > > > > > > > > > > > Hello, > > I try to solve the 99 haskell problems on several ways. > > But my first try which looks like this : > > Last2::[a]-> a > Last2:: last[list] > > gives the following error message : > > src/Main.hs at 1:1-1:6 > Invalid type signature: Last2 :: [a] -> a Should be of form > :: > > What am trying to do is say the input is a list which can be of integers > or chars so everything is the output can then also be of type everything. > > Roelof > > > > > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From r.wobben at home.nl Sun Nov 9 09:10:33 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Sun, 09 Nov 2014 10:10:33 +0100 Subject: [Haskell-beginners] invalid type signature last2::: [a] -> a Message-ID: <545F2F89.1080901@home.nl> Hello, I try to solve the 99 haskell problems on several ways. But my first try which looks like this : Last2::[a]-> a Last2:: last[list] gives the following error message : src/Main.hs at 1:1-1:6 Invalid type signature: Last2 :: [a] -> a Should be of form :: What am trying to do is say the input is a list which can be of integers or chars so everything is the output can then also be of type everything. Roelof From r.wobben at home.nl Sun Nov 9 10:02:41 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Sun, 09 Nov 2014 11:02:41 +0100 Subject: [Haskell-beginners] invalid type signature last2::: [a] -> a In-Reply-To: References: <545F2F89.1080901@home.nl> <545F3702.9070203@home.nl> Message-ID: <545F3BC1.6010701@home.nl> An HTML attachment was scrubbed... URL: From sweemaykhaw at gmail.com Sun Nov 9 10:17:28 2014 From: sweemaykhaw at gmail.com (May Khaw) Date: Sun, 09 Nov 2014 10:17:28 +0000 Subject: [Haskell-beginners] invalid type signature last2::: [a] -> a References: <545F2F89.1080901@home.nl> <545F3702.9070203@home.nl> <545F3BC1.6010701@home.nl> Message-ID: The first one means that you gave a type signature for a function you did not define. The second one means that there are 2 last2 type signature, but you can only have one. What you want is something like : last2 :: [a] - > a last2 list = (put your function here) On Sun, 9 Nov 2014 21:02 Roelof Wobben wrote: > Thanks, > > I changed it to this : > > last2::[a]-> a > last2::last[a] > > but now I see these error messages: > > src/Main.hs at 1:1-1:6 The type signature forlast2 lacks an accompanying > binding > src/Main.hs at 2:1-2:6 Duplicate type signatures for last2 > at > /home/app/isolation-runner-work/projects/75679/session.207/src/src/Main.hs:1:1-5 > > > /home/app/isolation-runner-work/projects/75679/session.207/src/src/Main.hs:2:1-5 > src/Main.hs at 2:1-2:6 > The type signature for last2 lacks an accompanying binding > > > > akash g schreef op 9-11-2014 10:48: > > The naming convention for variables and functions is that they should > start with a lower case letter. Types have capitalized names. > > > On Sun, Nov 9, 2014 at 3:12 PM, Roelof Wobben wrote: > >> >> >> >> >> >> >> >> >> >> >> >> >> >> Hello, >> >> I try to solve the 99 haskell problems on several ways. >> >> But my first try which looks like this : >> >> Last2::[a]-> a >> Last2:: last[list] >> >> gives the following error message : >> >> src/Main.hs at 1:1-1:6 >> Invalid type signature: Last2 :: [a] -> a Should be of form >> :: >> >> What am trying to do is say the input is a list which can be of integers >> or chars so everything is the output can then also be of type everything. >> >> Roelof >> >> >> >> >> >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > > _______________________________________________ > Beginners mailing listBeginners at haskell.orghttp://www.haskell.org/mailman/listinfo/beginners > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From r.wobben at home.nl Sun Nov 9 13:11:12 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Sun, 09 Nov 2014 14:11:12 +0100 Subject: [Haskell-beginners] invalid type signature last2::: [a] -> a In-Reply-To: References: <545F2F89.1080901@home.nl> <545F3702.9070203@home.nl> <545F3BC1.6010701@home.nl> Message-ID: <545F67F0.8010803@home.nl> An HTML attachment was scrubbed... URL: From aditbiswas1 at gmail.com Sun Nov 9 17:49:48 2014 From: aditbiswas1 at gmail.com (Adit Biswas) Date: Sun, 9 Nov 2014 23:19:48 +0530 Subject: [Haskell-beginners] First Project: Imperative Algorithm Visualization tool Message-ID: I'm kindoff a beginner in haskell, I've mostly just been exploring the language, some of the introductory books on the subject and writing some small programs. This is one of my first projects I wanted to try out, but I'm feeling a bit lost on how to go about doing it. I want to build a tool which takes in inputs describing algorithms, probably in their imperative form and generate animations using SVG and HTML5 preferably or OpenGL. I find that being able to visualize the execution of an algorithm helped me understand the material a few years ago when i started college but I mostly had to do these on pen and paper. So my higher level idea on approaching this problem is: 1. Create a dsl for describing data structures, e.g Link lists, trees, graphs 2. Create a dsl for describing each step of the algorithms manipulating the data structures 3. The algorithms would be a monadic composition of the step ADTs 4. Lift the algorithm to some monad which carries out the side effects of making changes to a visualization. I'm sorry if I didn't use all the correct terminology for describing the problem, I'm not completely sure if it's an appropriate approach. My logic was that Since each state of the visualization is a sideeffect of executing an algorithm, it would be a seperate monad like IO, the entire algorithm can be represented as some form of a graph, I should have data types representing each component of the graph. I have no idea how to go about designing a DSL or for designing a monad which will handle the animations. Could someone help me approach the solution and what topics I want to look at to understand how to build the solution. Feel free to let me know if you'd like to help work on the tool, or if you think I should simplify my project in some way. This is mostly just an idea for a project and since I'm feeling pretty beginnerish with the language, i wouldnt love suggestions on a simpler project which can sorta lead me in a direction in which i can make this tool in the future. Thank You! -- Adit Biswas From ky3 at atamo.com Sun Nov 9 18:03:48 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Mon, 10 Nov 2014 01:03:48 +0700 Subject: [Haskell-beginners] First Project: Imperative Algorithm Visualization tool In-Reply-To: References: Message-ID: On Mon, Nov 10, 2014 at 12:49 AM, Adit Biswas wrote: > I have no idea how to go about designing a DSL or for designing a > monad which will handle the animations. > Is it really about DSLs and monads? Or is it first getting a grasp of the problem space? You're trying to chew everything at once in a first bite. Scope out the smallest meaningful chunk and use that experiential learning to direct where to go next. "If you can't solve a problem, then there is an easier problem you can solve: find it." -- Polya -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeffbrown.the at gmail.com Sun Nov 9 19:05:39 2014 From: jeffbrown.the at gmail.com (Jeffrey Brown) Date: Sun, 9 Nov 2014 11:05:39 -0800 Subject: [Haskell-beginners] invalid type signature last2::: [a] -> a In-Reply-To: <545F67F0.8010803@home.nl> References: <545F2F89.1080901@home.nl> <545F3702.9070203@home.nl> <545F3BC1.6010701@home.nl> <545F67F0.8010803@home.nl> Message-ID: Have you read this ? I found it fascinating, helpful, and (perhaps unlike most papers out there on Haskell) not at all difficult to read quickly. On Sun, Nov 9, 2014 at 5:11 AM, Roelof Wobben wrote: > Thanks, > > Now when I run it, I see a error message that main is missing. > and I cannot find in theIlearnyouahaskell how to solve this. > > Roelof > > > > > May Khaw schreef op 9-11-2014 11:17: > > The first one means that you gave a type signature for a function you did > not define. > > The second one means that there are 2 last2 type signature, but you can > only have one. > > What you want is something like : > last2 :: [a] - > a > last2 list = (put your function here) > > On Sun, 9 Nov 2014 21:02 Roelof Wobben wrote: > >> Thanks, >> >> I changed it to this : >> >> last2::[a]-> a >> last2::last[a] >> >> but now I see these error messages: >> >> src/Main.hs at 1:1-1:6 The type signature forlast2 lacks an accompanying >> binding >> src/Main.hs at 2:1-2:6 Duplicate type signatures for last2 >> at >> /home/app/isolation-runner-work/projects/75679/session.207/src/src/Main.hs:1:1-5 >> >> >> /home/app/isolation-runner-work/projects/75679/session.207/src/src/Main.hs:2:1-5 >> src/Main.hs at 2:1-2:6 >> The type signature for last2 lacks an accompanying binding >> >> >> >> akash g schreef op 9-11-2014 10:48: >> >> The naming convention for variables and functions is that they should >> start with a lower case letter. Types have capitalized names. >> >> >> On Sun, Nov 9, 2014 at 3:12 PM, Roelof Wobben wrote: >> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> Hello, >>> >>> I try to solve the 99 haskell problems on several ways. >>> >>> But my first try which looks like this : >>> >>> Last2::[a]-> a >>> Last2:: last[list] >>> >>> gives the following error message : >>> >>> src/Main.hs at 1:1-1:6 >>> Invalid type signature: Last2 :: [a] -> a Should be of form >>> :: >>> >>> What am trying to do is say the input is a list which can be of integers >>> or chars so everything is the output can then also be of type everything. >>> >>> Roelof >>> >>> >>> >>> >>> >>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> >> >> _______________________________________________ >> Beginners mailing listBeginners at haskell.orghttp://www.haskell.org/mailman/listinfo/beginners >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > > _______________________________________________ > Beginners mailing listBeginners at haskell.orghttp://www.haskell.org/mailman/listinfo/beginners > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From r.wobben at home.nl Sun Nov 9 19:09:11 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Sun, 09 Nov 2014 20:09:11 +0100 Subject: [Haskell-beginners] invalid type signature last2::: [a] -> a In-Reply-To: References: <545F2F89.1080901@home.nl> <545F3702.9070203@home.nl> <545F3BC1.6010701@home.nl> <545F67F0.8010803@home.nl> Message-ID: <545FBBD7.3090508@home.nl> An HTML attachment was scrubbed... URL: From hjgtuyl at chello.nl Sun Nov 9 20:52:17 2014 From: hjgtuyl at chello.nl (Henk-Jan van Tuyl) Date: Sun, 09 Nov 2014 21:52:17 +0100 Subject: [Haskell-beginners] invalid type signature last2::: [a] -> a In-Reply-To: <545F67F0.8010803@home.nl> References: <545F2F89.1080901@home.nl> <545F3702.9070203@home.nl> <545F3BC1.6010701@home.nl> <545F67F0.8010803@home.nl> Message-ID: You must add something like module X where at the top of your file, to indicate that this is not the main module (where you must specify a main function). Groeten, Henk-Jan van Tuyl On Sun, 09 Nov 2014 14:11:12 +0100, Roelof Wobben wrote: > Thanks, > > Now when I run it, I see a error message that main is missing. > and I cannot find in theIlearnyouahaskell how to solve this. > > Roelof > > > > > May Khaw schreef op 9-11-2014 11:17: > > The first one means that you gave a type signature for a function you > did not > define. > > The second one means that there are 2 last2 type signature, but you can > only > have one. > > What you want is something like : > last2 :: [a] - > a > last2 list = (put your function here) > > On Sun, 9 Nov 2014 21:02 Roelof Wobben wrote: > > Thanks, > > I changed it to this : > > last2::[a]-> a > last2::last[a] > > but now I see these error messages: > > src/Main.hs at 1:1-1:6 The type signature forlast2 lacks an accompanying > binding > > src/Main.hs at 2:1-2:6 Duplicate type signatures for last2 > at > /home/app/isolation-runner-work/projects/75679/session.207/src/src/Main.hs:1:1-5 > /home/app/isolation-runner-work/projects/75679/session.207/src/src/Main.hs:2:1-5 > src/Main.hs at 2:1-2:6 > > The type signature for last2 lacks an accompanying binding > > > akash g schreef op 9-11-2014 10:48: > > The naming convention for variables and functions is that they should > start with > a lower case letter. Types have capitalized names. > > > On Sun, Nov 9, 2014 at 3:12 PM, Roelof Wobben wrote: > Hello, > > I try to solve the 99 haskell problems on several ways. > > But my first try which looks like this : > > Last2::[a]-> a > Last2:: last[list] > > gives the following error message : > > src/Main.hs at 1:1-1:6 > Invalid type signature: Last2 :: [a] -> a Should be of form > :: > > What am trying to do is say the input is a list which can be of integers > or chars so everything is the output can then also be of type everything. -- Folding at home What if you could share your unused computer power to help find a cure? In just 5 minutes you can join the world's biggest networked computer and get us closer sooner. Watch the video. http://folding.stanford.edu/ http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html Haskell programming -- From daniel.trstenjak at gmail.com Mon Nov 10 09:26:38 2014 From: daniel.trstenjak at gmail.com (Daniel Trstenjak) Date: Mon, 10 Nov 2014 10:26:38 +0100 Subject: [Haskell-beginners] First Project: Imperative Algorithm Visualization tool In-Reply-To: References: Message-ID: <20141110092638.GA4809@machine> Hi Adit, On Sun, Nov 09, 2014 at 11:19:48PM +0530, Adit Biswas wrote: > So my higher level idea on approaching this problem is: > 1. Create a dsl for describing data structures, e.g Link lists, > trees, graphs > 2. Create a dsl for describing each step of the algorithms > manipulating the data structures > 3. The algorithms would be a monadic composition of the step ADTs > 4. Lift the algorithm to some monad which carries out the side > effects of making changes to a visualization. I would start with defining the data structures which represent your algorithm and then defining a function that renders your data. If you're using OpenGL ist could be as simple as: render :: YourData -> IO () If you've a working version of this, then you could try defining a DSL, and if you want a monadic one, then most likely you will be using a free monad[1,2], so you don't have to write your own one. But at the end runnning your DSL will just result into 'YourData' and you can reuse your 'render' function. You don't need any kind of special Monad for the rendering and I also don't see any kind of advantage having one. Greetings, Daniel [1] http://www.haskellforall.com/2012/06/you-could-have-invented-free-monads.html [2] https://hackage.haskell.org/package/free From r.wobben at home.nl Mon Nov 10 09:50:23 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Mon, 10 Nov 2014 10:50:23 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell Message-ID: <54608A5F.3020707@home.nl> Hello, I tried to solve the first problem of the 99 haskell problems on 3 seperate ways. The problem is that I have to find the last item of a list. The solutions I found with a little help of this mailinglist are : last2::[a]-> Maybe a; last2 [] = Nothing; last2 ([x]) = Just x; last2 (_:xs) = last2 xs last3::[a]-> Maybe a; last3 x | null x = Nothing | null xs = Just (head x) | otherwise = last3 (tail x) where xs = tail x last4::[a]-> Maybe a; last4 x = case x of [] -> Nothing ; [x] -> Just x ; (_:xs) -> last4 xs main = print $ (last2 [] ) What do you experts think of the different ways ? Roelof From karl at karlv.net Mon Nov 10 10:16:37 2014 From: karl at karlv.net (Karl Voelker) Date: Mon, 10 Nov 2014 02:16:37 -0800 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <54608A5F.3020707@home.nl> References: <54608A5F.3020707@home.nl> Message-ID: <1415614597.807471.189088073.447B4E41@webmail.messagingengine.com> On Mon, Nov 10, 2014, at 01:50 AM, Roelof Wobben wrote: > What do you experts think of the different ways ? 2 and 4 are quite similar, and both fine. 3 is not so good: guards provide weaker guarantees than patterns, and head and tail are partial functions. All three implementations have in common that they do their own recursion. It would be a good exercise to try implementing last as a fold - in other words, letting the standard library do the recursion for you. -Karl From clintonf at kcg.com Mon Nov 10 10:24:57 2014 From: clintonf at kcg.com (Chris Linton-Ford) Date: Mon, 10 Nov 2014 10:24:57 +0000 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <1415614597.807471.189088073.447B4E41@webmail.messagingengine.com> References: <54608A5F.3020707@home.nl> <1415614597.807471.189088073.447B4E41@webmail.messagingengine.com> Message-ID: <4cd546b6a7d149e6a82e6384f821ca37@LN1WSCTSMAIL01.kcg.com> I've seen this mentioned a couple of times - that you should avoid explicit recursion where possible in Haskell (although I can't find the references now). Is this to make programs easier to understand, or more compact, or is there a performance benefit? Chris -----Original Message----- From: Beginners [mailto:beginners-bounces at haskell.org] On Behalf Of Karl Voelker Sent: Monday, November 10, 2014 10:17 AM To: beginners at haskell.org Subject: Re: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell On Mon, Nov 10, 2014, at 01:50 AM, Roelof Wobben wrote: > What do you experts think of the different ways ? 2 and 4 are quite similar, and both fine. 3 is not so good: guards provide weaker guarantees than patterns, and head and tail are partial functions. All three implementations have in common that they do their own recursion. It would be a good exercise to try implementing last as a fold - in other words, letting the standard library do the recursion for you. -Karl _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners KCG Europe Limited is authorized and regulated by the Financial Conduct Authority. Registered Office 55 Basinghall Street, London, EC2V 5DU. Registered in England & Wales No. 03632121 This e-mail and its attachments are intended only for the individual or entity to whom it is addressed and may contain information that is confidential, privileged, inside information, or subject to other restrictions on use or disclosure. Any unauthorized use, dissemination or copying of this transmission or the information in it is prohibited and may be unlawful. If you have received this transmission in error, please notify the sender immediately by return e-mail, and permanently delete or destroy this e-mail, any attachments, and all copies (digital or paper). Unless expressly stated in this e-mail, nothing in this message should be construed as a digital or electronic signature. For additional important disclaimers and disclosures regarding KCG?s products and services, please click on the following link: http://www.kcg.com/legal/global-disclosures From clintonf at kcg.com Mon Nov 10 10:29:29 2014 From: clintonf at kcg.com (Chris Linton-Ford) Date: Mon, 10 Nov 2014 10:29:29 +0000 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <4cd546b6a7d149e6a82e6384f821ca37@LN1WSCTSMAIL01.kcg.com> References: <54608A5F.3020707@home.nl> <1415614597.807471.189088073.447B4E41@webmail.messagingengine.com> <4cd546b6a7d149e6a82e6384f821ca37@LN1WSCTSMAIL01.kcg.com> Message-ID: <51439384ad644cc49ce092b5c48ea43f@LN1WSCTSMAIL01.kcg.com> Never mind - I should have Googled this: https://www.haskell.org/haskellwiki/Haskell_programming_tips#Avoid_explicit_recursion Chris -----Original Message----- From: Beginners [mailto:beginners-bounces at haskell.org] On Behalf Of Chris Linton-Ford Sent: Monday, November 10, 2014 10:25 AM To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject: Re: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell I've seen this mentioned a couple of times - that you should avoid explicit recursion where possible in Haskell (although I can't find the references now). Is this to make programs easier to understand, or more compact, or is there a performance benefit? Chris -----Original Message----- From: Beginners [mailto:beginners-bounces at haskell.org] On Behalf Of Karl Voelker Sent: Monday, November 10, 2014 10:17 AM To: beginners at haskell.org Subject: Re: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell On Mon, Nov 10, 2014, at 01:50 AM, Roelof Wobben wrote: > What do you experts think of the different ways ? 2 and 4 are quite similar, and both fine. 3 is not so good: guards provide weaker guarantees than patterns, and head and tail are partial functions. All three implementations have in common that they do their own recursion. It would be a good exercise to try implementing last as a fold - in other words, letting the standard library do the recursion for you. -Karl _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners KCG Europe Limited is authorized and regulated by the Financial Conduct Authority. Registered Office 55 Basinghall Street, London, EC2V 5DU. Registered in England & Wales No. 03632121 This e-mail and its attachments are intended only for the individual or entity to whom it is addressed and may contain information that is confidential, privileged, inside information, or subject to other restrictions on use or disclosure. Any unauthorized use, dissemination or copying of this transmission or the information in it is prohibited and may be unlawful. If you have received this transmission in error, please notify the sender immediately by return e-mail, and permanently delete or destroy this e-mail, any attachments, and all copies (digital or paper). Unless expressly stated in this e-mail, nothing in this message should be construed as a digital or electronic signature. For additional important disclaimers and disclosures regarding KCG?s products and services, please click on the following link: http://www.kcg.com/legal/global-disclosures _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners KCG Europe Limited is authorized and regulated by the Financial Conduct Authority. Registered Office 55 Basinghall Street, London, EC2V 5DU. Registered in England & Wales No. 03632121 This e-mail and its attachments are intended only for the individual or entity to whom it is addressed and may contain information that is confidential, privileged, inside information, or subject to other restrictions on use or disclosure. Any unauthorized use, dissemination or copying of this transmission or the information in it is prohibited and may be unlawful. If you have received this transmission in error, please notify the sender immediately by return e-mail, and permanently delete or destroy this e-mail, any attachments, and all copies (digital or paper). Unless expressly stated in this e-mail, nothing in this message should be construed as a digital or electronic signature. For additional important disclaimers and disclosures regarding KCG?s products and services, please click on the following link: http://www.kcg.com/legal/global-disclosures From raabe at froglogic.com Mon Nov 10 10:43:56 2014 From: raabe at froglogic.com (Frerich Raabe) Date: Mon, 10 Nov 2014 11:43:56 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <1415614597.807471.189088073.447B4E41@webmail.messagingengine.com> References: <54608A5F.3020707@home.nl> <1415614597.807471.189088073.447B4E41@webmail.messagingengine.com> Message-ID: On 2014-11-10 11:16, Karl Voelker wrote: > On Mon, Nov 10, 2014, at 01:50 AM, Roelof Wobben wrote: >> What do you experts think of the different ways ? > > 2 and 4 are quite similar, and both fine. 3 is not so good: guards > provide weaker guarantees than patterns, and head and tail are partial > functions. In addition to what Karl wrote, I'd like to suggest not using the semicolon all the time -- it's not needed and just adds noise. > All three implementations have in common that they do their own > recursion. It would be a good exercise to try implementing last as a > fold - in other words, letting the standard library do the recursion for > you. Right - I suggest trying to express the problem with the most abstract function first, then consider using a fold, then use manual recursion. in your particular case you could exploit that for non-empty lists, getting the last element of a list is the same as getting the first element of a reversed list (and there are ready-made functions for reversing a list and getting the first element of a list). -- Frerich Raabe - raabe at froglogic.com www.froglogic.com - Multi-Platform GUI Testing From r.wobben at home.nl Mon Nov 10 12:47:55 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Mon, 10 Nov 2014 13:47:55 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: References: <54608A5F.3020707@home.nl> <1415614597.807471.189088073.447B4E41@webmail.messagingengine.com> Message-ID: <5460B3FB.7040807@home.nl> Thanks all, I will try to make a fold solution as soon as I see that functional explained in the learnyouahaskell. Roelof Frerich Raabe schreef op 10-11-2014 11:43: > On 2014-11-10 11:16, Karl Voelker wrote: >> On Mon, Nov 10, 2014, at 01:50 AM, Roelof Wobben wrote: >>> What do you experts think of the different ways ? >> >> 2 and 4 are quite similar, and both fine. 3 is not so good: guards >> provide weaker guarantees than patterns, and head and tail are partial >> functions. > > In addition to what Karl wrote, I'd like to suggest not using the > semicolon all the time -- it's not needed and just adds noise. > >> All three implementations have in common that they do their own >> recursion. It would be a good exercise to try implementing last as a >> fold - in other words, letting the standard library do the recursion for >> you. > > Right - I suggest trying to express the problem with the most abstract > function first, then consider using a fold, then use manual recursion. in > your particular case you could exploit that for non-empty lists, getting > the last element of a list is the same as getting the first element of > a reversed list (and there are ready-made functions for reversing a list > and getting the first element of a list). > From r.wobben at home.nl Mon Nov 10 13:28:08 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Mon, 10 Nov 2014 14:28:08 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <5460B3FB.7040807@home.nl> References: <54608A5F.3020707@home.nl> <1415614597.807471.189088073.447B4E41@webmail.messagingengine.com> <5460B3FB.7040807@home.nl> Message-ID: <5460BD68.3010705@home.nl> I tried and so far I have this : last5 = foldl(\acc x -> if x=[] then x else acc xs) [] xs But now I get parse error on the -> part. Roelof Roelof Wobben schreef op 10-11-2014 13:47: > Thanks all, > > I will try to make a fold solution as soon as I see that functional > explained in the learnyouahaskell. > > Roelof > > > Frerich Raabe schreef op 10-11-2014 11:43: >> On 2014-11-10 11:16, Karl Voelker wrote: >>> On Mon, Nov 10, 2014, at 01:50 AM, Roelof Wobben wrote: >>>> What do you experts think of the different ways ? >>> >>> 2 and 4 are quite similar, and both fine. 3 is not so good: guards >>> provide weaker guarantees than patterns, and head and tail are partial >>> functions. >> >> In addition to what Karl wrote, I'd like to suggest not using the >> semicolon all the time -- it's not needed and just adds noise. >> >>> All three implementations have in common that they do their own >>> recursion. It would be a good exercise to try implementing last as a >>> fold - in other words, letting the standard library do the recursion >>> for >>> you. >> >> Right - I suggest trying to express the problem with the most abstract >> function first, then consider using a fold, then use manual >> recursion. in >> your particular case you could exploit that for non-empty lists, getting >> the last element of a list is the same as getting the first element of >> a reversed list (and there are ready-made functions for reversing a list >> and getting the first element of a list). >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From efasckenoth at gmail.com Mon Nov 10 14:12:20 2014 From: efasckenoth at gmail.com (Stefan =?iso-8859-1?Q?H=F6ck?=) Date: Mon, 10 Nov 2014 15:12:20 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <5460BD68.3010705@home.nl> References: <54608A5F.3020707@home.nl> <1415614597.807471.189088073.447B4E41@webmail.messagingengine.com> <5460B3FB.7040807@home.nl> <5460BD68.3010705@home.nl> Message-ID: <20141110141220.GB692@hunter> Hi Roelof It seems like you try to do too many things at once. Here's how you could go about this step-by-step and let GHC help you implement your functions along the way: First, give the type signature of your function: last5 :: [a] -> Maybe a last5 = undefined Now, load this into GHCi or compile with GHC. If it compiles, you're on the right track. Now, you want to implement it using a fold (try both, foldl and foldr): last5 :: [a] -> Maybe a last5 xs = foldr _ _ xs The underscores are 'type holes'. This tells the compiler to give you some information about what is supposed to be placed at the two positions. For the moment, we are only interested in the types of the things that go there. The compiler will tell you, that the hole at the first position is of type a -> Maybe a -> Maybe a and the hole at the second position is of type Maybe a Now, instead of filling the holes in place, let's define two helper functions together with their type signatures. You can later on inline them in your definition of last5, but for the time being, let's get as much help from the compiler as we can. last5 :: [a] -> Maybe a last5 xs = foldr acc initial xs acc :: a -> Maybe a -> Maybe a acc = undefined initial :: Maybe a initial = undefined Again, compile or load into GHCi. If you did anything wrong, the compiler will tell you so. There is only one possible way to implement function `initial` without cheating (= raising an error) initial :: Maybe a initial = Nothing Function `acc` can be implemented in several ways. Only one of them will lead to the desired behavior. Finding out the proper implementation is the main point of this folding-exercise. Try also an implementation using foldl. Does it behave as expected? What are the differences compared to foldr? When you feed your implementations a huge list - say [1..20000000] - what happens? Note that whenever you get an error message in a rather complex function implementation, move local function definitions and lambdas to the top level, give them a type signature and implement them separately one at a time. Use type holes to let the compiler give assistance with type signatures and possible implementations. Once everything compiles and runs as expected, move the toplevel definitions back to where you'd like them best. Stefan PS: A more succint implementation of last5 would use currying: last5 = foldr acc initial PPS: If you get a stack overflow with very large lists, try using foldl' from Data.List (or better, once you learned about the Foldable type class, from Data.Foldable). On Mon, Nov 10, 2014 at 02:28:08PM +0100, Roelof Wobben wrote: > I tried and so far I have this : > > last5 = foldl(\acc x -> if x=[] then x else acc xs) [] xs > > But now I get parse error on the -> part. > > Roelof > > > Roelof Wobben schreef op 10-11-2014 13:47: > >Thanks all, > > > >I will try to make a fold solution as soon as I see that functional > >explained in the learnyouahaskell. > > > >Roelof > > > > > >Frerich Raabe schreef op 10-11-2014 11:43: > >>On 2014-11-10 11:16, Karl Voelker wrote: > >>>On Mon, Nov 10, 2014, at 01:50 AM, Roelof Wobben wrote: > >>>>What do you experts think of the different ways ? > >>> > >>>2 and 4 are quite similar, and both fine. 3 is not so good: guards > >>>provide weaker guarantees than patterns, and head and tail are partial > >>>functions. > >> > >>In addition to what Karl wrote, I'd like to suggest not using the > >>semicolon all the time -- it's not needed and just adds noise. > >> > >>>All three implementations have in common that they do their own > >>>recursion. It would be a good exercise to try implementing last as a > >>>fold - in other words, letting the standard library do the recursion > >>>for > >>>you. > >> > >>Right - I suggest trying to express the problem with the most abstract > >>function first, then consider using a fold, then use manual recursion. > >>in > >>your particular case you could exploit that for non-empty lists, getting > >>the last element of a list is the same as getting the first element of > >>a reversed list (and there are ready-made functions for reversing a list > >>and getting the first element of a list). > >> > > > >_______________________________________________ > >Beginners mailing list > >Beginners at haskell.org > >http://www.haskell.org/mailman/listinfo/beginners > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From efasckenoth at gmail.com Mon Nov 10 14:23:31 2014 From: efasckenoth at gmail.com (Stefan =?iso-8859-1?Q?H=F6ck?=) Date: Mon, 10 Nov 2014 15:23:31 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <20141110141220.GB692@hunter> References: <54608A5F.3020707@home.nl> <1415614597.807471.189088073.447B4E41@webmail.messagingengine.com> <5460B3FB.7040807@home.nl> <5460BD68.3010705@home.nl> <20141110141220.GB692@hunter> Message-ID: <20141110142331.GC692@hunter> Just a quick note: That's 'typed holes' not 'type holes' ... On Mon, Nov 10, 2014 at 03:12:20PM +0100, Stefan H?ck wrote: > Hi Roelof > > It seems like you try to do too many things at once. Here's how you > could go about this step-by-step and let GHC help you implement your > functions along the way: > > First, give the type signature of your function: > > last5 :: [a] -> Maybe a > last5 = undefined > > Now, load this into GHCi or compile with GHC. If it compiles, you're > on the right track. Now, you want to implement it using a fold > (try both, foldl and foldr): > > last5 :: [a] -> Maybe a > last5 xs = foldr _ _ xs > > The underscores are 'type holes'. This tells the compiler to give you > some information about what is supposed to be placed at the two > positions. For the moment, we are only interested in the types of the > things that go there. The compiler will tell you, that > the hole at the first position is of type > > a -> Maybe a -> Maybe a > > and the hole at the second position is of type > > Maybe a > > Now, instead of filling the holes in place, let's define two helper > functions together with their type signatures. You can later on inline > them in your definition of last5, but for the time being, let's get as > much help from the compiler as we can. > > last5 :: [a] -> Maybe a > last5 xs = foldr acc initial xs > > acc :: a -> Maybe a -> Maybe a > acc = undefined > > initial :: Maybe a > initial = undefined > > Again, compile or load into GHCi. If you did anything wrong, the > compiler will tell you so. There is only one possible way to > implement function `initial` without cheating (= raising an error) > > initial :: Maybe a > initial = Nothing > > Function `acc` can be implemented in several ways. Only one of them > will lead to the desired behavior. Finding out the proper implementation > is the main point of this folding-exercise. Try also an implementation > using foldl. Does it behave as expected? What are the differences > compared to foldr? When you feed your implementations a huge list - > say [1..20000000] - what happens? > > Note that whenever you get an error message in a rather complex > function implementation, move local function definitions and lambdas > to the top level, give them a type signature and implement them > separately one at a time. Use type holes to let the compiler give > assistance with type signatures and possible implementations. > Once everything compiles and runs as expected, move the toplevel > definitions back to where you'd like them best. > > Stefan > > PS: A more succint implementation of last5 would use currying: > > last5 = foldr acc initial > > PPS: If you get a stack overflow with very large lists, try > using foldl' from Data.List (or better, once you learned > about the Foldable type class, from Data.Foldable). > > On Mon, Nov 10, 2014 at 02:28:08PM +0100, Roelof Wobben wrote: > > I tried and so far I have this : > > > > last5 = foldl(\acc x -> if x=[] then x else acc xs) [] xs > > > > But now I get parse error on the -> part. > > > > Roelof > > > > > > Roelof Wobben schreef op 10-11-2014 13:47: > > >Thanks all, > > > > > >I will try to make a fold solution as soon as I see that functional > > >explained in the learnyouahaskell. > > > > > >Roelof > > > > > > > > >Frerich Raabe schreef op 10-11-2014 11:43: > > >>On 2014-11-10 11:16, Karl Voelker wrote: > > >>>On Mon, Nov 10, 2014, at 01:50 AM, Roelof Wobben wrote: > > >>>>What do you experts think of the different ways ? > > >>> > > >>>2 and 4 are quite similar, and both fine. 3 is not so good: guards > > >>>provide weaker guarantees than patterns, and head and tail are partial > > >>>functions. > > >> > > >>In addition to what Karl wrote, I'd like to suggest not using the > > >>semicolon all the time -- it's not needed and just adds noise. > > >> > > >>>All three implementations have in common that they do their own > > >>>recursion. It would be a good exercise to try implementing last as a > > >>>fold - in other words, letting the standard library do the recursion > > >>>for > > >>>you. > > >> > > >>Right - I suggest trying to express the problem with the most abstract > > >>function first, then consider using a fold, then use manual recursion. > > >>in > > >>your particular case you could exploit that for non-empty lists, getting > > >>the last element of a list is the same as getting the first element of > > >>a reversed list (and there are ready-made functions for reversing a list > > >>and getting the first element of a list). > > >> > > > > > >_______________________________________________ > > >Beginners mailing list > > >Beginners at haskell.org > > >http://www.haskell.org/mailman/listinfo/beginners > > > > > > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://www.haskell.org/mailman/listinfo/beginners > > From haskell at elisehuard.be Mon Nov 10 14:54:07 2014 From: haskell at elisehuard.be (Elise Huard) Date: Mon, 10 Nov 2014 15:54:07 +0100 Subject: [Haskell-beginners] First Project: Imperative Algorithm Visualization tool In-Reply-To: <20141110092638.GA4809@machine> References: <20141110092638.GA4809@machine> Message-ID: Hi Adit, animations in OpenGL are mostly not exactly trivial - there's several routes you can take, from simple to not simple at all (buffers, shaders ...). But maybe you know the problem space already. Some example sources for animations in Haskell: - OpenGL, simple animation with rotations and translations: https://github.com/haskell-opengl/GLUT/blob/master/examples/Misc/Gears.hs - SDL2, using sprites: https://github.com/haskell-game/sdl2/blob/new-api/examples/lazyfoo/Lesson11.hs As the others said, it's probably better to start with a small problem, build up to what you want to build, and see if you can abstract from there ... Cheers, Elise On 10 November 2014 10:26, Daniel Trstenjak wrote: > > Hi Adit, > > On Sun, Nov 09, 2014 at 11:19:48PM +0530, Adit Biswas wrote: >> So my higher level idea on approaching this problem is: >> 1. Create a dsl for describing data structures, e.g Link lists, >> trees, graphs >> 2. Create a dsl for describing each step of the algorithms >> manipulating the data structures >> 3. The algorithms would be a monadic composition of the step ADTs >> 4. Lift the algorithm to some monad which carries out the side >> effects of making changes to a visualization. > > I would start with defining the data structures which represent > your algorithm and then defining a function that renders your data. > > If you're using OpenGL ist could be as simple as: > > render :: YourData -> IO () > > > If you've a working version of this, then you could try defining > a DSL, and if you want a monadic one, then most likely you will > be using a free monad[1,2], so you don't have to write your own one. > > But at the end runnning your DSL will just result into 'YourData' > and you can reuse your 'render' function. > > You don't need any kind of special Monad for the rendering and > I also don't see any kind of advantage having one. > > > Greetings, > Daniel > > [1] http://www.haskellforall.com/2012/06/you-could-have-invented-free-monads.html > [2] https://hackage.haskell.org/package/free > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From r.wobben at home.nl Mon Nov 10 15:10:37 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Mon, 10 Nov 2014 16:10:37 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <20141110142331.GC692@hunter> References: <54608A5F.3020707@home.nl> <1415614597.807471.189088073.447B4E41@webmail.messagingengine.com> <5460B3FB.7040807@home.nl> <5460BD68.3010705@home.nl> <20141110141220.GB692@hunter> <20141110142331.GC692@hunter> Message-ID: <5460D56D.5070709@home.nl> No problem . Im strugelling to make acc work. I try to say that if the input list has only 1 item the outcome is the head of that list. But doing acc = Just (head a) or doing acc = Just (head acc) gives both that acc or a is not in scope also doing acc x = Just (head x) gives a error messages that the types are not matching. Roelof Stefan H?ck schreef op 10-11-2014 15:23: > Just a quick note: That's 'typed holes' not 'type holes' ... > > On Mon, Nov 10, 2014 at 03:12:20PM +0100, Stefan H?ck wrote: >> Hi Roelof >> >> It seems like you try to do too many things at once. Here's how you >> could go about this step-by-step and let GHC help you implement your >> functions along the way: >> >> First, give the type signature of your function: >> >> last5 :: [a] -> Maybe a >> last5 = undefined >> >> Now, load this into GHCi or compile with GHC. If it compiles, you're >> on the right track. Now, you want to implement it using a fold >> (try both, foldl and foldr): >> >> last5 :: [a] -> Maybe a >> last5 xs = foldr _ _ xs >> >> The underscores are 'type holes'. This tells the compiler to give you >> some information about what is supposed to be placed at the two >> positions. For the moment, we are only interested in the types of the >> things that go there. The compiler will tell you, that >> the hole at the first position is of type >> >> a -> Maybe a -> Maybe a >> >> and the hole at the second position is of type >> >> Maybe a >> >> Now, instead of filling the holes in place, let's define two helper >> functions together with their type signatures. You can later on inline >> them in your definition of last5, but for the time being, let's get as >> much help from the compiler as we can. >> >> last5 :: [a] -> Maybe a >> last5 xs = foldr acc initial xs >> >> acc :: a -> Maybe a -> Maybe a >> acc = undefined >> >> initial :: Maybe a >> initial = undefined >> >> Again, compile or load into GHCi. If you did anything wrong, the >> compiler will tell you so. There is only one possible way to >> implement function `initial` without cheating (= raising an error) >> >> initial :: Maybe a >> initial = Nothing >> >> Function `acc` can be implemented in several ways. Only one of them >> will lead to the desired behavior. Finding out the proper implementation >> is the main point of this folding-exercise. Try also an implementation >> using foldl. Does it behave as expected? What are the differences >> compared to foldr? When you feed your implementations a huge list - >> say [1..20000000] - what happens? >> >> Note that whenever you get an error message in a rather complex >> function implementation, move local function definitions and lambdas >> to the top level, give them a type signature and implement them >> separately one at a time. Use type holes to let the compiler give >> assistance with type signatures and possible implementations. >> Once everything compiles and runs as expected, move the toplevel >> definitions back to where you'd like them best. >> >> Stefan >> >> PS: A more succint implementation of last5 would use currying: >> >> last5 = foldr acc initial >> >> PPS: If you get a stack overflow with very large lists, try >> using foldl' from Data.List (or better, once you learned >> about the Foldable type class, from Data.Foldable). >> >> On Mon, Nov 10, 2014 at 02:28:08PM +0100, Roelof Wobben wrote: >>> I tried and so far I have this : >>> >>> last5 = foldl(\acc x -> if x=[] then x else acc xs) [] xs >>> >>> But now I get parse error on the -> part. >>> >>> Roelof >>> >>> >>> Roelof Wobben schreef op 10-11-2014 13:47: >>>> Thanks all, >>>> >>>> I will try to make a fold solution as soon as I see that functional >>>> explained in the learnyouahaskell. >>>> >>>> Roelof >>>> >>>> >>>> Frerich Raabe schreef op 10-11-2014 11:43: >>>>> On 2014-11-10 11:16, Karl Voelker wrote: >>>>>> On Mon, Nov 10, 2014, at 01:50 AM, Roelof Wobben wrote: >>>>>>> What do you experts think of the different ways ? >>>>>> 2 and 4 are quite similar, and both fine. 3 is not so good: guards >>>>>> provide weaker guarantees than patterns, and head and tail are partial >>>>>> functions. >>>>> In addition to what Karl wrote, I'd like to suggest not using the >>>>> semicolon all the time -- it's not needed and just adds noise. >>>>> >>>>>> All three implementations have in common that they do their own >>>>>> recursion. It would be a good exercise to try implementing last as a >>>>>> fold - in other words, letting the standard library do the recursion >>>>>> for >>>>>> you. >>>>> Right - I suggest trying to express the problem with the most abstract >>>>> function first, then consider using a fold, then use manual recursion. >>>>> in >>>>> your particular case you could exploit that for non-empty lists, getting >>>>> the last element of a list is the same as getting the first element of >>>>> a reversed list (and there are ready-made functions for reversing a list >>>>> and getting the first element of a list). >>>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://www.haskell.org/mailman/listinfo/beginners >>>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From efasckenoth at gmail.com Mon Nov 10 16:18:59 2014 From: efasckenoth at gmail.com (Stefan =?iso-8859-1?Q?H=F6ck?=) Date: Mon, 10 Nov 2014 17:18:59 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell Message-ID: <20141110161858.GB715@hunter> > I try to say that if the input list has only 1 item the outcome is the head > of that list. This is not how one typically thinks about folds. Look at the type of acc: acc :: a -> Maybe a -> Maybe a acc is a function wich takes two arguments (ignoring currying): On is of type `a`. This is the element type of the list you fold over. The other is of type `Maybe a`. This is the value you accumulate in your fold. The initial value is `Nothing`. You start your fold literally with nothing, since you haven't visited any element in the list yet. Now the first element of the list (see below) is passed to acc. What do you do with it? acc a Nothing = ??? a is an element in your lit, Nothing is the value you have accumulated so far. What do you do? Ignore a? Keep it? If you ignore a, you return `Nothing`. acc a Nothing = Nothing With this accumulator, the list will be traversed (ignoring lazy evaluation for the time being), all values will be ignored and your result will be Nothing, no matter what kind of list you fold over. Therefore, let's keep value `a`. In order to do that, we have to wrap it in a Just, otherwise the types won't match: acc a Nothing = Just a This function will give the right result for empty lists (Nothing) and for lists with a single value where the value is wrapped in a Just. The function will throw an error however, if you pass it a list containing more than one element, because the pattern match is not exhaustive. We need to decide what happens, when we already have a value wrapped in a Just: acc a Just b = ??? Here you have three possibilities. a) return Just b, b) return Just a, c) return Nothing. These are all the options you have. Anything else (except for throwing errors) will not compile. You can try each of the three. One will give you the head of the list, one will give you the last item of the list, and one will give you Nothing for lists with more than one element. Note that the result of your function depends on which fold you use. foldl folds from the left. The first element passed to the accumulating function will be the head of the list. foldr folds from the right. The first value passed to the accumulating function is the last in the list. So, think about the fold like this: Every element of the list is passed to your accumulator and each time you must decide whether you want to keep it or let it pass. If you keep them all, you will in the end hold the last element passed to your accumulator. If you let them all pass (except for the first, which you keep since you still have Nothing), you will end up with the first element passed to you. If you fold from the right, the first element you get is the last in the list. Keep it and don't let it go. If you fold from the left, the first element will be the head of the list. Throw them all away except for the very last one. Feel free to ask, if this doesn't make sense. Stefan > Stefan H?ck schreef op 10-11-2014 15:23: > >Just a quick note: That's 'typed holes' not 'type holes' ... > > > >On Mon, Nov 10, 2014 at 03:12:20PM +0100, Stefan H?ck wrote: > >>Hi Roelof > >> > >>It seems like you try to do too many things at once. Here's how you > >>could go about this step-by-step and let GHC help you implement your > >>functions along the way: > >> > >>First, give the type signature of your function: > >> > >> last5 :: [a] -> Maybe a > >> last5 = undefined > >> > >>Now, load this into GHCi or compile with GHC. If it compiles, you're > >>on the right track. Now, you want to implement it using a fold > >>(try both, foldl and foldr): > >> > >> last5 :: [a] -> Maybe a > >> last5 xs = foldr _ _ xs > >> > >>The underscores are 'type holes'. This tells the compiler to give you > >>some information about what is supposed to be placed at the two > >>positions. For the moment, we are only interested in the types of the > >>things that go there. The compiler will tell you, that > >>the hole at the first position is of type > >> > >> a -> Maybe a -> Maybe a > >> > >>and the hole at the second position is of type > >> > >> Maybe a > >> > >>Now, instead of filling the holes in place, let's define two helper > >>functions together with their type signatures. You can later on inline > >>them in your definition of last5, but for the time being, let's get as > >>much help from the compiler as we can. > >> > >> last5 :: [a] -> Maybe a > >> last5 xs = foldr acc initial xs > >> > >> acc :: a -> Maybe a -> Maybe a > >> acc = undefined > >> > >> initial :: Maybe a > >> initial = undefined > >> > >>Again, compile or load into GHCi. If you did anything wrong, the > >>compiler will tell you so. There is only one possible way to > >>implement function `initial` without cheating (= raising an error) > >> > >> initial :: Maybe a > >> initial = Nothing > >> > >>Function `acc` can be implemented in several ways. Only one of them > >>will lead to the desired behavior. Finding out the proper implementation > >>is the main point of this folding-exercise. Try also an implementation > >>using foldl. Does it behave as expected? What are the differences > >>compared to foldr? When you feed your implementations a huge list - > >>say [1..20000000] - what happens? > >> > >>Note that whenever you get an error message in a rather complex > >>function implementation, move local function definitions and lambdas > >>to the top level, give them a type signature and implement them > >>separately one at a time. Use type holes to let the compiler give > >>assistance with type signatures and possible implementations. > >>Once everything compiles and runs as expected, move the toplevel > >>definitions back to where you'd like them best. > >> > >>Stefan > >> > >>PS: A more succint implementation of last5 would use currying: > >> > >> last5 = foldr acc initial > >> > >>PPS: If you get a stack overflow with very large lists, try > >> using foldl' from Data.List (or better, once you learned > >> about the Foldable type class, from Data.Foldable). > >> > >>On Mon, Nov 10, 2014 at 02:28:08PM +0100, Roelof Wobben wrote: > >>>I tried and so far I have this : > >>> > >>>last5 = foldl(\acc x -> if x=[] then x else acc xs) [] xs > >>> > >>>But now I get parse error on the -> part. > >>> > >>>Roelof > >>> > >>> > >>>Roelof Wobben schreef op 10-11-2014 13:47: > >>>>Thanks all, > >>>> > >>>>I will try to make a fold solution as soon as I see that functional > >>>>explained in the learnyouahaskell. > >>>> > >>>>Roelof > >>>> > >>>> > >>>>Frerich Raabe schreef op 10-11-2014 11:43: > >>>>>On 2014-11-10 11:16, Karl Voelker wrote: > >>>>>>On Mon, Nov 10, 2014, at 01:50 AM, Roelof Wobben wrote: > >>>>>>>What do you experts think of the different ways ? > >>>>>>2 and 4 are quite similar, and both fine. 3 is not so good: guards > >>>>>>provide weaker guarantees than patterns, and head and tail are partial > >>>>>>functions. > >>>>>In addition to what Karl wrote, I'd like to suggest not using the > >>>>>semicolon all the time -- it's not needed and just adds noise. > >>>>> > >>>>>>All three implementations have in common that they do their own > >>>>>>recursion. It would be a good exercise to try implementing last as a > >>>>>>fold - in other words, letting the standard library do the recursion > >>>>>>for > >>>>>>you. > >>>>>Right - I suggest trying to express the problem with the most abstract > >>>>>function first, then consider using a fold, then use manual recursion. > >>>>>in > >>>>>your particular case you could exploit that for non-empty lists, getting > >>>>>the last element of a list is the same as getting the first element of > >>>>>a reversed list (and there are ready-made functions for reversing a list > >>>>>and getting the first element of a list). > >>>>> > >>>>_______________________________________________ > >>>>Beginners mailing list > >>>>Beginners at haskell.org > >>>>http://www.haskell.org/mailman/listinfo/beginners > >>>> > >>>_______________________________________________ > >>>Beginners mailing list > >>>Beginners at haskell.org > >>>http://www.haskell.org/mailman/listinfo/beginners > >>> > >_______________________________________________ > >Beginners mailing list > >Beginners at haskell.org > >http://www.haskell.org/mailman/listinfo/beginners > > > From r.wobben at home.nl Mon Nov 10 16:32:26 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Mon, 10 Nov 2014 17:32:26 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <20141110161858.GB715@hunter> References: <20141110161858.GB715@hunter> Message-ID: <5460E89A.3070301@home.nl> Stefan H?ck schreef op 10-11-2014 17:18: >> I try to say that if the input list has only 1 item the outcome is the head >> of that list. > This is not how one typically thinks about folds. Look at the type of > acc: > > acc :: a -> Maybe a -> Maybe a > > acc is a function wich takes two arguments (ignoring currying): On is of > type `a`. This is the element type of the list you fold over. The other > is of type `Maybe a`. This is the value you accumulate in your fold. > The initial value is `Nothing`. You start your fold literally with > nothing, since you haven't visited any element in the list yet. Now the > first element of the list (see below) is passed to acc. What do you do > with it? > > acc a Nothing = ??? > > a is an element in your lit, Nothing is the value you have accumulated > so far. What do you do? Ignore a? Keep it? If you ignore a, you return > `Nothing`. > > acc a Nothing = Nothing > > With this accumulator, the list will be traversed (ignoring lazy > evaluation for the time being), all values will be > ignored and your result will be Nothing, no matter what kind of list you > fold over. Therefore, let's keep value `a`. In order to do that, we have > to wrap it in a Just, otherwise the types won't match: > > acc a Nothing = Just a > > This function will give the right result for empty lists (Nothing) and > for lists with a single value where the value is wrapped in a Just. The > function will throw an error however, if you pass it a list containing > more than one element, because the pattern match is not exhaustive. > We need to decide what happens, when we already have a > value wrapped in a Just: > > acc a Just b = ??? > > It makes sence except when I enter acc a Just B I see this error message : ConstructorJustshould have 1 argument, but has been given none? From edwards.benj at gmail.com Mon Nov 10 16:39:21 2014 From: edwards.benj at gmail.com (Benjamin Edwards) Date: Mon, 10 Nov 2014 16:39:21 +0000 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell References: <20141110161858.GB715@hunter> <5460E89A.3070301@home.nl> Message-ID: This is simply a precedence problem. Judicious use of parentheses will solve it. On Mon Nov 10 2014 at 16:32:36 Roelof Wobben wrote: > Stefan H?ck schreef op 10-11-2014 17:18: > >> I try to say that if the input list has only 1 item the outcome is the > head > >> of that list. > > This is not how one typically thinks about folds. Look at the type of > > acc: > > > > acc :: a -> Maybe a -> Maybe a > > > > acc is a function wich takes two arguments (ignoring currying): On is of > > type `a`. This is the element type of the list you fold over. The other > > is of type `Maybe a`. This is the value you accumulate in your fold. > > The initial value is `Nothing`. You start your fold literally with > > nothing, since you haven't visited any element in the list yet. Now the > > first element of the list (see below) is passed to acc. What do you do > > with it? > > > > acc a Nothing = ??? > > > > a is an element in your lit, Nothing is the value you have accumulated > > so far. What do you do? Ignore a? Keep it? If you ignore a, you return > > `Nothing`. > > > > acc a Nothing = Nothing > > > > With this accumulator, the list will be traversed (ignoring lazy > > evaluation for the time being), all values will be > > ignored and your result will be Nothing, no matter what kind of list you > > fold over. Therefore, let's keep value `a`. In order to do that, we have > > to wrap it in a Just, otherwise the types won't match: > > > > acc a Nothing = Just a > > > > This function will give the right result for empty lists (Nothing) and > > for lists with a single value where the value is wrapped in a Just. The > > function will throw an error however, if you pass it a list containing > > more than one element, because the pattern match is not exhaustive. > > We need to decide what happens, when we already have a > > value wrapped in a Just: > > > > acc a Just b = ??? > > > > > > It makes sence except when I enter acc a Just B I see this error message : > ConstructorJustshould have 1 argument, but has been given none? > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From r.wobben at home.nl Mon Nov 10 16:45:30 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Mon, 10 Nov 2014 17:45:30 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: References: <20141110161858.GB715@hunter> <5460E89A.3070301@home.nl> Message-ID: <5460EBAA.1030202@home.nl> An HTML attachment was scrubbed... URL: From dontdieych at gmail.com Mon Nov 10 17:05:52 2014 From: dontdieych at gmail.com (Dontdie YCH) Date: Tue, 11 Nov 2014 02:05:52 +0900 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <20141110161858.GB715@hunter> References: <20141110161858.GB715@hunter> Message-ID: Another noob here. Thanks for this step by step guide. Your writing style is very attractive. Do you have blog about Haskell or any programming subject ? Thanks. 2014. 11. 11. ?? 1:19? "Stefan H?ck" ?? ??: > > I try to say that if the input list has only 1 item the outcome is the > head > > of that list. > > This is not how one typically thinks about folds. Look at the type of > acc: > > acc :: a -> Maybe a -> Maybe a > > acc is a function wich takes two arguments (ignoring currying): On is of > type `a`. This is the element type of the list you fold over. The other > is of type `Maybe a`. This is the value you accumulate in your fold. > The initial value is `Nothing`. You start your fold literally with > nothing, since you haven't visited any element in the list yet. Now the > first element of the list (see below) is passed to acc. What do you do > with it? > > acc a Nothing = ??? > > a is an element in your lit, Nothing is the value you have accumulated > so far. What do you do? Ignore a? Keep it? If you ignore a, you return > `Nothing`. > > acc a Nothing = Nothing > > With this accumulator, the list will be traversed (ignoring lazy > evaluation for the time being), all values will be > ignored and your result will be Nothing, no matter what kind of list you > fold over. Therefore, let's keep value `a`. In order to do that, we have > to wrap it in a Just, otherwise the types won't match: > > acc a Nothing = Just a > > This function will give the right result for empty lists (Nothing) and > for lists with a single value where the value is wrapped in a Just. The > function will throw an error however, if you pass it a list containing > more than one element, because the pattern match is not exhaustive. > We need to decide what happens, when we already have a > value wrapped in a Just: > > acc a Just b = ??? > > Here you have three possibilities. a) return Just b, b) return Just a, > c) return Nothing. These are all the options you have. Anything else > (except for throwing errors) will not compile. You can try each of the > three. One will give you the head of the list, one will give you the > last item of the list, and one will give you Nothing for lists with more > than one element. > > Note that the result of your function depends on which fold you use. > foldl folds from the left. The first element passed to the accumulating > function will be the head of the list. foldr folds from the right. > The first value passed to the accumulating function is the last in the > list. > > So, think about the fold like this: Every element of the list is passed > to your accumulator and each time you must decide whether you want to > keep it or let it pass. If you keep them all, you will in the end hold > the last element passed to your accumulator. If you let them all pass > (except for the first, which you keep since you still have Nothing), > you will end up with the first element passed to you. If you fold > from the right, the first element you get is the last in the list. Keep > it and don't let it go. If you fold from the left, the first element > will be the head of the list. Throw them all away except for the very last > one. > > Feel free to ask, if this doesn't make sense. > > Stefan > > > Stefan H?ck schreef op 10-11-2014 15:23: > > >Just a quick note: That's 'typed holes' not 'type holes' ... > > > > > >On Mon, Nov 10, 2014 at 03:12:20PM +0100, Stefan H?ck wrote: > > >>Hi Roelof > > >> > > >>It seems like you try to do too many things at once. Here's how you > > >>could go about this step-by-step and let GHC help you implement your > > >>functions along the way: > > >> > > >>First, give the type signature of your function: > > >> > > >> last5 :: [a] -> Maybe a > > >> last5 = undefined > > >> > > >>Now, load this into GHCi or compile with GHC. If it compiles, you're > > >>on the right track. Now, you want to implement it using a fold > > >>(try both, foldl and foldr): > > >> > > >> last5 :: [a] -> Maybe a > > >> last5 xs = foldr _ _ xs > > >> > > >>The underscores are 'type holes'. This tells the compiler to give you > > >>some information about what is supposed to be placed at the two > > >>positions. For the moment, we are only interested in the types of the > > >>things that go there. The compiler will tell you, that > > >>the hole at the first position is of type > > >> > > >> a -> Maybe a -> Maybe a > > >> > > >>and the hole at the second position is of type > > >> > > >> Maybe a > > >> > > >>Now, instead of filling the holes in place, let's define two helper > > >>functions together with their type signatures. You can later on inline > > >>them in your definition of last5, but for the time being, let's get as > > >>much help from the compiler as we can. > > >> > > >> last5 :: [a] -> Maybe a > > >> last5 xs = foldr acc initial xs > > >> > > >> acc :: a -> Maybe a -> Maybe a > > >> acc = undefined > > >> > > >> initial :: Maybe a > > >> initial = undefined > > >> > > >>Again, compile or load into GHCi. If you did anything wrong, the > > >>compiler will tell you so. There is only one possible way to > > >>implement function `initial` without cheating (= raising an error) > > >> > > >> initial :: Maybe a > > >> initial = Nothing > > >> > > >>Function `acc` can be implemented in several ways. Only one of them > > >>will lead to the desired behavior. Finding out the proper > implementation > > >>is the main point of this folding-exercise. Try also an implementation > > >>using foldl. Does it behave as expected? What are the differences > > >>compared to foldr? When you feed your implementations a huge list - > > >>say [1..20000000] - what happens? > > >> > > >>Note that whenever you get an error message in a rather complex > > >>function implementation, move local function definitions and lambdas > > >>to the top level, give them a type signature and implement them > > >>separately one at a time. Use type holes to let the compiler give > > >>assistance with type signatures and possible implementations. > > >>Once everything compiles and runs as expected, move the toplevel > > >>definitions back to where you'd like them best. > > >> > > >>Stefan > > >> > > >>PS: A more succint implementation of last5 would use currying: > > >> > > >> last5 = foldr acc initial > > >> > > >>PPS: If you get a stack overflow with very large lists, try > > >> using foldl' from Data.List (or better, once you learned > > >> about the Foldable type class, from Data.Foldable). > > >> > > >>On Mon, Nov 10, 2014 at 02:28:08PM +0100, Roelof Wobben wrote: > > >>>I tried and so far I have this : > > >>> > > >>>last5 = foldl(\acc x -> if x=[] then x else acc xs) [] xs > > >>> > > >>>But now I get parse error on the -> part. > > >>> > > >>>Roelof > > >>> > > >>> > > >>>Roelof Wobben schreef op 10-11-2014 13:47: > > >>>>Thanks all, > > >>>> > > >>>>I will try to make a fold solution as soon as I see that functional > > >>>>explained in the learnyouahaskell. > > >>>> > > >>>>Roelof > > >>>> > > >>>> > > >>>>Frerich Raabe schreef op 10-11-2014 11:43: > > >>>>>On 2014-11-10 11:16, Karl Voelker wrote: > > >>>>>>On Mon, Nov 10, 2014, at 01:50 AM, Roelof Wobben wrote: > > >>>>>>>What do you experts think of the different ways ? > > >>>>>>2 and 4 are quite similar, and both fine. 3 is not so good: guards > > >>>>>>provide weaker guarantees than patterns, and head and tail are > partial > > >>>>>>functions. > > >>>>>In addition to what Karl wrote, I'd like to suggest not using the > > >>>>>semicolon all the time -- it's not needed and just adds noise. > > >>>>> > > >>>>>>All three implementations have in common that they do their own > > >>>>>>recursion. It would be a good exercise to try implementing last as > a > > >>>>>>fold - in other words, letting the standard library do the > recursion > > >>>>>>for > > >>>>>>you. > > >>>>>Right - I suggest trying to express the problem with the most > abstract > > >>>>>function first, then consider using a fold, then use manual > recursion. > > >>>>>in > > >>>>>your particular case you could exploit that for non-empty lists, > getting > > >>>>>the last element of a list is the same as getting the first element > of > > >>>>>a reversed list (and there are ready-made functions for reversing a > list > > >>>>>and getting the first element of a list). > > >>>>> > > >>>>_______________________________________________ > > >>>>Beginners mailing list > > >>>>Beginners at haskell.org > > >>>>http://www.haskell.org/mailman/listinfo/beginners > > >>>> > > >>>_______________________________________________ > > >>>Beginners mailing list > > >>>Beginners at haskell.org > > >>>http://www.haskell.org/mailman/listinfo/beginners > > >>> > > >_______________________________________________ > > >Beginners mailing list > > >Beginners at haskell.org > > >http://www.haskell.org/mailman/listinfo/beginners > > > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From edwards.benj at gmail.com Mon Nov 10 17:07:02 2014 From: edwards.benj at gmail.com (Benjamin Edwards) Date: Mon, 10 Nov 2014 17:07:02 +0000 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell References: <20141110161858.GB715@hunter> <5460E89A.3070301@home.nl> <5460EBAA.1030202@home.nl> Message-ID: > It is complining but as soon as I want to run it , I see this message : > Non-exhaustive patterns in function acc Yep, Maybe has two constructors, and you are only handling one. You should handle the Nothing case as well. -------------- next part -------------- An HTML attachment was scrubbed... URL: From r.wobben at home.nl Mon Nov 10 17:28:46 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Mon, 10 Nov 2014 18:28:46 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: References: <20141110161858.GB715@hunter> <5460E89A.3070301@home.nl> <5460EBAA.1030202@home.nl> Message-ID: <5460F5CE.8080704@home.nl> An HTML attachment was scrubbed... URL: From leza.ml at fecrd.cujae.edu.cu Mon Nov 10 18:52:21 2014 From: leza.ml at fecrd.cujae.edu.cu (Leza Morais Lutonda) Date: Mon, 10 Nov 2014 13:52:21 -0500 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <5460D56D.5070709@home.nl> References: <54608A5F.3020707@home.nl> <1415614597.807471.189088073.447B4E41@webmail.messagingengine.com> <5460B3FB.7040807@home.nl> <5460BD68.3010705@home.nl> <20141110141220.GB692@hunter> <20141110142331.GC692@hunter> <5460D56D.5070709@home.nl> Message-ID: <54610965.6050209@fecrd.cujae.edu.cu> Hi Roelof, On 10/11/14 10:10, Roelof Wobben wrote: > No problem . > > Im strugelling to make acc work. > > I try to say that if the input list has only 1 item the outcome is the > head of that list. > But doing > > acc = Just (head a) or doing acc = Just (head acc) gives both that > acc or a is not in scope The `a` in the type signature `acc :: a -> Maybe a -> Maybe a` is not a variable name, it is a type. > > also doing acc x = Just (head x) gives a error messages that the types > are not matching. :t Just (head x) Maybe a :t acc x Maybe a -> Maybe a So, the compiler is right! > > Roelof > > > -- Leza Morais Lutonda, Lemol-C Electronic and Telecommunication Engineer Software Development and Architecture Enthusiast http://lemol.github.io 50 Aniversario de la Cujae. Inaugurada por Fidel el 2 de diciembre de 1964 http://cujae.edu.cu From r.wobben at home.nl Mon Nov 10 18:59:01 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Mon, 10 Nov 2014 19:59:01 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <54610965.6050209@fecrd.cujae.edu.cu> References: <54608A5F.3020707@home.nl> <1415614597.807471.189088073.447B4E41@webmail.messagingengine.com> <5460B3FB.7040807@home.nl> <5460BD68.3010705@home.nl> <20141110141220.GB692@hunter> <20141110142331.GC692@hunter> <5460D56D.5070709@home.nl> <54610965.6050209@fecrd.cujae.edu.cu> Message-ID: <54610AF5.2090506@home.nl> Of course the compiler is right. Im still struggeling. What I try to do is this 1) if acc is empty then a empty list so the output must be Nothing 2) if the input has only one item then it is the last so the output must be that item. 3) if the input has more then 1item then the last item is not reached so acc must have the value of the next item of the input file And I do not see how I can give acc the value of the next value of the input file maybe head xs Roelof Leza Morais Lutonda schreef op 10-11-2014 19:52: > Hi Roelof, > > On 10/11/14 10:10, Roelof Wobben wrote: >> No problem . >> >> Im strugelling to make acc work. >> >> I try to say that if the input list has only 1 item the outcome is >> the head of that list. >> But doing >> >> acc = Just (head a) or doing acc = Just (head acc) gives both that >> acc or a is not in scope > The `a` in the type signature `acc :: a -> Maybe a -> Maybe a` is not > a variable name, it is a type. >> >> also doing acc x = Just (head x) gives a error messages that the >> types are not matching. > :t Just (head x) > Maybe a > > :t acc x > Maybe a -> Maybe a > > So, the compiler is right! > > >> >> Roelof >> >> >> > > From julian.birch at gmail.com Mon Nov 10 19:41:10 2014 From: julian.birch at gmail.com (Julian Birch) Date: Mon, 10 Nov 2014 19:41:10 +0000 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <54610AF5.2090506@home.nl> References: <54608A5F.3020707@home.nl> <1415614597.807471.189088073.447B4E41@webmail.messagingengine.com> <5460B3FB.7040807@home.nl> <5460BD68.3010705@home.nl> <20141110141220.GB692@hunter> <20141110142331.GC692@hunter> <5460D56D.5070709@home.nl> <54610965.6050209@fecrd.cujae.edu.cu> <54610AF5.2090506@home.nl> Message-ID: Consider what happens if you apply the last function to xs. On Monday, November 10, 2014, Roelof Wobben wrote: > Of course the compiler is right. > > Im still struggeling. > > What I try to do is this > > 1) if acc is empty then a empty list so the output must be Nothing > 2) if the input has only one item then it is the last so the output must > be that item. > 3) if the input has more then 1item then the last item is not reached so > acc must have the value of the next item of the input file > > And I do not see how I can give acc the value of the next value of the > input file maybe head xs > > Roelof > > > Leza Morais Lutonda schreef op 10-11-2014 19:52: > >> Hi Roelof, >> >> On 10/11/14 10:10, Roelof Wobben wrote: >> >>> No problem . >>> >>> Im strugelling to make acc work. >>> >>> I try to say that if the input list has only 1 item the outcome is the >>> head of that list. >>> But doing >>> >>> acc = Just (head a) or doing acc = Just (head acc) gives both that acc >>> or a is not in scope >>> >> The `a` in the type signature `acc :: a -> Maybe a -> Maybe a` is not a >> variable name, it is a type. >> >>> >>> also doing acc x = Just (head x) gives a error messages that the types >>> are not matching. >>> >> :t Just (head x) >> Maybe a >> >> :t acc x >> Maybe a -> Maybe a >> >> So, the compiler is right! >> >> >> >>> Roelof >>> >>> >>> >>> >> >> > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -- Sent from an iPhone, please excuse brevity and typos. -------------- next part -------------- An HTML attachment was scrubbed... URL: From r.wobben at home.nl Mon Nov 10 19:47:01 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Mon, 10 Nov 2014 20:47:01 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: References: <54608A5F.3020707@home.nl> <1415614597.807471.189088073.447B4E41@webmail.messagingengine.com> <5460B3FB.7040807@home.nl> <5460BD68.3010705@home.nl> <20141110141220.GB692@hunter> <20141110142331.GC692@hunter> <5460D56D.5070709@home.nl> <54610965.6050209@fecrd.cujae.edu.cu> <54610AF5.2090506@home.nl> Message-ID: <54611635.7040209@home.nl> An HTML attachment was scrubbed... URL: From efasckenoth at gmail.com Mon Nov 10 19:47:28 2014 From: efasckenoth at gmail.com (Stefan =?iso-8859-1?Q?H=F6ck?=) Date: Mon, 10 Nov 2014 20:47:28 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <5460F5CE.8080704@home.nl> References: <20141110161858.GB715@hunter> <5460E89A.3070301@home.nl> <5460EBAA.1030202@home.nl> <5460F5CE.8080704@home.nl> Message-ID: <20141110194728.GA31159@hunter.zhaw.ch> Let's do this step by step. We use the following list: [1,2,3] foldr takes three arguments: foldr :: (a -> b -> b) -> b -> [a] -> b The last of the three has type [a]. This means, the fold expects a list as an argument here. The lower case letter `a` is the type of the items in the list. In our case this is `Int`, but we could pass it other lists with other element types. The second argument of the fold has type `b`, this is the result type we'd like to get from the fold. In our case this is `Maybe Int` since we want to know from the result, whether the list was empty or not. Nothing in case of an empty list, Just x with x being the right-most item in the non-empty case. The first argument of the fold is a higher order function. It accepts an argument of type `a` (the element type of the list) and of type `b` (the result type of the fold) and should return a value of type `b`. In our example, `a` is equal to `Int` and b is equal to `Maybe Int`. Therefore, the following cannot possibly work: > acc a acc = if null a then Nothing else if (null (tail a)) then (Just the first argument of acc (the value `a` in your implementation) has type `a` (note the distinction between a value and a type, both have the same name here, but that doesn't need to be the case. You could rename the value to x or foo or whatever). In our case, the type `a` is `Int` since we fold over a list of Ints. If we were to fold over a list of Strings, type `a` would be equal to `String`. Note however, that since `a` can be anything (Int, String etc, depending on the element type of your list), you cannot possibly call `null` on it. null takes a list as an argument, but `a` is not necessarily a list. It's the type of the list's elements, not the whole list. You do not pass the whole list to the accumulator. You only pass it the list's element in single file. Now, what happens, when we fold over the list? Since we fold from the right, our accumulator is passed two arguments: The last value of the list and the initial value of type `b` (in our case `Nothing`). acc 3 Nothing = ??? What should be the result of this? Clearly you'd like to keep the 3 as it is actually the result you are looking for. But you cannot return the 3 directly. The return type of our function must be `Maybe Int` and the type of `3` is `Int`. We first must wrap the `3` in a `Maybe`. There is only one way to do that: acc 3 Nothing = Maybe 3 (This is pseudocode an will not compile. The real implementation follows below) Now comes the next item in the list: `2`. Function acc gets called again, this time with `2` as its first argument, and the result of our fold so far which is `Just 3` acc 2 (Just 3) = ??? Clearly we do not want to lose the `3`, it's the result we are looking for after all! acc 2 (Just 3) = Just 3 Finally, the last item (from the right) in the list comes along: acc 1 (Just 3) = Just 3 Again we let it pass and get Just 3 as the result. This is how foldr works. Every element of the list starting with the rightmost is passed as an argument to the accumulator function together with the value accumulated so far (for which you must provide an initial value). Now, the implementation. We have seen, that we want to get hold of the very first value that comes from the list and ignore everything else that comes after it. acc :: a -> Maybe a -> Maybe a # This is the function's type signature acc x Nothing = Just x # We wrap the first value ... acc x (Just y) = Just y # and keep hold of it When we now use this accumulator in our fold, it gets first called with arguments `3` and `Nothing` so the first pattern matches. Variable x is assigned the value `3` and the result is `Just 3`. The function gets called again with the second value `2` and `Just 3`, the result from the first call. This time, the first pattern does not match (Nothing does not match `Just 3`), but the second does. `x` is assigned the value `2`, `y` is assigned the value `3` (the one wrapped in the `Just`) and the result is `Just 3`. Same for the last element. Here is the complete implementation: last5 :: [a] -> Maybe a last5 = foldr acc Nothing where acc x Nothing = Just x acc x (Just y) = Just y Or better last5 :: [a] -> Maybe a last5 = foldr acc Nothing where acc x Nothing = Just x acc _ j = j Or even (this one will be harder to figure out) last5 :: [a] -> Maybe a last5 = foldr acc Nothing where acc x = maybe (Just x) Just Cheers, Stefan On Mon, Nov 10, 2014 at 06:28:46PM +0100, Roelof Wobben wrote: > oke, > I try it with this : > acc :: a -> Maybe a -> Maybe a > acc a acc = if null a then Nothing else if (null (tail a)) then (Just > acc) else acc a acc > else > if the tail a is empty so there is only 1 item then the output must be > acc > else > must be run again ( there is a list with more then 1 item) > but now I see this error message : > Couldn't match expected type ?a -> Maybe a -> Maybe a? with actual type > ?Maybe a? Relevant bindings include acc :: Maybe a > and that is on the else part > Roelof > Benjamin Edwards schreef op 10-11-2014 18:07: > > > It is complining but as soon as I want to run it , I see this message > : > > Non-exhaustive patterns in function acc > > Yep, Maybe has two constructors, and you are only handling one. You > should handle the Nothing case as well. > > _______________________________________________ > Beginners mailing list > [1]Beginners at haskell.org > [2]http://www.haskell.org/mailman/listinfo/beginners > > References > > 1. mailto:Beginners at haskell.org > 2. http://www.haskell.org/mailman/listinfo/beginners > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From julian.birch at gmail.com Mon Nov 10 19:50:05 2014 From: julian.birch at gmail.com (Julian Birch) Date: Mon, 10 Nov 2014 19:50:05 +0000 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <54611635.7040209@home.nl> References: <54608A5F.3020707@home.nl> <1415614597.807471.189088073.447B4E41@webmail.messagingengine.com> <5460B3FB.7040807@home.nl> <5460BD68.3010705@home.nl> <20141110141220.GB692@hunter> <20141110142331.GC692@hunter> <5460D56D.5070709@home.nl> <54610965.6050209@fecrd.cujae.edu.cu> <54610AF5.2090506@home.nl> <54611635.7040209@home.nl> Message-ID: Me misunderstanding actually. I'd forgotten the foldr. The answer is that foldr calls your folding function multiple times, do you don't need to worry about it. On Monday, November 10, 2014, Roelof Wobben wrote: > Julian Birch schreef op 10-11-2014 20:41: > > > Consider what happens if you apply the last function to xs. > > > Then you get the last item but fold supposes to run over all items. > > Or do I misunderstood the question. > > Roelof > > > > > On Monday, November 10, 2014, Roelof Wobben > wrote: > >> Of course the compiler is right. >> >> Im still struggeling. >> >> What I try to do is this >> >> 1) if acc is empty then a empty list so the output must be Nothing >> 2) if the input has only one item then it is the last so the output must >> be that item. >> 3) if the input has more then 1item then the last item is not reached so >> acc must have the value of the next item of the input file >> >> And I do not see how I can give acc the value of the next value of the >> input file maybe head xs >> >> Roelof >> >> >> Leza Morais Lutonda schreef op 10-11-2014 19:52: >> >>> Hi Roelof, >>> >>> On 10/11/14 10:10, Roelof Wobben wrote: >>> >>>> No problem . >>>> >>>> Im strugelling to make acc work. >>>> >>>> I try to say that if the input list has only 1 item the outcome is the >>>> head of that list. >>>> But doing >>>> >>>> acc = Just (head a) or doing acc = Just (head acc) gives both that acc >>>> or a is not in scope >>>> >>> The `a` in the type signature `acc :: a -> Maybe a -> Maybe a` is not a >>> variable name, it is a type. >>> >>>> >>>> also doing acc x = Just (head x) gives a error messages that the types >>>> are not matching. >>>> >>> :t Just (head x) >>> Maybe a >>> >>> :t acc x >>> Maybe a -> Maybe a >>> >>> So, the compiler is right! >>> >>> >>> >>>> Roelof >>>> >>>> >>>> >>>> >>> >>> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > > -- > Sent from an iPhone, please excuse brevity and typos. > > > _______________________________________________ > Beginners mailing listBeginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners > > > -- Sent from an iPhone, please excuse brevity and typos. -------------- next part -------------- An HTML attachment was scrubbed... URL: From efasckenoth at gmail.com Mon Nov 10 19:58:18 2014 From: efasckenoth at gmail.com (Stefan =?iso-8859-1?Q?H=F6ck?=) Date: Mon, 10 Nov 2014 20:58:18 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: References: <20141110161858.GB715@hunter> Message-ID: <20141110195818.GB31159@hunter.zhaw.ch> I used to have a blog (well I still do), but it was about functional reactive programming in Scala. I'm actually quite a noob in Haskell myself but with some experince in functional programming in Scala. There is a Scala library called 'scalaz' which was inspired by all the cool concepts found in Haskell and Cathegory Theory and I learned lots of stuff there. Nowadays, two little children keep me busy. No time for blogs. :-) On Tue, Nov 11, 2014 at 02:05:52AM +0900, Dontdie YCH wrote: > Another noob here. > > Thanks for this step by step guide. Your writing style is very attractive. > > Do you have blog about Haskell or any programming subject ? > > Thanks. From r.wobben at home.nl Mon Nov 10 19:59:40 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Mon, 10 Nov 2014 20:59:40 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <20141110194728.GA31159@hunter.zhaw.ch> References: <20141110161858.GB715@hunter> <5460E89A.3070301@home.nl> <5460EBAA.1030202@home.nl> <5460F5CE.8080704@home.nl> <20141110194728.GA31159@hunter.zhaw.ch> Message-ID: <5461192C.6060800@home.nl> Stefan H?ck schreef op 10-11-2014 20:47: > Let's do this step by step. We use the following list: [1,2,3] > > foldr takes three arguments: > > foldr :: (a -> b -> b) -> b -> [a] -> b > > The last of the three has type [a]. > This means, the fold expects a list as an argument here. The > lower case letter `a` is the type of the items in the list. In > our case this is `Int`, but we could pass it other lists with > other element types. > > The second argument of the fold has type `b`, this is the result > type we'd like to get from the fold. In our case this is `Maybe Int` > since we want to know from the result, whether the list was empty > or not. Nothing in case of an empty list, Just x with x being the > right-most item in the non-empty case. > > The first argument of the fold is a higher order function. It > accepts an argument of type `a` (the element type of the list) > and of type `b` (the result type of the fold) and should return > a value of type `b`. In our example, `a` is equal to `Int` and b > is equal to `Maybe Int`. > > Therefore, the following cannot possibly work: > >> acc a acc = if null a then Nothing else if (null (tail a)) then (Just > the first argument of acc (the value `a` in your implementation) has > type `a` (note the distinction between a value and a type, both have > the same name here, but that doesn't need to be the case. You could > rename the value to x or foo or whatever). In our case, the type > `a` is `Int` since we fold over a list of Ints. If we were to fold > over a list of Strings, type `a` would be equal to `String`. Note > however, that since `a` can be anything (Int, String etc, depending > on the element type of your list), you cannot possibly call `null` > on it. null takes a list as an argument, but `a` is not necessarily a > list. It's the type of the list's elements, not the whole list. > You do not pass the whole list to the accumulator. You only pass it > the list's element in single file. > > Now, what happens, when we fold over the list? Since we fold from the > right, our accumulator is passed two arguments: The last value of the > list and the initial value of type `b` (in our case `Nothing`). > > acc 3 Nothing = ??? > > What should be the result of this? Clearly you'd like to keep the 3 as > it is actually the result you are looking for. But you cannot return the > 3 directly. The return type of our function must be `Maybe Int` and the > type of `3` is `Int`. We first must wrap the `3` in a `Maybe`. There > is only one way to do that: > > acc 3 Nothing = Maybe 3 > > (This is pseudocode an will not compile. The real implementation follows > below) > > Now comes the next item in the list: `2`. Function acc gets called > again, this time with `2` as its first argument, and the result of > our fold so far which is `Just 3` > > acc 2 (Just 3) = ??? > > Clearly we do not want to lose the `3`, it's the result we are looking > for after all! > > acc 2 (Just 3) = Just 3 > > Finally, the last item (from the right) in the list comes along: > > acc 1 (Just 3) = Just 3 > > Again we let it pass and get Just 3 as the result. > > This is how foldr works. Every element of the list starting with the > rightmost is passed as an argument to the accumulator function together > with the value accumulated so far (for which you must provide an initial > value). > > Now, the implementation. We have seen, that we want to get hold of > the very first value that comes from the list and ignore everything > else that comes after it. > > acc :: a -> Maybe a -> Maybe a # This is the function's type signature > acc x Nothing = Just x # We wrap the first value ... > acc x (Just y) = Just y # and keep hold of it > > When we now use this accumulator in our fold, it gets first called with > arguments `3` and `Nothing` so the first pattern matches. Variable x > is assigned the value `3` and the result is `Just 3`. > > The function gets called again with the second value `2` and `Just 3`, > the result from the first call. This time, the first pattern does not > match (Nothing does not match `Just 3`), but the second does. `x` is > assigned the value `2`, `y` is assigned the value `3` (the one wrapped > in the `Just`) and the result is `Just 3`. Same for the last element. > > Here is the complete implementation: > > last5 :: [a] -> Maybe a > last5 = foldr acc Nothing where > acc x Nothing = Just x > acc x (Just y) = Just y > > Or better > > last5 :: [a] -> Maybe a > last5 = foldr acc Nothing where > acc x Nothing = Just x > acc _ j = j > > Or even (this one will be harder to figure out) > last5 :: [a] -> Maybe a > last5 = foldr acc Nothing where > acc x = maybe (Just x) Just > > Cheers, Stefan > > I understand the theory but i loose it on the implementation. Lets take the better variant. last5 :: [a] -> Maybe a last5 = foldr acc Nothing where acc x Nothing = Just x acc _ j = j Let's say we have [1,2,3] Do I understand that acc = Nothing. and x = 3 ? and after acc x Nothing = Just x acc get's the value 3 But how does x get's the value 3 or does foldr takes care of that ? Roelof From efasckenoth at gmail.com Mon Nov 10 20:15:12 2014 From: efasckenoth at gmail.com (Stefan =?iso-8859-1?Q?H=F6ck?=) Date: Mon, 10 Nov 2014 21:15:12 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <5461192C.6060800@home.nl> References: <20141110161858.GB715@hunter> <5460E89A.3070301@home.nl> <5460EBAA.1030202@home.nl> <5460F5CE.8080704@home.nl> <20141110194728.GA31159@hunter.zhaw.ch> <5461192C.6060800@home.nl> Message-ID: <20141110201512.GC31159@hunter.zhaw.ch> > > I understand the theory but i loose it on the implementation. > > Lets take the better variant. > > last5 :: [a] -> Maybe a > last5 = foldr acc Nothing where > acc x Nothing = Just x > acc _ j = j > > > Let's say we have [1,2,3] > > Do I understand that acc = Nothing. and x = 3 ? Not, acc is not Nothing. acc is a function of type (a -> Maybe a -> Maybe a). Nothing is a value of type Maybe a. The two cannot possibly be the same. foldr takes care of feeding the proper arguments to `acc`. > and after acc x Nothing = Just x acc get's the value 3 The result of calling `acc` with arguments 3 and Nothing is `Just 3`. But that's NOT the value of `acc`. acc is still a function. Note that there is no such thing as in-place mutation in Haskell, so you cannot just change the value of `acc`. But the RESULT of calling acc 3 Nothing is `Just 3`. This result is taken by foldr and passed again to `acc` together with the next item in the list. This continues until the list is exhausted and foldr returns the last result it got from calling `acc`. > But how does x get's the value 3 or does foldr takes care of that ? foldr takes care of all that. foldr calls `acc` with the proper arguments and does all the bookkeeping internally. There is actually not much to bookkeep. Here's an implementation of foldr foldr :: (a -> b -> b) -> b -> [a] -> b foldr _ b [] = b foldr f b (a:as) = foldr f (f a b) as So, no great bookkeeping, just plain recursion. Stefan From r.wobben at home.nl Mon Nov 10 20:43:38 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Mon, 10 Nov 2014 21:43:38 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <20141110201512.GC31159@hunter.zhaw.ch> References: <20141110161858.GB715@hunter> <5460E89A.3070301@home.nl> <5460EBAA.1030202@home.nl> <5460F5CE.8080704@home.nl> <20141110194728.GA31159@hunter.zhaw.ch> <5461192C.6060800@home.nl> <20141110201512.GC31159@hunter.zhaw.ch> Message-ID: <5461237A.3060604@home.nl> Thanks, I think my error in thinking is that most of the languages store values in a variable, But it seems that haskell do not use it in a fold Roelof Stefan H?ck schreef op 10-11-2014 21:15: >> I understand the theory but i loose it on the implementation. >> >> Lets take the better variant. >> >> last5 :: [a] -> Maybe a >> last5 = foldr acc Nothing where >> acc x Nothing = Just x >> acc _ j = j >> >> >> Let's say we have [1,2,3] >> >> Do I understand that acc = Nothing. and x = 3 ? > Not, acc is not Nothing. acc is a function of type (a -> Maybe a -> > Maybe a). Nothing is a value of type Maybe a. The two cannot > possibly be the same. > foldr takes care of feeding the proper arguments to `acc`. > >> and after acc x Nothing = Just x acc get's the value 3 > The result of calling `acc` with arguments 3 and Nothing is `Just 3`. > But that's NOT the value of `acc`. acc is still a function. Note that > there is no such thing as in-place mutation in Haskell, so you > cannot just change the value of `acc`. But the RESULT of calling > > acc 3 Nothing > > is `Just 3`. This result is taken by foldr and passed again to `acc` > together with the next item in the list. This continues until > the list is exhausted and foldr returns the last result it got from > calling `acc`. > >> But how does x get's the value 3 or does foldr takes care of that ? > foldr takes care of all that. foldr calls `acc` with the proper > arguments and does all the bookkeeping internally. There is actually > not much to bookkeep. Here's an implementation of foldr > > foldr :: (a -> b -> b) -> b -> [a] -> b > foldr _ b [] = b > foldr f b (a:as) = foldr f (f a b) as > > So, no great bookkeeping, just plain recursion. > > Stefan > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From r.wobben at home.nl Mon Nov 10 20:48:11 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Mon, 10 Nov 2014 21:48:11 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <5461192C.6060800@home.nl> References: <20141110161858.GB715@hunter> <5460E89A.3070301@home.nl> <5460EBAA.1030202@home.nl> <5460F5CE.8080704@home.nl> <20141110194728.GA31159@hunter.zhaw.ch> <5461192C.6060800@home.nl> Message-ID: <5461248B.3020605@home.nl> Roelof Wobben schreef op 10-11-2014 20:59: > last5 :: [a] -> Maybe a > last5 = foldr acc Nothing where > acc x Nothing = Just x > acc _ j = j When I enter this in GHCI I see this error : Illegal type signature: ?[a] -> Maybe a last5? From ahammel87 at gmail.com Mon Nov 10 21:47:05 2014 From: ahammel87 at gmail.com (Alex Hammel) Date: Mon, 10 Nov 2014 13:47:05 -0800 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <5461248B.3020605@home.nl> References: <20141110161858.GB715@hunter> <5460E89A.3070301@home.nl> <5460EBAA.1030202@home.nl> <5460F5CE.8080704@home.nl> <20141110194728.GA31159@hunter.zhaw.ch> <5461192C.6060800@home.nl> <5461248B.3020605@home.nl> Message-ID: It's an indentation issue. Make sure that there are no spaces before the bit about 'last5 = foldr acc' etc. On Mon, Nov 10, 2014 at 12:48 PM, Roelof Wobben wrote: > Roelof Wobben schreef op 10-11-2014 20:59: > >> last5 :: [a] -> Maybe a >> last5 = foldr acc Nothing where >> acc x Nothing = Just x >> acc _ j = j >> > > When I enter this in GHCI I see this error : > > Illegal type signature: ?[a] -> Maybe a last5? > > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jack.mott at gmail.com Tue Nov 11 00:32:14 2014 From: jack.mott at gmail.com (Jack Mott) Date: Mon, 10 Nov 2014 18:32:14 -0600 Subject: [Haskell-beginners] Finding Keith Numbers with Haskell Message-ID: I'm on day 2 of learning Haskell, and decided to write a program to find Keith Numbers as my first project. So far so good, I have some code that does the job (see below). First I would just like to ask others to look it over and let me know if I am doing anything particularly wrong or not advised here. Also, I am using the "isKeithHelper" function because I didn't know how to make use of the filter function directly with isKeith, because isKeith takes two parameters. I'm pretty sure there is a better way to handle that, any tips? Lastly, I'd like to make this as efficient as possible. At the moment I am adding [s] to the end of the list in the isKeith function, which I know is O(n) instead of O(1). I don't think there is a way to avoid either that or having to use drop which would have the same downside. Is this a case where a Sequence or Vector might be more useful? How would I convert this to use a Sequence instead? import Data.Time.Clock.POSIX import Data.Digits isKeith :: Integer -> [Integer] -> Bool isKeith n l | s == n = True | s > n = False | otherwise = isKeith n x (l ++ [s]) where s = sum l isKeithHelper :: Integer -> Bool isKeithHelper n = isKeith n (digits 10 n) main :: IO () main = do start <- getPOSIXTime print (filter isKeithHelper [1000..9999]) end <- getPOSIXTime print (end - start) -------------- next part -------------- An HTML attachment was scrubbed... URL: From rein.henrichs at gmail.com Tue Nov 11 02:50:23 2014 From: rein.henrichs at gmail.com (Rein Henrichs) Date: Mon, 10 Nov 2014 18:50:23 -0800 Subject: [Haskell-beginners] Finding Keith Numbers with Haskell In-Reply-To: References: Message-ID: Since summing is commutative (sum [a,b] = sum [b,a]), it doesn't matter what order they appear in. You can start out by reversing the digits and then by taking from and adding to the beginning of the list instead of the end. Also, isKeith needs to know the length of the decimal representation of n. Since you are already computing that representation in isKeithHelper, it might be best to compute this there as well and pass it into isKeith. (Also please consider the criterion library for benchmarking.) > import Data.Digits > isKeith :: Int -> Bool > isKeith n = let ds = digitsRev 10 n in isKeith' n (length ds) ds > isKeith' :: Int -> Int -> [Int] -> Bool > isKeith' n len ds > | n == s = True > | s > n = False > | otherwise = isKeith' n len (take len (s : ds)) > where s = sum ds > main :: IO () > main = print (isKeith 197) True -------------- next part -------------- An HTML attachment was scrubbed... URL: From frankdmartinez at gmail.com Tue Nov 11 06:01:05 2014 From: frankdmartinez at gmail.com (Frank) Date: Tue, 11 Nov 2014 01:01:05 -0500 Subject: [Haskell-beginners] Haskell and LiveCode Message-ID: How do the 2 languages compare? -- P.S.: I prefer to be reached on BitMessage at BM-2D8txNiU7b84d2tgqvJQdgBog6A69oDAx6 -------------- next part -------------- An HTML attachment was scrubbed... URL: From r.wobben at home.nl Tue Nov 11 07:20:40 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Tue, 11 Nov 2014 08:20:40 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: References: <20141110161858.GB715@hunter> <5460E89A.3070301@home.nl> <5460EBAA.1030202@home.nl> <5460F5CE.8080704@home.nl> <20141110194728.GA31159@hunter.zhaw.ch> <5461192C.6060800@home.nl> <5461248B.3020605@home.nl> Message-ID: <5461B8C8.2050702@home.nl> An HTML attachment was scrubbed... URL: From julian.birch at gmail.com Tue Nov 11 08:35:13 2014 From: julian.birch at gmail.com (Julian Birch) Date: Tue, 11 Nov 2014 08:35:13 +0000 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <5461B8C8.2050702@home.nl> References: <20141110161858.GB715@hunter> <5460E89A.3070301@home.nl> <5460EBAA.1030202@home.nl> <5460F5CE.8080704@home.nl> <20141110194728.GA31159@hunter.zhaw.ch> <5461192C.6060800@home.nl> <5461248B.3020605@home.nl> <5461B8C8.2050702@home.nl> Message-ID: _ just means "ignore this parameter, I'm not going to use it" On Tuesday, November 11, 2014, Roelof Wobben wrote: > Thanks, this is working well. > > But just for understanding > > in the part acc x Nothing x is the input string, > and in the part _j means the same as (xs:x) > > Roelof > > > > Alex Hammel schreef op 10-11-2014 22:47: > > It's an indentation issue. Make sure that there are no spaces before the > bit about 'last5 = foldr acc' etc. > > On Mon, Nov 10, 2014 at 12:48 PM, Roelof Wobben > wrote: > >> Roelof Wobben schreef op 10-11-2014 20:59: >> >>> last5 :: [a] -> Maybe a >>> last5 = foldr acc Nothing where >>> acc x Nothing = Just x >>> acc _ j = j >>> >> >> When I enter this in GHCI I see this error : >> >> Illegal type signature: ?[a] -> Maybe a last5? >> >> >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> >> http://www.haskell.org/mailman/listinfo/beginners >> > > > > _______________________________________________ > Beginners mailing listBeginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners > > > -- Sent from an iPhone, please excuse brevity and typos. -------------- next part -------------- An HTML attachment was scrubbed... URL: From r.wobben at home.nl Tue Nov 11 13:20:25 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Tue, 11 Nov 2014 14:20:25 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <5461B8C8.2050702@home.nl> References: <20141110161858.GB715@hunter> <5460E89A.3070301@home.nl> <5460EBAA.1030202@home.nl> <5460F5CE.8080704@home.nl> <20141110194728.GA31159@hunter.zhaw.ch> <5461192C.6060800@home.nl> <5461248B.3020605@home.nl> <5461B8C8.2050702@home.nl> Message-ID: <54620D19.8020805@home.nl> An HTML attachment was scrubbed... URL: From jack.mott at gmail.com Tue Nov 11 13:26:53 2014 From: jack.mott at gmail.com (Jack Mott) Date: Tue, 11 Nov 2014 07:26:53 -0600 Subject: [Haskell-beginners] Finding Keith Numbers with Haskell In-Reply-To: References: Message-ID: Thanks, is the notation with the single quote (isKeith') a convention for helper functions or does it actually change something about the program? I did try this approach as well, reversing the digits and adding elements to the head, however having to use take and letting the list build up larger seems to result in slightly worse performance overall. On Mon, Nov 10, 2014 at 8:50 PM, Rein Henrichs wrote: > Since summing is commutative (sum [a,b] = sum [b,a]), it doesn't matter > what order they appear in. You can start out by reversing the digits and > then by taking from and adding to the beginning of the list instead of the > end. Also, isKeith needs to know the length of the decimal representation > of n. Since you are already computing that representation in isKeithHelper, > it might be best to compute this there as well and pass it into isKeith. > > (Also please consider the criterion library for benchmarking.) > > > import Data.Digits > > > isKeith :: Int -> Bool > > isKeith n = let ds = digitsRev 10 n in isKeith' n (length ds) ds > > > isKeith' :: Int -> Int -> [Int] -> Bool > > isKeith' n len ds > > | n == s = True > > | s > n = False > > | otherwise = isKeith' n len (take len (s : ds)) > > where s = sum ds > > > main :: IO () > > main = print (isKeith 197) > > True > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From k.bleijenberg at lijbrandt.nl Tue Nov 11 14:46:02 2014 From: k.bleijenberg at lijbrandt.nl (Kees Bleijenberg) Date: Tue, 11 Nov 2014 15:46:02 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <54620D19.8020805@home.nl> References: <20141110161858.GB715@hunter> <5460E89A.3070301@home.nl> <5460EBAA.1030202@home.nl> <5460F5CE.8080704@home.nl> <20141110194728.GA31159@hunter.zhaw.ch> <5461192C.6060800@home.nl> <5461248B.3020605@home.nl> <5461B8C8.2050702@home.nl> <54620D19.8020805@home.nl> Message-ID: <000601cffdbe$374a1810$a5de4830$@bleijenberg@lijbrandt.nl> The program traverses the list from right to left. The first element that acc sees during traversal is the last element of the list. The idea is as follows: Parameter y is the partial calcresult. If y (in your code) is Nothing then acc returns Just x. So the new partial result is the current list element in a Just. The next time acc is called, y is not Nothing but a Just. In that case acc returns the partial calcresult unchanged. So, y is Nothing for the last list element and Just for all other list elements. last5 :: [a] -> Maybe a last5 xs = foldr acc initial xs acc :: a -> Maybe a -> Maybe a acc x Nothing = Just x acc _ aJust = aJust initial :: Maybe a initial = Nothing In your code you don?t need the ?where? in the acc definition. Define acc for Nothing and for Just. Kees --------------- Im now trying to make a rfold solution to this problem. Till now I have this : last5 :: [a] -> Maybe a last5 xs = foldr acc initial xs acc :: a -> Maybe a -> Maybe a acc x y = where ??? acc x Nothing? = Just x ??? acc _ j??????? = j initial :: Maybe a initial = Nothing So still not working. Can someone explain to me in small steps how to figure out what the right answer is to the acc part. Roelof Roelof Wobben schreef op 11-11-2014 8:20: Thanks, this is working well. But just for understanding in the part acc x Nothing?? x is the input string, and in the part _j means the same as (xs:x) Roelof ? Alex Hammel schreef op 10-11-2014 22:47: It's an indentation issue. Make sure that there are no spaces before the bit about 'last5 = foldr acc' etc. On Mon, Nov 10, 2014 at 12:48 PM, Roelof Wobben wrote: Roelof Wobben schreef op 10-11-2014 20:59: last5 :: [a] -> Maybe a ? ?last5 = foldr acc Nothing where ? ? ? ?acc x Nothing? = Just x ? ? ? ?acc _ j? ? ? ? = j When I enter this in GHCI? I see this error : Illegal type signature: ?[a] -> Maybe a last5? _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners From ky3 at atamo.com Tue Nov 11 15:17:04 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Tue, 11 Nov 2014 22:17:04 +0700 Subject: [Haskell-beginners] Finding Keith Numbers with Haskell In-Reply-To: References: Message-ID: On Tue, Nov 11, 2014 at 8:26 PM, Jack Mott wrote: > Thanks, is the notation with the single quote (isKeith') a convention for > helper functions or does it actually change something about the program? An unspoken coding convention. Function foo' would be read "foo-prime". E.g.of usage: haskell-prime is the next standard of haskell, which became moot because you need good vibes for diverse peoples to collaborate on such an undertaking, including supporting it by writing more than one implementation. Strictly syntax, the compiler doesn't treat it differently from any other name label. So yes, you could have foo-double-prime and so forth. -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From r.wobben at home.nl Tue Nov 11 16:35:28 2014 From: r.wobben at home.nl (Roelof Wobben) Date: Tue, 11 Nov 2014 17:35:28 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <000601cffdbe$374a1810$a5de4830$@bleijenberg@lijbrandt.nl> References: <20141110161858.GB715@hunter> <5460E89A.3070301@home.nl> <5460EBAA.1030202@home.nl> <5460F5CE.8080704@home.nl> <20141110194728.GA31159@hunter.zhaw.ch> <5461192C.6060800@home.nl> <5461248B.3020605@home.nl> <5461B8C8.2050702@home.nl> <54620D19.8020805@home.nl> <000601cffdbe$374a1810$a5de4830$@bleijenberg@lijbrandt.nl> Message-ID: <54623AD0.20407@home.nl> Oke, But what If I try it on a left folt. Then it must be the other way. let's say we have [1 , 2, 3] so first it's nothing and 1 is taken. so y could be one and nothuing must stay nothing because it's not the last part, The same for 2 , When it's three nothing must changed to just 3. Fold is something I find it very difficult to wrap my head around it. Maybe haskell or any functional proggramming language is not for me, Roelof Kees Bleijenberg schreef op 11-11-2014 15:46: > The program traverses the list from right to left. The first element that > acc sees during traversal is the last element of the list. > The idea is as follows: > Parameter y is the partial calcresult. > If y (in your code) is Nothing then acc returns Just x. So the new partial > result is the current list element in a Just. > The next time acc is called, y is not Nothing but a Just. In that case acc > returns the partial calcresult unchanged. > So, y is Nothing for the last list element and Just list> for all other list elements. > > last5 :: [a] -> Maybe a > last5 xs = foldr acc initial xs > > acc :: a -> Maybe a -> Maybe a > acc x Nothing = Just x > acc _ aJust = aJust > > initial :: Maybe a > initial = Nothing > > In your code you don?t need the ?where? in the acc definition. Define acc > for Nothing and for Just. > > Kees > > --------------- > Im now trying to make a rfold solution to this problem. > > Till now I have this : > > last5 :: [a] -> Maybe a > last5 xs = foldr acc initial xs > > acc :: a -> Maybe a -> Maybe a > acc x y = where > acc x Nothing = Just x > acc _ j = j > > initial :: Maybe a > initial = Nothing > > So still not working. > > Can someone explain to me in small steps how to figure out what the right > answer is to the acc part. > > Roelof > > > > > > > > > > Roelof Wobben schreef op 11-11-2014 8:20: > Thanks, this is working well. > > But just for understanding > > in the part acc x Nothing x is the input string, > and in the part _j means the same as (xs:x) > > Roelof > > > > Alex Hammel schreef op 10-11-2014 22:47: > It's an indentation issue. Make sure that there are no spaces before the bit > about 'last5 = foldr acc' etc. > > On Mon, Nov 10, 2014 at 12:48 PM, Roelof Wobben wrote: > Roelof Wobben schreef op 10-11-2014 20:59: > last5 :: [a] -> Maybe a > last5 = foldr acc Nothing where > acc x Nothing = Just x > acc _ j = j > > When I enter this in GHCI I see this error : > > Illegal type signature: ?[a] -> Maybe a last5? > > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From k.bleijenberg at lijbrandt.nl Tue Nov 11 17:43:21 2014 From: k.bleijenberg at lijbrandt.nl (Kees Bleijenberg) Date: Tue, 11 Nov 2014 18:43:21 +0100 Subject: [Haskell-beginners] Are these soloutions all valid and a good use of Haskell In-Reply-To: <54623AD0.20407@home.nl> References: <20141110161858.GB715@hunter> <5460E89A.3070301@home.nl> <5460EBAA.1030202@home.nl> <5460F5CE.8080704@home.nl> <20141110194728.GA31159@hunter.zhaw.ch> <5461192C.6060800@home.nl> <5461248B.3020605@home.nl> <5461B8C8.2050702@home.nl> <54620D19.8020805@home.nl> <000601cffdbe$374a1810$a5de4830$@bleijenberg@lijbrandt.nl> <54623AD0.20407@home.nl> Message-ID: <000001cffdd6$fc774fa0$f565eee0$@bleijenberg@lijbrandt.nl> Roelof, The acc for the foldr doesn't work in foldl. But foldl is easier then foldr. foldl traverses the list from left to right. For foldl acc returns the current list element (second param) in a Just and ignores the result of the partial calculation (param 1). Walking through the list [1,2,4], acc returns for the first element Just 1, for the second element Just 2 en finally Just 4. The result of the foldl is in this case Just 4. If the foldl is over the empty list, acc is never called and foldl returns initial, so Nothing. In code: module Test() where last5 :: [a] -> Maybe a last5 xs = foldl acc initial xs acc :: Maybe a -> a -> Maybe a -- swapped params! acc _ x = Just x initial :: Maybe a initial = Nothing Kees -----Oorspronkelijk bericht----- Oke, But what If I try it on a left folt. Then it must be the other way. let's say we have [1 , 2, 3] so first it's nothing and 1 is taken. so y could be one and nothuing must stay nothing because it's not the last part, The same for 2 , When it's three nothing must changed to just 3. Fold is something I find it very difficult to wrap my head around it. Maybe haskell or any functional proggramming language is not for me, Roelof Kees Bleijenberg schreef op 11-11-2014 15:46: > The program traverses the list from right to left. The first element > that acc sees during traversal is the last element of the list. > The idea is as follows: > Parameter y is the partial calcresult. > If y (in your code) is Nothing then acc returns Just x. So the new > partial result is the current list element in a Just. > The next time acc is called, y is not Nothing but a Just. In that case > acc returns the partial calcresult unchanged. > So, y is Nothing for the last list element and Just the > list> for all other list elements. > > last5 :: [a] -> Maybe a > last5 xs = foldr acc initial xs > > acc :: a -> Maybe a -> Maybe a > acc x Nothing = Just x > acc _ aJust = aJust > > initial :: Maybe a > initial = Nothing > > In your code you don't need the 'where' in the acc definition. Define > acc for Nothing and for Just. > > Kees --- snip ---- From defigueiredo at ucdavis.edu Wed Nov 12 00:47:11 2014 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Tue, 11 Nov 2014 17:47:11 -0700 Subject: [Haskell-beginners] Can I set Cabal conditional options for all build targets In-Reply-To: <54623AD0.20407@home.nl> References: <20141110161858.GB715@hunter> <5460E89A.3070301@home.nl> <5460EBAA.1030202@home.nl> <5460F5CE.8080704@home.nl> <20141110194728.GA31159@hunter.zhaw.ch> <5461192C.6060800@home.nl> <5461248B.3020605@home.nl> <5461B8C8.2050702@home.nl> <54620D19.8020805@home.nl> <000601cffdbe$374a1810$a5de4830$@bleijenberg@lijbrandt.nl> <54623AD0.20407@home.nl> Message-ID: <5462AE0F.9090709@ucdavis.edu> Hi, Is there a way to set a conditional in a .cabal file that applies to all the build targets in the file? I have many build targets and repeating the conditional for each of them doesn't make sense. But I get an error message when doing that. For example, I can do this in my example.cabal file: ---- name: MyExample build-type: Simple -- the conditional flag I want to use Flag production Description: Attempts to get production env Default: True executable client main-is: client.hs if flag(production) CPP-Options: -DPRODUCTION executable server main-is: server.hs if flag(production) CPP-Options: -DPRODUCTION ------ So I have to specify the options once per target. I get an error message if I move the "if flag(production)" outside an "executable" block. And the docs "seem to indicate" that's not possible. Thanks, Dimitri PS. I'm using cabal "1.20.0.3" From rein.henrichs at gmail.com Wed Nov 12 01:44:45 2014 From: rein.henrichs at gmail.com (Rein Henrichs) Date: Tue, 11 Nov 2014 17:44:45 -0800 Subject: [Haskell-beginners] Finding Keith Numbers with Haskell In-Reply-To: References: Message-ID: On Tue, Nov 11, 2014 at 5:26 AM, Jack Mott wrote: > I did try this approach as well, reversing the digits and adding elements > to the head, however having to use take and letting the list build up > larger seems to result in slightly worse performance overall. > The list doesn't build up. The specification for Keith Numbers requires that we only sum the previous n numbers, where n is the length of the decimal encoding of the given number, so we always truncate using take before the recursive call. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alexander at plaimi.net Wed Nov 12 09:07:03 2014 From: alexander at plaimi.net (Alexander Berntsen) Date: Wed, 12 Nov 2014 10:07:03 +0100 Subject: [Haskell-beginners] Finding Keith Numbers with Haskell In-Reply-To: References: Message-ID: <54632337.407@plaimi.net> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 On 11/11/14 16:17, Kim-Ee Yeoh wrote: > On Tue, Nov 11, 2014 at 8:26 PM, Jack Mott > wrote: > >> Thanks, is the notation with the single quote (isKeith') a >> convention for helper functions or does it actually change >> something about the program? > > > An unspoken coding convention. > > Function foo' would be read "foo-prime". E.g.of usage: > haskell-prime is the next standard of haskell, which became moot > because you need good vibes for diverse peoples to collaborate on > such an undertaking, including supporting it by writing more than > one implementation. > > Strictly syntax, the compiler doesn't treat it differently from > any other name label. > > So yes, you could have foo-double-prime and so forth. It's inherited from maths (like everything else). Very commonly used for a changed/new version of something. Comes up a lot in games programming. One banal example: update :: World -> World update (World p p2 b e) = let p' = update p p2' = update p2 b' = update b e' = update e in World p' p2' b' e' where update = draw . move . react - -- Alexander alexander at plaimi.net https://secure.plaimi.net/~alexander -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iF4EAREIAAYFAlRjIzcACgkQRtClrXBQc7WPHwEAuIDb/vALYJ6Duz2ZDdB/tqHs l35idQYgOkngDrVe4pwA/0PcF4IUv8MwBAOI3kE8OiBDFTPhsS2SlRtmgihqXwty =XyN/ -----END PGP SIGNATURE----- From dragos-adrian.seredinschi at epfl.ch Wed Nov 12 20:15:25 2014 From: dragos-adrian.seredinschi at epfl.ch (Adrian Seredinschi) Date: Wed, 12 Nov 2014 20:15:25 +0000 (UTC) Subject: [Haskell-beginners] latest Haskell Platform build fails References: <543EB50E.3070406@gmail.com> Message-ID: Michael L Martin gmail.com> writes: > > Hello all, > > I am trying to install the latest version of Haskell Platform and am > having a problem. > ... > Exit code: 1 > Stderr: > mtl-2.1.3.1: package(s) with this id already exist: mtl-2.1.3.1 > > Any suggestions on how to get past this? > > Thanks, > Michael Martin > > Encountered the exact same problem. I'm looking for a workaround, but nothing yet. Did you fix it eventually? From rohits79 at gmail.com Sat Nov 15 12:10:55 2014 From: rohits79 at gmail.com (Rohit Sharma) Date: Sat, 15 Nov 2014 20:10:55 +0800 Subject: [Haskell-beginners] Bind parser returning wrong return type Message-ID: Hi All, Sorry if this is a very lame question but i am a beginner and much appreciate if some one could correct me? I am reading haskell book and curious why the return type of the bind operator look odd to me For the given definitions * type Parser a = String -> [(a, String)]* * item :: Parser Char* * item = \inp -> case inp of * * [] -> []* * (x:xs) -> [(x,xs)]* * bind :: Parser a -> (a -> Parser b) -> Parser b* * p `bind` f = \inp -> concat [ f x inp' | (x, inp') <- p inp]* when I define z in GHCI as * let z = item `bind` (\x -> (\y -> result (x,y))) "Rohit"* the return type is * >> :t z* * z :: Parser ([Char], Char)* Question: (1) Shouldn't the return type of (Char, [Char])? looking at the list comprehension, "(x, inp') <- p inp" should yield -> "('r', "ohit")". Next f x inp' is left associative, so f x should yield character 'r' and pass to the lambda that should return result tuple ('r', "ohit"), but why is it that z type is ([Char], char) :: (x,y) (2) How can i print the value of z in the above case on the ghci Many thanks, Rohit -------------- next part -------------- An HTML attachment was scrubbed... URL: From ky3 at atamo.com Sat Nov 15 15:08:49 2014 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Sat, 15 Nov 2014 22:08:49 +0700 Subject: [Haskell-beginners] Bind parser returning wrong return type In-Reply-To: References: Message-ID: Your questions aren't lame. They are common confusions from those who come from a not-so-mathy background. Your first question: Shouldn't the return type be of (Char, [Char])? suggests that you're confusing the type synonym of (Parser a), which is String -> (a, String), with just the right-hand-side, (a, String). Your "z" expression has type Parser ([Char], Char), which means the same thing as String -> ((String, Char), String). How did that happen? Because item `bind` (\x -> (\y -> result (x,y))) "Rohit" is equivalent to item `bind` ( (\x -> (\y -> result (x,y))) "Rohit" ) as those last two expressions go together by the parsing rules. So what you actually have is item `bind` (\y -> result ("Rohit",y)) The first argument to bind has type Parser Char, the second argument (a -> Parser (String,a)). The result is exactly what's expected: Parser (String, Char), i.e. String -> ((String, Char), String). p.s. This might be what you're looking for: Try evaluating item "Rohit" in the repl. -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From mihai.maruseac at gmail.com Mon Nov 17 02:02:38 2014 From: mihai.maruseac at gmail.com (Mihai Maruseac) Date: Sun, 16 Nov 2014 21:02:38 -0500 Subject: [Haskell-beginners] [Haskell] ANNOUNCE: Haskell Communities and Activities Report (27th ed., November 2014) Message-ID: On behalf of all the contributors, we are pleased to announce that the Haskell Communities and Activities Report (27th edition, November 2014) is now available, in PDF and HTML formats: http://haskell.org/communities/11-2014/report.pdf http://haskell.org/communities/11-2014/html/report.html Many thanks go to all the people that contributed to this report, both directly, by sending in descriptions, and indirectly, by doing all the interesting things that are reported. We hope you will find it as interesting a read as we did. If you have not encountered the Haskell Communities and Activities Reports before, you may like to know that the first of these reports was published in November 2001. Their goal is to improve the communication between the increasingly diverse groups, projects, and individuals working on, with, or inspired by Haskell. The idea behind these reports is simple: Every six months, a call goes out to all of you enjoying Haskell to contribute brief summaries of your own area of work. Many of you respond (eagerly, unprompted, and sometimes in time for the actual deadline) to the call. The editors collect all the contributions into a single report and feed that back to the community. When we try for the next update, six months from now, you might want to report on your own work, project, research area or group as well. So, please put the following into your diaries now: ======================================== End of April 2015: target deadline for contributions to the May 2015 edition of the HC&A Report ======================================== Unfortunately, many Haskellers working on interesting projects are so busy with their work that they seem to have lost the time to follow the Haskell related mailing lists and newsgroups, and have trouble even finding time to report on their work. If you are a member, user or friend of a project so burdened, please find someone willing to make time to report and ask them to "register" with the editors for a simple e-mail reminder in October (you could point us to them as well, and we can then politely ask if they want to contribute, but it might work better if you do the initial asking). Of course, they will still have to find the ten to fifteen minutes to draw up their report, but maybe we can increase our coverage of all that is going on in the community. Feel free to circulate this announcement further in order to reach people who might otherwise not see it. Enjoy! Mihai Maruseac and Alejandro Serrano Mena -- Mihai Maruseac (MM) "If you don't know, the thing to do is not to get scared, but to learn." -- Atlas Shrugged. From rohits79 at gmail.com Mon Nov 17 05:27:11 2014 From: rohits79 at gmail.com (Rohit Sharma) Date: Mon, 17 Nov 2014 05:27:11 +0000 Subject: [Haskell-beginners] Bind parser returning wrong return type References: Message-ID: Thanks much Kim, that was very helpful. On Sat, 15 Nov, 2014 11:09 pm Kim-Ee Yeoh wrote: > Your questions aren't lame. They are common confusions from those who come > from a not-so-mathy background. > > Your first question: > > Shouldn't the return type be of (Char, [Char])? > > suggests that you're confusing the type synonym of (Parser a), which is > String -> (a, String), with just the right-hand-side, (a, String). > > Your "z" expression has type Parser ([Char], Char), which means the same > thing as String -> ((String, Char), String). > > How did that happen? > > Because > > > item `bind` (\x -> (\y -> result (x,y))) "Rohit" > > is equivalent to > > item `bind` ( (\x -> (\y -> result (x,y))) "Rohit" ) > > as those last two expressions go together by the parsing rules. > > So what you actually have is > > item `bind` (\y -> result ("Rohit",y)) > > The first argument to bind has type Parser Char, the second argument (a -> > Parser (String,a)). > > The result is exactly what's expected: Parser (String, Char), i.e. String > -> ((String, Char), String). > > p.s. This might be what you're looking for: Try evaluating > > item "Rohit" > > in the repl. > > > -- Kim-Ee > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From simonpj at microsoft.com Mon Nov 17 09:17:27 2014 From: simonpj at microsoft.com (Simon Peyton Jones) Date: Mon, 17 Nov 2014 09:17:27 +0000 Subject: [Haskell-beginners] [Haskell-cafe] [Haskell] ANNOUNCE: Haskell Communities and Activities Report (27th ed., November 2014) In-Reply-To: References: Message-ID: <618BE556AADD624C9C918AA5D5911BEF3F3C37C2@DB3PRD3001MB020.064d.mgd.msft.net> Friends With each issue of the Haskell Communities and Activities Report I am freshly awed by the range and creativity of the things you are all doing with Haskell. From web frameworks to bioinformatics, from automatic differentiation to GUIs and games. Amazing stuff. I think we owe the editors, Mihai Maruseac and Alejandro Serrano Mena, a huge debt for putting it together. Thank you! Simon | -----Original Message----- | From: Haskell-Cafe [mailto:haskell-cafe-bounces at haskell.org] On Behalf | Of Mihai Maruseac | Sent: 17 November 2014 02:03 | To: Haskell; haskell; Haskell Beginners; Lista principala | Subject: [Haskell-cafe] [Haskell] ANNOUNCE: Haskell Communities and | Activities Report (27th ed., November 2014) | | On behalf of all the contributors, we are pleased to announce that the | | Haskell Communities and Activities Report | (27th edition, November 2014) | | is now available, in PDF and HTML formats: | | http://haskell.org/communities/11-2014/report.pdf | http://haskell.org/communities/11-2014/html/report.html From objitsu at gmail.com Mon Nov 17 10:49:53 2014 From: objitsu at gmail.com (emacstheviking) Date: Mon, 17 Nov 2014 10:49:53 +0000 Subject: [Haskell-beginners] [Haskell] ANNOUNCE: Haskell Communities and Activities Report (27th ed., November 2014) In-Reply-To: References: Message-ID: Amazing work well done everybody on that one! On 17 November 2014 02:02, Mihai Maruseac wrote: > On behalf of all the contributors, we are pleased to announce that the > > Haskell Communities and Activities Report > (27th edition, November 2014) > > is now available, in PDF and HTML formats: > > http://haskell.org/communities/11-2014/report.pdf > http://haskell.org/communities/11-2014/html/report.html > > Many thanks go to all the people that contributed to this report, > both directly, by sending in descriptions, and indirectly, by doing > all the interesting things that are reported. We hope you will find > it as interesting a read as we did. > > If you have not encountered the Haskell Communities and Activities > Reports before, you may like to know that the first of these reports > was published in November 2001. Their goal is to improve the > communication between the increasingly diverse groups, projects, and > individuals working on, with, or inspired by Haskell. The idea behind > these reports is simple: > > Every six months, a call goes out to all of you enjoying Haskell to > contribute brief summaries of your own area of work. Many of you > respond (eagerly, unprompted, and sometimes in time for the actual > deadline) to the call. The editors collect all the contributions > into a single report and feed that back to the community. > > When we try for the next update, six months from now, you might want > to report on your own work, project, research area or group as well. > So, please put the following into your diaries now: > > ======================================== > End of April 2015: > target deadline for contributions to the > May 2015 edition of the HC&A Report > ======================================== > > Unfortunately, many Haskellers working on interesting projects are so > busy with their work that they seem to have lost the time to follow > the Haskell related mailing lists and newsgroups, and have trouble even > finding time to report on their work. If you are a member, user or > friend of a project so burdened, please find someone willing to make > time to report and ask them to "register" with the editors for a simple > e-mail reminder in October (you could point us to them as well, and we > can then politely ask if they want to contribute, but it might work > better if you do the initial asking). Of course, they will still have to > find the ten to fifteen minutes to draw up their report, but maybe we > can increase our coverage of all that is going on in the community. > > Feel free to circulate this announcement further in order to > reach people who might otherwise not see it. Enjoy! > > Mihai Maruseac and Alejandro Serrano Mena > > > -- > Mihai Maruseac (MM) > "If you don't know, the thing to do is not to get scared, but to > learn." -- Atlas Shrugged. > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rohits79 at gmail.com Mon Nov 17 15:20:09 2014 From: rohits79 at gmail.com (Rohit Sharma) Date: Mon, 17 Nov 2014 23:20:09 +0800 Subject: [Haskell-beginners] Unable to sequence using 'do' operator Message-ID: type Parser a = String -> [(a, String)] Hi, I am learning haskell and wondering why my definition of firstAndThird does not work with the do operator, however when i try to use the same using bind (firstandThird2) it works as expected. I basically want to return a tuple of first and third character of a string. Can someone please correct me what i might be overseeing? I have pasted the same code on codetidy in case if the email lose formatting. http://codetidy.com/5726/ Many Thanks, Rohit zero :: Parser a zero = \inp -> [] result :: a -> Parser a result x = \inp -> [(x, inp)] item :: Parser Char item = \inp -> case inp of [] -> [] (x:xs) -> [(x,xs)] bind :: Parser a -> (a -> Parser b) -> Parser b p `bind` f = \inp -> concat [ ((f x) inp') | (x, inp') <- p inp] sat :: (Char -> Bool) -> Parser Char sat predicate = item `bind` (\x -> if predicate x then result x else zero ) lower :: Parser Char lower = sat (\x -> 'a' <= x && x <= 'z') firstAndThird :: Parser (Char, Char) firstAndThird = do x <- item item y <- item return (x,y) firstAndThird2 :: Parser (Char, Char) firstAndThird2 = item `bind` \x -> item `bind` \y -> item `bind` \z -> result (x,z) -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Mon Nov 17 15:30:33 2014 From: toad3k at gmail.com (David McBride) Date: Mon, 17 Nov 2014 10:30:33 -0500 Subject: [Haskell-beginners] Unable to sequence using 'do' operator In-Reply-To: References: Message-ID: Your indentation is not correct. Remember that haskell is whitespace sensitive. firstAndThird :: Parser (Char, Char) firstAndThird = do x <- item item y <- item return (x,y) should work. On Mon, Nov 17, 2014 at 10:20 AM, Rohit Sharma wrote: > type Parser a = String -> [(a, String)] > > Hi, > > I am learning haskell and wondering why my definition of firstAndThird > does not work with the do operator, however when i try to use the same > using bind (firstandThird2) it works as expected. I basically want to > return a tuple of first and third character of a string. Can someone please > correct me what i might be overseeing? > > I have pasted the same code on codetidy in case if the email lose > formatting. > http://codetidy.com/5726/ > > Many Thanks, > Rohit > > zero :: Parser a > zero = \inp -> [] > > result :: a -> Parser a > result x = \inp -> [(x, inp)] > > item :: Parser Char > item = \inp -> case inp of > [] -> [] > (x:xs) -> [(x,xs)] > > bind :: Parser a -> (a -> Parser b) -> Parser b > p `bind` f = \inp -> concat [ ((f x) inp') | (x, inp') <- p inp] > > sat :: (Char -> Bool) -> Parser Char > sat predicate = item `bind` (\x -> if predicate x then result x else zero ) > > lower :: Parser Char > lower = sat (\x -> 'a' <= x && x <= 'z') > > firstAndThird :: Parser (Char, Char) > firstAndThird = do x <- item > item > y <- item > return (x,y) > > firstAndThird2 :: Parser (Char, Char) > firstAndThird2 = item `bind` \x -> > item `bind` \y -> > item `bind` \z -> > result (x,z) > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From stephen.tetley at gmail.com Mon Nov 17 18:22:34 2014 From: stephen.tetley at gmail.com (Stephen Tetley) Date: Mon, 17 Nov 2014 18:22:34 +0000 Subject: [Haskell-beginners] Unable to sequence using 'do' operator In-Reply-To: References: Message-ID: Hi Rohit It looks like you might be using the parser combinators for Graham Hutton's book. Note that the code in the book does not work with standard Haskell as Graham doesn't want to clutter his presentation with "newtypes". There is an explanation at the end of the chapter and alternative code on the website accompanying the book that is written in standard Haskell. Best wishes Stephen On 17 November 2014 15:20, Rohit Sharma wrote: > type Parser a = String -> [(a, String)] > > Hi, > > I am learning haskell and wondering why my definition of firstAndThird does > not work with the do operator, however when i try to use the same using bind > (firstandThird2) it works as expected. I basically want to return a tuple of > first and third character of a string. Can someone please correct me what i > might be overseeing? > > I have pasted the same code on codetidy in case if the email lose > formatting. > http://codetidy.com/5726/ > > Many Thanks, > Rohit > > zero :: Parser a > zero = \inp -> [] > > result :: a -> Parser a > result x = \inp -> [(x, inp)] > > item :: Parser Char > item = \inp -> case inp of > [] -> [] > (x:xs) -> [(x,xs)] > > bind :: Parser a -> (a -> Parser b) -> Parser b > p `bind` f = \inp -> concat [ ((f x) inp') | (x, inp') <- p inp] > > sat :: (Char -> Bool) -> Parser Char > sat predicate = item `bind` (\x -> if predicate x then result x else zero ) > > lower :: Parser Char > lower = sat (\x -> 'a' <= x && x <= 'z') > > firstAndThird :: Parser (Char, Char) > firstAndThird = do x <- item > item > y <- item > return (x,y) > > firstAndThird2 :: Parser (Char, Char) > firstAndThird2 = item `bind` \x -> > item `bind` \y -> > item `bind` \z -> > result (x,z) > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From spam at scientician.net Mon Nov 17 19:01:32 2014 From: spam at scientician.net (Bardur Arantsson) Date: Mon, 17 Nov 2014 20:01:32 +0100 Subject: [Haskell-beginners] [Haskell-cafe] [Haskell] ANNOUNCE: Haskell Communities and Activities Report (27th ed., November 2014) In-Reply-To: <618BE556AADD624C9C918AA5D5911BEF3F3C37C2@DB3PRD3001MB020.064d.mgd.msft.net> References: <618BE556AADD624C9C918AA5D5911BEF3F3C37C2@DB3PRD3001MB020.064d.mgd.msft.net> Message-ID: On 2014-11-17 10:17, Simon Peyton Jones wrote: > Friends > > With each issue of the Haskell Communities and Activities Report I am freshly awed by the range and creativity of the things you are all doing with Haskell. From web frameworks to bioinformatics, from automatic differentiation to GUIs and games. Amazing stuff. > > I think we owe the editors, Mihai Maruseac and Alejandro Serrano Mena, a huge debt for putting it together. Thank you! > Hear, hear! From andrew.bernard at gmail.com Thu Nov 20 10:30:27 2014 From: andrew.bernard at gmail.com (Andrew Bernard) Date: Thu, 20 Nov 2014 21:30:27 +1100 Subject: [Haskell-beginners] IntelliJ IDEA Haskell Message-ID: <6E0341DD-B111-47BF-8C01-F624DE7DF510@gmail.com> Greetings List, I?d like to try IntelliJ IDEA for Haskell. There seem to be many plugins in various stages of maturity. Which one is recommended? Is this a good environment for Haskell development? Andrew From sean at objitsu.com Thu Nov 20 10:36:34 2014 From: sean at objitsu.com (emacstheviking) Date: Thu, 20 Nov 2014 10:36:34 +0000 Subject: [Haskell-beginners] IntelliJ IDEA Haskell In-Reply-To: <6E0341DD-B111-47BF-8C01-F624DE7DF510@gmail.com> References: <6E0341DD-B111-47BF-8C01-F624DE7DF510@gmail.com> Message-ID: Leksah is good and seems to do what IntelliJ *might* one day do for Haskell. Last time I tried Leksah it was a CPU hogger though but that was about a year ago so it may well have become even better since then. On 20 November 2014 10:30, Andrew Bernard wrote: > Greetings List, > > I?d like to try IntelliJ IDEA for Haskell. There seem to be many plugins > in various stages of maturity. Which one is recommended? > > Is this a good environment for Haskell development? > > Andrew > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From Christian.Maeder at dfki.de Thu Nov 20 11:07:56 2014 From: Christian.Maeder at dfki.de (Christian Maeder) Date: Thu, 20 Nov 2014 12:07:56 +0100 Subject: [Haskell-beginners] IntelliJ IDEA Haskell In-Reply-To: <6E0341DD-B111-47BF-8C01-F624DE7DF510@gmail.com> References: <6E0341DD-B111-47BF-8C01-F624DE7DF510@gmail.com> Message-ID: <546DCB8C.6010907@dfki.de> Hi, I've used IntelliJ IDEA for Java and Scala, which is good. But I soon gave up my admittedly weak attempt to use IntelliJ IDEA for Haskell. However I do remember what stopped me using it about a year ago (syntax support or configuration problems). Maybe I'm just too much used to Emacs. Cheers Christian Am 20.11.2014 um 11:30 schrieb Andrew Bernard: > Greetings List, > > I?d like to try IntelliJ IDEA for Haskell. There seem to be many plugins in various stages of maturity. Which one is recommended? > > Is this a good environment for Haskell development? > > Andrew > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From Christian.Maeder at dfki.de Thu Nov 20 11:10:24 2014 From: Christian.Maeder at dfki.de (Christian Maeder) Date: Thu, 20 Nov 2014 12:10:24 +0100 Subject: [Haskell-beginners] IntelliJ IDEA Haskell In-Reply-To: <546DCB8C.6010907@dfki.de> References: <6E0341DD-B111-47BF-8C01-F624DE7DF510@gmail.com> <546DCB8C.6010907@dfki.de> Message-ID: <546DCC20.6020807@dfki.de> Am 20.11.2014 um 12:07 schrieb Christian Maeder: > Hi, > > I've used IntelliJ IDEA for Java and Scala, which is good. > > But I soon gave up my admittedly weak attempt to use > IntelliJ IDEA for Haskell. > > However I do remember what stopped me using it about a year ago (syntax Sorry: I do _not_ remember! > support or configuration problems). Maybe I'm just too much used to Emacs. > > Cheers Christian > > > Am 20.11.2014 um 11:30 schrieb Andrew Bernard: >> Greetings List, >> >> I?d like to try IntelliJ IDEA for Haskell. There seem to be many >> plugins in various stages of maturity. Which one is recommended? >> >> Is this a good environment for Haskell development? >> >> Andrew >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From miroslav.karpis at gmail.com Fri Nov 21 23:13:38 2014 From: miroslav.karpis at gmail.com (Miro Karpis) Date: Sat, 22 Nov 2014 00:13:38 +0100 Subject: [Haskell-beginners] building ghc for cross-platform raspberry pi (ubuntu) Message-ID: Hi, I'm trying to build ghc for raspberry pi, but I'm having problem with .configure. I'm following these instructions from ( https://ghc.haskell.org/trac/ghc/wiki/Building/Preparation/RaspberryPi). I have the tools installed and they should be also in my PATH,..... Please any ideas? ghc> ./configure --target=arm-linux-gnueabihf --enable-unregisterised checking for gfind... no checking for find... /usr/bin/find checking for sort... /usr/bin/sort checking for GHC version date... inferred 7.9.20141121 checking for ghc... /usr/bin/ghc checking version of ghc... 7.6.3 checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking target system type... arm-unknown-linux-gnueabihf Build platform inferred as: x86_64-unknown-linux Host platform inferred as: x86_64-unknown-linux GHC build : x86_64-unknown-linux GHC host : x86_64-unknown-linux GHC target : arm-unknown-linux configure: Building in-tree ghc-pwd checking for path to top of build tree... /home/parallels/code/ghc checking for gcc... gcc checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether gcc accepts -g... yes checking for gcc option to accept ISO C89... none needed checking how to run the C preprocessor... gcc -E checking for grep that handles long lines and -e... /bin/grep checking for egrep... /bin/grep -E checking for ANSI C header files... yes checking for sys/types.h... yes checking for sys/stat.h... yes checking for stdlib.h... yes checking for string.h... yes checking for memory.h... yes checking for strings.h... yes checking for inttypes.h... yes checking for stdint.h... yes checking for unistd.h... yes checking for arm-linux-gnueabihf-gcc... /home/parallels/code/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-gcc checking for arm-linux-gnueabihf-ld... /home/parallels/code/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-ld checking for arm-linux-gnueabihf-nm... /home/parallels/code/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-nm checking for arm-linux-gnueabihf-ar... /home/parallels/code/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-ar checking for arm-linux-gnueabihf-ranlib... /home/parallels/code/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-ranlib checking for llc... no checking for opt... no checking whether #! works in shell scripts... yes checking for perl... /usr/bin/perl checking version of gcc... configure: error: Need at least gcc version 3.0 (3.4+ recommended) Regards, Miro -------------- next part -------------- An HTML attachment was scrubbed... URL: From hlmerscher at gmail.com Tue Nov 25 14:23:20 2014 From: hlmerscher at gmail.com (Hercules Merscher) Date: Tue, 25 Nov 2014 12:23:20 -0200 Subject: [Haskell-beginners] building ghc for cross-platform raspberry pi (ubuntu) In-Reply-To: References: Message-ID: Looks like that your gcc version is lesser than 3.0. On Fri, Nov 21, 2014 at 9:13 PM, Miro Karpis wrote: > Hi, I'm trying to build ghc for raspberry pi, but I'm having problem with > .configure. I'm following these instructions from ( > https://ghc.haskell.org/trac/ghc/wiki/Building/Preparation/RaspberryPi). > > I have the tools installed and they should be also in my PATH,..... > > Please any ideas? > > > > ghc> ./configure --target=arm-linux-gnueabihf --enable-unregisterised > checking for gfind... no > checking for find... /usr/bin/find > checking for sort... /usr/bin/sort > checking for GHC version date... inferred 7.9.20141121 > checking for ghc... /usr/bin/ghc > checking version of ghc... 7.6.3 > checking build system type... x86_64-unknown-linux-gnu > checking host system type... x86_64-unknown-linux-gnu > checking target system type... arm-unknown-linux-gnueabihf > Build platform inferred as: x86_64-unknown-linux > Host platform inferred as: x86_64-unknown-linux > GHC build : x86_64-unknown-linux > GHC host : x86_64-unknown-linux > GHC target : arm-unknown-linux > configure: Building in-tree ghc-pwd > checking for path to top of build tree... /home/parallels/code/ghc > checking for gcc... gcc > checking whether the C compiler works... yes > checking for C compiler default output file name... a.out > checking for suffix of executables... > checking whether we are cross compiling... no > checking for suffix of object files... o > checking whether we are using the GNU C compiler... yes > checking whether gcc accepts -g... yes > checking for gcc option to accept ISO C89... none needed > checking how to run the C preprocessor... gcc -E > checking for grep that handles long lines and -e... /bin/grep > checking for egrep... /bin/grep -E > checking for ANSI C header files... yes > checking for sys/types.h... yes > checking for sys/stat.h... yes > checking for stdlib.h... yes > checking for string.h... yes > checking for memory.h... yes > checking for strings.h... yes > checking for inttypes.h... yes > checking for stdint.h... yes > checking for unistd.h... yes > checking for arm-linux-gnueabihf-gcc... > /home/parallels/code/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-gcc > checking for arm-linux-gnueabihf-ld... > /home/parallels/code/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-ld > checking for arm-linux-gnueabihf-nm... > /home/parallels/code/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-nm > checking for arm-linux-gnueabihf-ar... > /home/parallels/code/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-ar > checking for arm-linux-gnueabihf-ranlib... > /home/parallels/code/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-ranlib > checking for llc... no > checking for opt... no > checking whether #! works in shell scripts... yes > checking for perl... /usr/bin/perl > checking version of gcc... configure: error: Need at least gcc version 3.0 > (3.4+ recommended) > > > Regards, > Miro > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Hercules Lemke Merscher http://www.herculesdev.com.br -------------- next part -------------- An HTML attachment was scrubbed... URL: