From borgauf at gmail.com Fri Jan 1 04:33:36 2021 From: borgauf at gmail.com (Lawrence Bottorff) Date: Thu, 31 Dec 2020 22:33:36 -0600 Subject: [Haskell-beginners] Lambda calc version of Haskell lambda function Message-ID: Here is a function declaration makeAddress :: Int -> String -> String -> (Int, String, String) makeAddress number street town = (number,street,town) and here is a lambda function version makeAddressLambda = (\number -> (\street -> (\town -> (number, street, town)))) How would this lambda version look in lambda calculus? Like this? \number.\street.\town.(number street town) then (\number.\street.\town.(number street town) (123 "Sunny St." "Fergus") (\street.\town.(123 street town) ("Sunny St." "Fergus") (\town.(123 "Sunny St.") ("Fergus") (123 "Sunny St." "Fergus") Not always sure. LB -------------- next part -------------- An HTML attachment was scrubbed... URL: From borgauf at gmail.com Fri Jan 1 05:04:19 2021 From: borgauf at gmail.com (Lawrence Bottorff) Date: Thu, 31 Dec 2020 23:04:19 -0600 Subject: [Haskell-beginners] Type variables Message-ID: I put this into ghci makeTriple :: a -> b -> c -> (a,b,c) makeTriple x y z = (x,y,z) then as expected > :t makeTriple : makeTriple :: a -> b -> c -> (a, b, c) but then on a whim I try this > :t (makeTriple 123 "Hi" 234.0) (makeTriple 123 "Hi" 234.0) :: (Fractional c, Num a) => (a, [Char], c) I retry > :t (123,"Hi",234.0) (123,"Hi",234.0) :: (Fractional c, Num a) => (a, [Char], c) What is this telling me? I'm not experienced enough to decode this. LB -------------- next part -------------- An HTML attachment was scrubbed... URL: From utprimum at gmail.com Fri Jan 1 09:25:03 2021 From: utprimum at gmail.com (Ut Primum) Date: Fri, 1 Jan 2021 10:25:03 +0100 Subject: [Haskell-beginners] Type variables In-Reply-To: References: Message-ID: Hi, the first time you asked for the type of the function MakeTriple, which as you expected takes 3 arguments of generic types a, b and c and returns something of type (a,b,c). The second time you asked for the type of MakeTriple applied to three specific arguments. This time the result is no more the type of a function, but is just the type of the result, which is exactly the same as the type of (123,"Hi",234.0) Maybe now your question is why that expression has the type you obtain. First of all, what is at the left of the symbol => are class constraints. This means that they tell you that now c is not "any type" (as it was in the type of MakeTriple), but is "any type in the class Fractional". So it could be for example of type Float or Double, which are two different types but both in the class Fractional. Similarly Num a means that a can be "any type in the class Num" (so it can be Int, Integer, Float etc.). The same class constraint you obtain if you ask for the type of 12. Then at the right of the => symbol, you have the type of your expression, where a and c are not completely generic types, because of the contraints expressed before. happy new year, Ut Il ven 1 gen 2021, 06:15 Lawrence Bottorff ha scritto: > I put this into ghci > > makeTriple :: a -> b -> c -> (a,b,c) > makeTriple x y z = (x,y,z) > > then as expected > > > :t makeTriple > : makeTriple :: a -> b -> c -> (a, b, c) > > but then on a whim I try this > > > :t (makeTriple 123 "Hi" 234.0) > (makeTriple 123 "Hi" 234.0) > :: (Fractional c, Num a) => (a, [Char], c) > > I retry > > > :t (123,"Hi",234.0) > (123,"Hi",234.0) :: (Fractional c, Num a) => (a, [Char], c) > > What is this telling me? I'm not experienced enough to decode this. > > LB > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From fa-ml at ariis.it Fri Jan 1 09:58:16 2021 From: fa-ml at ariis.it (Francesco Ariis) Date: Fri, 1 Jan 2021 10:58:16 +0100 Subject: [Haskell-beginners] Lambda calc version of Haskell lambda function In-Reply-To: References: Message-ID: <20210101095816.GA30146@extensa> Il 31 dicembre 2020 alle 22:33 Lawrence Bottorff ha scritto: > Here is a function declaration > > makeAddress :: Int -> String -> String -> (Int, String, String) > makeAddress number street town = (number,street,town) > > and here is a lambda function version > > makeAddressLambda = (\number -> (\street -> (\town -> (number, street, > town)))) You can lose most of the parentheses: makeAddressLambda = \number -> \street -> \town -> (number, street, town) > How would this lambda version look in lambda calculus? Like this? > > \number.\street.\town.(number street town) Yes. > then > > (\number.\street.\town.(number street town) (123 "Sunny St." "Fergus") Wait! You have lost a ‘)’ on the lambdas (remember λ goes as far right as possible). Also (123, "Sunny St.", "Fergus") makes it look like it is a single argument, which leads to a wrong result: (λnumber. λstreet. λtown. number street town) (123, "Sunny St.", "Fergus") λstreet. λtown. (123, "Sunny St.", "Fergus") street town -- woops! Instead, keeping in mind Haskell follows a beta-nu-mu strategy, we have a series of 3 βs: (λnumber. λstreet. λtown. (number, street, town)) 123 "Sunny St." "Fergus" (λstreet. λtown. (123, street, town) "Sunny St." "Fergus" (λtown. (123, "Sunny St.", town) "Fergus" (123, "Sunny St.", "Fergus") From fa-ml at ariis.it Fri Jan 1 10:20:02 2021 From: fa-ml at ariis.it (Francesco Ariis) Date: Fri, 1 Jan 2021 11:20:02 +0100 Subject: [Haskell-beginners] Type variables In-Reply-To: References: Message-ID: <20210101102002.GA20222@extensa> Il 31 dicembre 2020 alle 23:04 Lawrence Bottorff ha scritto: > > :t (123,"Hi",234.0) > (123,"Hi",234.0) :: (Fractional c, Num a) => (a, [Char], c) When you do not write a signature yourself GHC/GHCi tries to infer it. Alas — for certain types — GHC tries to default it -- prova.hs a = (123, "Hi", 234.0) λ> :load prova.hs [1 of 1] Compiling Main ( prova.hs, interpreted ) Ok, one module loaded. λ> :t a a :: (Integer, [Char], Double) while GHCi goes for a more general type λ> b = (123, "Hi", 234.0) λ> :t b (123,"Hi",234.0) :: (Fractional c, Num a) => (a, [Char], c) A couple of considerations: - those are typeclasses — tl;dr they are interfaces providing a specific type of polymorphism. Ignore them for now if your text hasn’t explained them yet. - When GHCi gives a type signature with typeclasses but you would rather see concrete types, use `+d` λ> :t +d b b :: (Integer, [Char], Double) From borgauf at gmail.com Sat Jan 2 17:51:57 2021 From: borgauf at gmail.com (Lawrence Bottorff) Date: Sat, 2 Jan 2021 11:51:57 -0600 Subject: [Haskell-beginners] Type explanation of foldl? Message-ID: I've got this myFoldl f init [] = init myFoldl f init (x:xs) = myFoldl f newInit xs where newInit = f init x which when evaluated gives this for its type > :t myFoldl myFoldl :: (t -> a -> t) -> t -> [a] -> t . . . not totally sure what that all means, e.g., how the t as a generic class is behaving is very mysterious. Obviously, the final result is of type t . . . and that must relate to the incoming argument init, correct? (BTW, does a type declaration like this one reflect in any way the recursion going on?) But then with Prelude foldl I'm really clueless > :t foldl foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b I'm guessing the t is again a generic type of the Foldable class, but then. . . Can someone walk me through this? LB -------------- next part -------------- An HTML attachment was scrubbed... URL: From dj112358 at outlook.com Sun Jan 3 10:31:44 2021 From: dj112358 at outlook.com (David James) Date: Sun, 3 Jan 2021 10:31:44 +0000 Subject: [Haskell-beginners] Type explanation of foldl? In-Reply-To: References: Message-ID: Hello – in myFoldl :: (t -> a -> t) -> t -> [a] -> t the t and the a are type variables. There are no “classes” at all. Since you didn’t name the type variables, Haskell has provided default names for you “at random”, and it’s in some ways a little unfortunate that it’s named one of them t. You could (probably should) provide your own declaration, e.g. renaming t to b: myFoldl :: (b -> a -> b) -> b -> [a] -> b This represents exactly the same thing, and says that myFoldl will work for any two types a and b. It takes three params: (b -> a -> b) A function of values of type b and type a that returns a value of type b. (The accumulation function) b A value of type b (The initial accumulated value) [a] A list of values of type a (This list to accumulate over) and returns b A value of type b (The final accumulated value) The prelude function: foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b is then almost identical. This time, though, instead of being restricted to a list of values (of type a) to fold over, it can fold over any type of foldable collection of values (of type a). t represents the type of the foldable collection, and Foldable t => is a constraint that says the function will only work for a type t that is an instance of the class Foldable. [BTW: does a type declaration like this one reflect in any way the recursion going on? I didn’t really understand the question, but you can often deduce what a function does, and how it might be implemented, from its type signature.] I don’t know how you’re going about learning Haskell, but following a tutorial (such as http://learnyouahaskell.com/) might be helpful, and I think explains all of the above step by step. Regards, David. From: Lawrence Bottorff Sent: 02 January 2021 17:52 To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject: [Haskell-beginners] Type explanation of foldl? I've got this myFoldl f init [] = init myFoldl f init (x:xs) = myFoldl f newInit xs where newInit = f init x which when evaluated gives this for its type > :t myFoldl myFoldl :: (t -> a -> t) -> t -> [a] -> t . . . not totally sure what that all means, e.g., how the t as a generic class is behaving is very mysterious. Obviously, the final result is of type t . . . and that must relate to the incoming argument init, correct? (BTW, does a type declaration like this one reflect in any way the recursion going on?) But then with Prelude foldl I'm really clueless > :t foldl foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b I'm guessing the t is again a generic type of the Foldable class, but then. . . Can someone walk me through this? LB -------------- next part -------------- An HTML attachment was scrubbed... URL: From borgauf at gmail.com Sun Jan 3 22:22:12 2021 From: borgauf at gmail.com (Lawrence Bottorff) Date: Sun, 3 Jan 2021 16:22:12 -0600 Subject: [Haskell-beginners] No ghci return for homemade types Message-ID: I've tried this data MyMaybe a = Just a | Nothing maybeHead :: [t] -> MyMaybe t maybeHead [] = Nothing maybeHead (x:_) = Just x and it evaluates fine. But this maybeHead [1,2,3] produces error: : • No instance for (Show (MyMaybe Integer)) : arising from a use of ‘show’ : • In the expression: show (maybeHead [1, 2, 3]) : In an equation for ‘it’: it = show (maybeHead [1, 2, 3]) Similarly, data Color a = Blue a | Green a | Red a myFavoriteColor :: Color Int myFavoriteColor = Green 50 but now I'm stuck. How can I access the "Green 50" inside myFavoriteColor? LB -------------- next part -------------- An HTML attachment was scrubbed... URL: From fa-ml at ariis.it Sun Jan 3 22:29:26 2021 From: fa-ml at ariis.it (Francesco Ariis) Date: Sun, 3 Jan 2021 23:29:26 +0100 Subject: [Haskell-beginners] No ghci return for homemade types In-Reply-To: References: Message-ID: <20210103222926.GA9403@extensa> Il 03 gennaio 2021 alle 16:22 Lawrence Bottorff ha scritto: > I've tried this > > data MyMaybe a = Just a | Nothing > > maybeHead :: [t] -> MyMaybe t > maybeHead [] = Nothing > maybeHead (x:_) = Just x > > and it evaluates fine. But this > > maybeHead [1,2,3] > > produces error: > : • No instance for (Show (MyMaybe Integer)) > : arising from a use of ‘show’ > : • In the expression: show (maybeHead [1, 2, 3]) > : In an equation for ‘it’: it = show (maybeHead [1, 2, 3]) data MyMaybe a = Jus a | Not deriving (Show, Eq) Your text should have introduced to/warned you about deriving and typeclasses. From velichko.lefterov at gmail.com Tue Jan 5 18:22:50 2021 From: velichko.lefterov at gmail.com (Velichko Lefterov) Date: Tue, 5 Jan 2021 20:22:50 +0200 Subject: [Haskell-beginners] newEmptyMVar return type Message-ID: Hello, could someone explain me simple how return-type polymorphism works in the following case: newEmptyMVar :: IO (MVar a) ? -------------- next part -------------- An HTML attachment was scrubbed... URL: From velichko.lefterov at gmail.com Wed Jan 6 06:37:03 2021 From: velichko.lefterov at gmail.com (Velichko Lefterov) Date: Wed, 6 Jan 2021 08:37:03 +0200 Subject: [Haskell-beginners] newEmptyMVar return type In-Reply-To: References: Message-ID: OK, I tried this one: test:: IO (Maybe a) test = return Nothing It looks similar and it seems to work... On Tue, Jan 5, 2021 at 8:22 PM Velichko Lefterov < velichko.lefterov at gmail.com> wrote: > Hello, > could someone explain me simple how return-type polymorphism works > in the following case: > newEmptyMVar :: IO (MVar a) ? > -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Wed Jan 6 11:37:52 2021 From: toad3k at gmail.com (David McBride) Date: Wed, 6 Jan 2021 06:37:52 -0500 Subject: [Haskell-beginners] newEmptyMVar return type In-Reply-To: References: Message-ID: It means that the function can return an mvar containing any type that you need. What type it returns depends on what type your program asks for. On Wed, Jan 6, 2021, 01:37 Velichko Lefterov wrote: > OK, > I tried this one: > > test:: IO (Maybe a) > test = return Nothing > > It looks similar and it seems to work... > > On Tue, Jan 5, 2021 at 8:22 PM Velichko Lefterov < > velichko.lefterov at gmail.com> wrote: > >> Hello, >> could someone explain me simple how return-type polymorphism works >> in the following case: >> newEmptyMVar :: IO (MVar a) ? >> > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From grdvnl at gmail.com Sat Jan 9 17:03:11 2021 From: grdvnl at gmail.com (Guru Devanla) Date: Sat, 9 Jan 2021 09:03:11 -0800 Subject: [Haskell-beginners] Haskell equivalent of Python's prompt-toolkit Message-ID: Hello Cafe, Can someone point me to any packages that are an equivalent to the prompt-toolkit (https://github.com/prompt-toolkit/python-prompt-toolkit). I would like to use functionality in this library, to create a powerful terminal application which will support syntax highlighting and auto completion. Perhaps, I can use a mix of haskell packages for this. For now, the only relevant package I found was http://hackage.haskell.org/package/readline-1.0.3.0/docs/System-Console-Readline.html Any pointers into this would be greatly appreciated. Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From borgauf at gmail.com Wed Jan 13 21:09:35 2021 From: borgauf at gmail.com (Lawrence Bottorff) Date: Wed, 13 Jan 2021 15:09:35 -0600 Subject: [Haskell-beginners] Explanation of composite with foldl Message-ID: I see this on the Haskell 99 questions which will return the second-from-last element of a list lastbut1 :: Foldable f => f a -> a lastbut1 = fst . foldl (\(a,b) x -> (b,x)) (err1,err2) where err1 = error "lastbut1: Empty list" err2 = error "lastbut1: Singleton" I understand how the code works, but not the significance of the type declaration. That looks like a type class. It works without it, I believe. Why have we used Foldable type class? Likewise with this lastbut1safe :: Foldable f => f a -> Maybe a lastbut1safe = fst . foldl (\(a,b) x -> (b,Just x)) (Nothing,Nothing) What's happening with the type definition? LB -------------- next part -------------- An HTML attachment was scrubbed... URL: From fa-ml at ariis.it Wed Jan 13 21:33:51 2021 From: fa-ml at ariis.it (Francesco Ariis) Date: Wed, 13 Jan 2021 22:33:51 +0100 Subject: [Haskell-beginners] Explanation of composite with foldl In-Reply-To: References: Message-ID: <20210113213351.GA30794@extensa> Il 13 gennaio 2021 alle 15:09 Lawrence Bottorff ha scritto: > I see this on the Haskell 99 questions > which will return the > second-from-last element of a list > > lastbut1 :: Foldable f => f a -> a > lastbut1 = fst . foldl (\(a,b) x -> (b,x)) (err1,err2) > where > err1 = error "lastbut1: Empty list" > err2 = error "lastbut1: Singleton" > > I understand how the code works, but not the significance of the type > declaration. That looks like a type class. It works without it, I believe. > Why have we used Foldable type class? A `Foldable` constraint will make the function work on a plethora of types [1] apart from lists (e.g. Arrays, etc.). The assignment is clearly monomorphic («Find the last but one element of a list.»), I would have preferred a `:: [a] -> a` signature. [1] https://hackage.haskell.org/package/base-4.14.1.0/docs/Data-Foldable.html#t:Foldable From borgauf at gmail.com Thu Jan 14 06:24:26 2021 From: borgauf at gmail.com (Lawrence Bottorff) Date: Thu, 14 Jan 2021 00:24:26 -0600 Subject: [Haskell-beginners] foldr with ($) as the passed function Message-ID: With 99 questions Problem 3 wants a function elementAt that will take a list and an index and return the element for that index. One very odd version in the solutions is elementAt xs n = head $ foldr ($) xs $ replicate (n - 1) tail So the function "passed" is ($) and the accumulator "seed" is the incoming list xs and the list to be worked on is (replicate (n-1) tail) which . . . and I can't fathom what's happening -- other than perhaps (replicate (n-1) (tail xs)) elementAt [1,2] 2 would be foldr ($) [1,2] (replicate 1 (tail [1,2]) foldr ($) [1,2] ([2]) . . . now I'm lost. Can someone walk me through this? LB -------------- next part -------------- An HTML attachment was scrubbed... URL: From utprimum at gmail.com Thu Jan 14 07:16:04 2021 From: utprimum at gmail.com (Ut Primum) Date: Thu, 14 Jan 2021 08:16:04 +0100 Subject: [Haskell-beginners] foldr with ($) as the passed function In-Reply-To: References: Message-ID: Hi, I think your interpretation of (replicate (n-1) tail) is wrong. First note that tail is not applied to anything, is just the function tail. So replicate (n-1) tail is [tail, tail, tail, .... , tail]. For example replicate 3 tail = [tail, tail, tail] Note that if you try to compute this in Haskell it won't be able to show that result, because there is no "show" defined for something that has the type of a list of functions. Having said this, your example can now be written elementAt [1,2] 2 = = head (foldr ($) [1,2] (replicate 1 tail)) = head (foldr ($) [1,2] [tail]) = head (tail $ (foldr ($) [1,2] [])) = head (tail $ [1,2]) = head (tail ([1,2])) = head [2] = 2 For a more general example you can try elementAt [1,2,3,4] 3 = = head (foldr ($) [1,2,3,4] (replicate 2 tail)) = head (foldr ($) [1,2,3,4] [tail,tail]) = head (tail $ (foldr ($) [1,2,3,4] [tail])) = head (tail $ tail $ [1,2,3,4]) = head (tail (tail [1,2,3,4))) = head (tail [2,3,4]) = head [3,4] = 3 Cheers, Ut Mail priva di virus. www.avg.com <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> Il giorno gio 14 gen 2021 alle ore 07:25 Lawrence Bottorff < borgauf at gmail.com> ha scritto: > With 99 questions Problem > 3 wants a function elementAt that will take a list and an index and return > the element for that index. One very odd version in the solutions is > > elementAt xs n = head $ foldr ($) xs $ replicate (n - 1) tail > > So the function "passed" is ($) and the accumulator "seed" is the incoming > list xs and the list to be worked on is (replicate (n-1) tail) which . . . > and I can't fathom what's happening -- other than perhaps (replicate (n-1) > (tail xs)) > > elementAt [1,2] 2 would be > > foldr ($) [1,2] (replicate 1 (tail [1,2]) > foldr ($) [1,2] ([2]) > > . . . now I'm lost. Can someone walk me through this? > > LB > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From borgauf at gmail.com Sat Jan 16 22:10:47 2021 From: borgauf at gmail.com (Lawrence Bottorff) Date: Sat, 16 Jan 2021 16:10:47 -0600 Subject: [Haskell-beginners] Dependent and independent variables in foldl and foldr Message-ID: I have this myLength1 = foldl (\n _ -> n + 1) 0 and this myLength2 = foldr (\_ n -> n + 1) 0 I am guessing that foldl knows to assign the accumulator-seed argument to the dependent variable and the list argument's elements recursively to the independent variable; and with foldr to do the opposite. Is this a fair assumption? BTW, where can I get a look at the code for fold functions; or does the type definition answer my original question? Not really able to decipher it so well :t foldl foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b LB -------------- next part -------------- An HTML attachment was scrubbed... URL: From fa-ml at ariis.it Sat Jan 16 22:35:55 2021 From: fa-ml at ariis.it (Francesco Ariis) Date: Sat, 16 Jan 2021 23:35:55 +0100 Subject: [Haskell-beginners] Dependent and independent variables in foldl and foldr In-Reply-To: References: Message-ID: <20210116223555.GA25810@extensa> Il 16 gennaio 2021 alle 16:10 Lawrence Bottorff ha scritto: > I have this > > myLength1 = foldl (\n _ -> n + 1) 0 > > and this > > myLength2 = foldr (\_ n -> n + 1) 0 > > I am guessing that foldl knows to assign the accumulator-seed argument to > the dependent variable and the list argument's elements recursively to the > independent variable; and with foldr to do the opposite. Is this a fair > assumption? BTW, where can I get a look at the code for fold functions; or > does the type definition answer my original question? Not really able to > decipher it so well > > :t foldl > foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b foldl and foldr have slightly different signatures, λ> :t +d foldl foldl :: (b -> a -> b) -> b -> [a] -> b λ> :t +d foldr foldr :: (a -> b -> b) -> b -> [a] -> b (Notice `b -> a -> b` vs. `a -> b -> b`), hence the lambdas have a different non-matched parameter. Does this answer your question? —F From julian_ong at yahoo.com Sun Jan 17 00:14:47 2021 From: julian_ong at yahoo.com (Julian Ong) Date: Sun, 17 Jan 2021 00:14:47 +0000 (UTC) Subject: [Haskell-beginners] list, map, sequence - stack overflow and performance issues References: <745530623.204282.1610842487199.ref@mail.yahoo.com> Message-ID: <745530623.204282.1610842487199@mail.yahoo.com> Hi Haskellers - I'm learning Haskell and attempting to solve the Advent of Code 2020 puzzles using Haskell. I'm stuck on part 2 of Day 15 and have been for a while now, so I'm reaching out. The puzzle asks you to find the nth element in a list of integers. Here's how the list is constructed: Start with a seed list of integers, like [0,3,6]. Then, referring to the last element (6), the next element is given by these rules: - If the last element was the first time the element has appeared in the list, then the next element is 0. - Otherwise, the next element is the age, or distance in the number of index positions, between the last element and when it last appeared before that. For example, starting with [0,3,6], the next elements are 0, 3, 3, 1, 0, 4, 0, etc. Part 1 of the puzzle asks you to find the 2020th element in the list. You can do this by constructing increasingly longer lists like this (using Data.List): nextNum :: [Int] -> [Int]nextNum l@(x:xs) = if not (x `elem` xs) then 0 : l else age l : l    where        age (x:xs) = let Just i = elemIndex x xs                           in  i+1 Then: head $ (iterate nextNum [6,3,0]) !! 2017 will give you the 2020th element of 436. Note that you provide the starting list in reverse order and iterate so that it will keep adding new elements to the head of the list, which is more efficient than adding to the end. You can also use unfoldr to generate the list element by element like this: nextNum' :: [Int] -> IntnextNum' (x:xs) = if not (x `elem` xs) then 0 else age x xs    where        age x xs = let Just i = elemIndex x xs                               in  i+1 Then: (unfoldr (\l -> Just (nextNum' l, nextNum' l : l)) slist) !! 2016 will give you the 2020th element of 436. --- Part 2 of the puzzle asks you to find the 30000000th element given starting list [9,3,1,0,8,4]. I cannot find a way to do this without stack overflow and performance issues (I've run my attempts overnight with no answer generated). I've tried using Data.Map and Data.Sequence because my Stack Overflow searching suggested these might be more efficient data structures for this sort of task. Here are my attempts: -- Uses Data.Map to avoid duplicate numbers thereby shortening the list. The dictionary entry (k, v) gives the element and the last position of that element. nextNum'' :: (IntMap Int, (Int, Int)) -> (IntMap Int, (Int, Int))nextNum'' (mp, (k, v)) = case IntMap.lookup k mp of    Nothing  -> (IntMap.insert k v mp, (0, v+1))    Just pos -> (IntMap.insert k v mp, (v-pos, v+1)) Then: snd $ (iterate nextNum'' (IntMap.fromList [(0,1),(3,2)],(6,3))) !! 2017 provides the answer for the 2020th element but either stack overflows or runs for hours (if I use a strict version of iterate) trying to figure out the 30000000th element. Similarly, using Data.Sequence, I tried: nextNum''' :: Seq Int -> IntnextNum'''' (xs :|> x) = if not (x `elem` xs) then 0 else age x xs    where        age x xs = let Just i = Seq.elemIndexR x xs                          in  Seq.length xs - i aoc15b' :: Seq Int -> Int -> Intaoc15b' slist tnum = (\(xs :> x) -> x) $ Seq.viewr (Seq.unfoldr (\l -> if Seq.length l == tnum then Nothing else let nnum = force (nextNum'''' l) in Just (nnum, force (l |> nnum))) slist) I found that I needed to fix stack overflow problems by using "force" from Control.DeepSeq. Despite seemingly fixing stack overflow issues though, the calculation just takes too long, and in fact, I have never been able to actually output a solution. I thought that using Data.Map or Data.Sequence would speed things up based on my Stack Overflow searching, but I'm unable to come up with a Haskell solution that runs in reasonable time. I'm at a loss for different strategies at this point and would appreciate any ideas from the community. Thanks, Julian -------------- next part -------------- An HTML attachment was scrubbed... URL: From julian_ong at yahoo.com Sun Jan 17 00:20:29 2021 From: julian_ong at yahoo.com (Julian Ong) Date: Sun, 17 Jan 2021 00:20:29 +0000 (UTC) Subject: [Haskell-beginners] list, map, sequence - stack overflow and performance issues In-Reply-To: <745530623.204282.1610842487199@mail.yahoo.com> References: <745530623.204282.1610842487199.ref@mail.yahoo.com> <745530623.204282.1610842487199@mail.yahoo.com> Message-ID: <296472078.205601.1610842829600@mail.yahoo.com> Sorry, corrected some typos below in the number of apostrophes. On Saturday, January 16, 2021, 04:14:47 PM PST, Julian Ong wrote: Hi Haskellers - I'm learning Haskell and attempting to solve the Advent of Code 2020 puzzles using Haskell. I'm stuck on part 2 of Day 15 and have been for a while now, so I'm reaching out. The puzzle asks you to find the nth element in a list of integers. Here's how the list is constructed: Start with a seed list of integers, like [0,3,6]. Then, referring to the last element (6), the next element is given by these rules: - If the last element was the first time the element has appeared in the list, then the next element is 0. - Otherwise, the next element is the age, or distance in the number of index positions, between the last element and when it last appeared before that. For example, starting with [0,3,6], the next elements are 0, 3, 3, 1, 0, 4, 0, etc. Part 1 of the puzzle asks you to find the 2020th element in the list. You can do this by constructing increasingly longer lists like this (using Data.List): nextNum :: [Int] -> [Int]nextNum l@(x:xs) = if not (x `elem` xs) then 0 : l else age l : l    where        age (x:xs) = let Just i = elemIndex x xs                           in  i+1 Then: head $ (iterate nextNum [6,3,0]) !! 2017 will give you the 2020th element of 436. Note that you provide the starting list in reverse order and iterate so that it will keep adding new elements to the head of the list, which is more efficient than adding to the end. You can also use unfoldr to generate the list element by element like this: nextNum' :: [Int] -> IntnextNum' (x:xs) = if not (x `elem` xs) then 0 else age x xs    where        age x xs = let Just i = elemIndex x xs                               in  i+1 Then: (unfoldr (\l -> Just (nextNum' l, nextNum' l : l)) slist) !! 2016 will give you the 2020th element of 436. --- Part 2 of the puzzle asks you to find the 30000000th element given starting list [9,3,1,0,8,4]. I cannot find a way to do this without stack overflow and performance issues (I've run my attempts overnight with no answer generated). I've tried using Data.Map and Data.Sequence because my Stack Overflow searching suggested these might be more efficient data structures for this sort of task. Here are my attempts: -- Uses Data.Map to avoid duplicate numbers thereby shortening the list. The dictionary entry (k, v) gives the element and the last position of that element. nextNum'' :: (IntMap Int, (Int, Int)) -> (IntMap Int, (Int, Int))nextNum'' (mp, (k, v)) = case IntMap.lookup k mp of    Nothing  -> (IntMap.insert k v mp, (0, v+1))    Just pos -> (IntMap.insert k v mp, (v-pos, v+1)) Then: snd $ (iterate nextNum'' (IntMap.fromList [(0,1),(3,2)],(6,3))) !! 2017 provides the answer for the 2020th element but either stack overflows or runs for hours (if I use a strict version of iterate) trying to figure out the 30000000th element. Similarly, using Data.Sequence, I tried: nextNum''' :: Seq Int -> IntnextNum''' (xs :|> x) = if not (x `elem` xs) then 0 else age x xs    where        age x xs = let Just i = Seq.elemIndexR x xs                          in  Seq.length xs - i aoc15b' :: Seq Int -> Int -> Intaoc15b' slist tnum = (\(xs :> x) -> x) $ Seq.viewr (Seq.unfoldr (\l -> if Seq.length l == tnum then Nothing else let nnum = force (nextNum''' l) in Just (nnum, force (l |> nnum))) slist) I found that I needed to fix stack overflow problems by using "force" from Control.DeepSeq. Despite seemingly fixing stack overflow issues though, the calculation just takes too long, and in fact, I have never been able to actually output a solution. I thought that using Data.Map or Data.Sequence would speed things up based on my Stack Overflow searching, but I'm unable to come up with a Haskell solution that runs in reasonable time. I'm at a loss for different strategies at this point and would appreciate any ideas from the community. Thanks, Julian -------------- next part -------------- An HTML attachment was scrubbed... URL: From borgauf at gmail.com Sun Jan 17 05:32:33 2021 From: borgauf at gmail.com (Lawrence Bottorff) Date: Sat, 16 Jan 2021 23:32:33 -0600 Subject: [Haskell-beginners] Dependent and independent variables in foldl and foldr In-Reply-To: <20210116223555.GA25810@extensa> References: <20210116223555.GA25810@extensa> Message-ID: This is the definition of list foldr foldr :: (a -> b -> b) -> b -> [a] -> b foldr _ z [] = z foldr f z (x:xs) = f x (foldr f z xs) In both foldl and foldr in the OP the n variable in lambda functions would seem to be for the accumulator, hence, I assume the n is considered the free variable? And then the wildcard in each lambda function refers to the bound variable, i.e., the list argument's elements to be folded. So I can recreate foldr (+) 5 [1,2,3,4] with foldr (\x n -> x + n) 5 [1,2,3,4] They both return 15. The first one results in (+) 1 ((+) 2 ((+) 3 ((+) 4 5))) but the second example I'm not sure how the (\x n -> x + n) is being applied in the form . . . f x (foldr f z xs) It obviously must be doing the same (+) 1 ((+) 2 ((+) 3 ((+) 4 5))) but how the function is being applied I don't understand. Beta reduction doesn't get me very far \x n -> x + n (5)([1,2,3,4]) \x 5 -> x + 5 ([1,2,3,4]) but obviously the enclosing lambda calc for foldr is doing something to create the (+) 1 ((+) 2 ((+) 3 ((+) 4 5))) form. BTW, is the t a format in :type foldr foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b something from category theory, i.e., for the list instance, t a is [a] What is the algebraic syntax where t a becomes [a] in the case of lists? It would be nice to understand some day exactly what :i Foldable is saying On Sat, Jan 16, 2021 at 4:36 PM Francesco Ariis wrote: > Il 16 gennaio 2021 alle 16:10 Lawrence Bottorff ha scritto: > > I have this > > > > myLength1 = foldl (\n _ -> n + 1) 0 > > > > and this > > > > myLength2 = foldr (\_ n -> n + 1) 0 > > > > I am guessing that foldl knows to assign the accumulator-seed argument to > > the dependent variable and the list argument's elements recursively to > the > > independent variable; and with foldr to do the opposite. Is this a fair > > assumption? BTW, where can I get a look at the code for fold functions; > or > > does the type definition answer my original question? Not really able to > > decipher it so well > > > > :t foldl > > foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b > > foldl and foldr have slightly different signatures, > > λ> :t +d foldl > foldl :: (b -> a -> b) -> b -> [a] -> b > λ> :t +d foldr > foldr :: (a -> b -> b) -> b -> [a] -> b > > (Notice `b -> a -> b` vs. `a -> b -> b`), hence the lambdas have a > different non-matched parameter. > Does this answer your question? —F > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From fa-ml at ariis.it Sun Jan 17 05:55:20 2021 From: fa-ml at ariis.it (Francesco Ariis) Date: Sun, 17 Jan 2021 06:55:20 +0100 Subject: [Haskell-beginners] Dependent and independent variables in foldl and foldr In-Reply-To: References: <20210116223555.GA25810@extensa> Message-ID: <20210117055520.GA27441@extensa> Il 16 gennaio 2021 alle 23:32 Lawrence Bottorff ha scritto: > This is the definition of list foldr > > foldr :: (a -> b -> b) -> b -> [a] -> b > foldr _ z [] = z > foldr f z (x:xs) = f x (foldr f z xs) > > In both foldl and foldr in the OP the n variable in lambda functions would > seem to be for the accumulator, hence, I assume the n is considered the > free variable? And then the wildcard in each lambda function refers to the > bound variable, i.e., the list argument's elements to be folded. The ‘_’ means «do not bind this parameter». Hence λ> (\x _ -> x + 1) 10 20 11 > but the second example I'm not sure how the (\x n -> x + n) is being > applied in the form . . . f x (foldr f z xs) It obviously must be doing the > same (+) 1 ((+) 2 ((+) 3 ((+) 4 5))) but how the function is being applied > I don't understand. foldr (+) 0 ([1,2,3]) (+) 1 (foldr (+) 0 [2,3]) (+) 1 ((+) 2 (foldr (+) 0 [3])) (+) 1 ((+) 2 ((+) 3 (foldr (+) 0 []))) (+) 1 ((+) 2 ((+) 3 0)) (+) 1 ((+) 2 3) (+) 1 5 6 > BTW, is the t a format in > > :type foldr > foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b > > something from category theory, i.e., for the list instance, t a is [a] What > is the algebraic syntax where t a becomes [a] in the case of lists? It > would be nice to understand some day exactly what :i Foldable is saying What book are you studying on that does not talk about Typeclasses? I feel you are making your life harder by not following a good study program. From julian_ong at yahoo.com Thu Jan 21 07:12:33 2021 From: julian_ong at yahoo.com (Julian Ong) Date: Thu, 21 Jan 2021 07:12:33 +0000 (UTC) Subject: [Haskell-beginners] list, map, sequence - stack overflow and performance issues In-Reply-To: <296472078.205601.1610842829600@mail.yahoo.com> References: <745530623.204282.1610842487199.ref@mail.yahoo.com> <745530623.204282.1610842487199@mail.yahoo.com> <296472078.205601.1610842829600@mail.yahoo.com> Message-ID: <2010737496.3843630.1611213153526@mail.yahoo.com> I have an update on my post. I implemented the same solution in Python 3 using a dictionary and it ran pretty quickly. I couldn't understand why my similar Haskell solution using an IntMap dictionary wasn't running similarly quickly. I have been running Haskell code in GHCI. I decided to break the code for this puzzle out into a separate file. Upon compiling and running, this code produced the correct output relatively quickly, on par with the Python 3 version. I am very surprised the code runs so much faster after compiling with GHC. I'm sure this is obvious to experienced Haskellers but it was new to me that performance could be so drastically different upon compilation, so I'm sharing in case anyone else has run into something similar. Here is my Haskell code: --- import Data.IntMap.Strict (IntMap)import qualified Data.IntMap.Strict as IntMap main = do    let iterate' f x = x `seq` x : iterate' f (f x)    print $ snd $ (iterate' nextNum (IntMap.fromList [(9,1),(3,2),(1,3),(0,4),(8,5)],(4,6)))!!29999994 -- Uses Data.IntMap.Strict to keep track of each number that has appeared in the list and its last position in an IntMap dictionarynextNum :: (IntMap Int, (Int, Int)) -> (IntMap Int, (Int, Int))nextNum (mp, (k, v)) = case IntMap.lookup k mp of    Nothing  -> (IntMap.insert k v mp, (0, v+1))    Just pos -> (IntMap.insert k v mp, (v-pos, v+1)) On Saturday, January 16, 2021, 04:20:29 PM PST, Julian Ong wrote: Sorry, corrected some typos below in the number of apostrophes. On Saturday, January 16, 2021, 04:14:47 PM PST, Julian Ong wrote: Hi Haskellers - I'm learning Haskell and attempting to solve the Advent of Code 2020 puzzles using Haskell. I'm stuck on part 2 of Day 15 and have been for a while now, so I'm reaching out. The puzzle asks you to find the nth element in a list of integers. Here's how the list is constructed: Start with a seed list of integers, like [0,3,6]. Then, referring to the last element (6), the next element is given by these rules: - If the last element was the first time the element has appeared in the list, then the next element is 0. - Otherwise, the next element is the age, or distance in the number of index positions, between the last element and when it last appeared before that. For example, starting with [0,3,6], the next elements are 0, 3, 3, 1, 0, 4, 0, etc. Part 1 of the puzzle asks you to find the 2020th element in the list. You can do this by constructing increasingly longer lists like this (using Data.List): nextNum :: [Int] -> [Int]nextNum l@(x:xs) = if not (x `elem` xs) then 0 : l else age l : l    where        age (x:xs) = let Just i = elemIndex x xs                           in  i+1 Then: head $ (iterate nextNum [6,3,0]) !! 2017 will give you the 2020th element of 436. Note that you provide the starting list in reverse order and iterate so that it will keep adding new elements to the head of the list, which is more efficient than adding to the end. You can also use unfoldr to generate the list element by element like this: nextNum' :: [Int] -> IntnextNum' (x:xs) = if not (x `elem` xs) then 0 else age x xs    where        age x xs = let Just i = elemIndex x xs                               in  i+1 Then: (unfoldr (\l -> Just (nextNum' l, nextNum' l : l)) slist) !! 2016 will give you the 2020th element of 436. --- Part 2 of the puzzle asks you to find the 30000000th element given starting list [9,3,1,0,8,4]. I cannot find a way to do this without stack overflow and performance issues (I've run my attempts overnight with no answer generated). I've tried using Data.Map and Data.Sequence because my Stack Overflow searching suggested these might be more efficient data structures for this sort of task. Here are my attempts: -- Uses Data.Map to avoid duplicate numbers thereby shortening the list. The dictionary entry (k, v) gives the element and the last position of that element. nextNum'' :: (IntMap Int, (Int, Int)) -> (IntMap Int, (Int, Int))nextNum'' (mp, (k, v)) = case IntMap.lookup k mp of    Nothing  -> (IntMap.insert k v mp, (0, v+1))    Just pos -> (IntMap.insert k v mp, (v-pos, v+1)) Then: snd $ (iterate nextNum'' (IntMap.fromList [(0,1),(3,2)],(6,3))) !! 2017 provides the answer for the 2020th element but either stack overflows or runs for hours (if I use a strict version of iterate) trying to figure out the 30000000th element. Similarly, using Data.Sequence, I tried: nextNum''' :: Seq Int -> IntnextNum''' (xs :|> x) = if not (x `elem` xs) then 0 else age x xs    where        age x xs = let Just i = Seq.elemIndexR x xs                          in  Seq.length xs - i aoc15b' :: Seq Int -> Int -> Intaoc15b' slist tnum = (\(xs :> x) -> x) $ Seq.viewr (Seq.unfoldr (\l -> if Seq.length l == tnum then Nothing else let nnum = force (nextNum''' l) in Just (nnum, force (l |> nnum))) slist) I found that I needed to fix stack overflow problems by using "force" from Control.DeepSeq. Despite seemingly fixing stack overflow issues though, the calculation just takes too long, and in fact, I have never been able to actually output a solution. I thought that using Data.Map or Data.Sequence would speed things up based on my Stack Overflow searching, but I'm unable to come up with a Haskell solution that runs in reasonable time. I'm at a loss for different strategies at this point and would appreciate any ideas from the community. Thanks, Julian -------------- next part -------------- An HTML attachment was scrubbed... URL: From 47dragonfyre at gmail.com Thu Jan 21 17:49:03 2021 From: 47dragonfyre at gmail.com (A. Mc.) Date: Thu, 21 Jan 2021 09:49:03 -0800 Subject: [Haskell-beginners] Count Words from File Message-ID: Hello, I apologize in advance if this is crossposting. My IRC client did not appear to be working properly. I am new to Haskell and I need to find a way to count specific words in a file. File could contain spaces between words, no spacing, uppercase, lowercase, etc so I've standardized it to once the file is taken in, convert to lowercase and remove the spacing. I've also read the postings about using ByteString instead of [Char] so I am trying to use that. But, as it still seems to either view all elements as fused or each letter as individual, I'm not entirely sure how to tackle this. The input after transforming would be something like "theblueskyisveryblue" for uniformity and would need to count "the" and "blue". Feels like I should be able to do a map and foldr(?) but I'm not sure how to get Haskell to recognize 'the' for example and not count all the t's, h's, e's etc in the file, nor am I entirely sure how to properly compose a map-fold for character arrays like this. Thanks in advance and thank you for your time. -------------- next part -------------- An HTML attachment was scrubbed... URL: From fa-ml at ariis.it Thu Jan 21 17:53:12 2021 From: fa-ml at ariis.it (Francesco Ariis) Date: Thu, 21 Jan 2021 18:53:12 +0100 Subject: [Haskell-beginners] Count Words from File In-Reply-To: References: Message-ID: <20210121175312.GA23139@extensa> Il 21 gennaio 2021 alle 09:49 A. Mc. ha scritto: > I am new to Haskell and I need to find a way to count specific words in a > file. File could contain spaces between words, no spacing, uppercase, > lowercase, etc so I've standardized it to once the file is taken in, > convert to lowercase and remove the spacing. I've also read the postings > about using ByteString instead of [Char] so I am trying to use that. But, > as it still seems to either view all elements as fused or each letter as > individual, I'm not entirely sure how to tackle this. The input after > transforming would be something like "theblueskyisveryblue" for uniformity > and would need to count "the" and "blue". Feels like I should be able to > do a map and foldr(?) but I'm not sure how to get Haskell to recognize > 'the' for example and not count all the t's, h's, e's etc in the file, nor > am I entirely sure how to properly compose a map-fold for character arrays > like this. Have you considered using `words`? λ> :t words words :: String -> [String] λ> words "Chiare, fresche e dolci acque" ["Chiare,","fresche","e","dolci","acque"] From 47dragonfyre at gmail.com Fri Jan 22 22:36:16 2021 From: 47dragonfyre at gmail.com (A. Mc.) Date: Fri, 22 Jan 2021 14:36:16 -0800 Subject: [Haskell-beginners] Count Words In File In-Reply-To: References: Message-ID: I had thought about doing something like that, except about 80% of the time the words in the file won't have a delimitor such as in "theblueskythegreenearth" but I still need to be able to count "the" or the ordered sequence of [ t, h, e ] within such a character string (or [ b, l, u, e ] or whatever) Again, thank you for your time & thanks in advance. On Fri, Jan 22, 2021, 4:03 AM wrote: > Send Beginners mailing list submissions to > beginners at haskell.org > > To subscribe or unsubscribe via the World Wide Web, visit > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > or, via email, send a message with subject or body 'help' to > beginners-request at haskell.org > > You can reach the person managing the list at > beginners-owner at haskell.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Beginners digest..." > > > Today's Topics: > > 1. Count Words from File (A. Mc.) > 2. Re: Count Words from File (Francesco Ariis) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Thu, 21 Jan 2021 09:49:03 -0800 > From: "A. Mc." <47dragonfyre at gmail.com> > To: beginners at haskell.org > Subject: [Haskell-beginners] Count Words from File > Message-ID: > cC3ZSGsKUmZsF7jivpTkNcyACRfYC01ExPPtg at mail.gmail.com> > Content-Type: text/plain; charset="utf-8" > > Hello, > > I apologize in advance if this is crossposting. My IRC client did not > appear to be working properly. > > I am new to Haskell and I need to find a way to count specific words in a > file. File could contain spaces between words, no spacing, uppercase, > lowercase, etc so I've standardized it to once the file is taken in, > convert to lowercase and remove the spacing. I've also read the postings > about using ByteString instead of [Char] so I am trying to use that. But, > as it still seems to either view all elements as fused or each letter as > individual, I'm not entirely sure how to tackle this. The input after > transforming would be something like "theblueskyisveryblue" for uniformity > and would need to count "the" and "blue". Feels like I should be able to > do a map and foldr(?) but I'm not sure how to get Haskell to recognize > 'the' for example and not count all the t's, h's, e's etc in the file, nor > am I entirely sure how to properly compose a map-fold for character arrays > like this. > > Thanks in advance and thank you for your time. > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: < > http://mail.haskell.org/pipermail/beginners/attachments/20210121/56633de9/attachment-0001.html > > > > ------------------------------ > > Message: 2 > Date: Thu, 21 Jan 2021 18:53:12 +0100 > From: Francesco Ariis > To: beginners at haskell.org > Subject: Re: [Haskell-beginners] Count Words from File > Message-ID: <20210121175312.GA23139 at extensa> > Content-Type: text/plain; charset=utf-8 > > Il 21 gennaio 2021 alle 09:49 A. Mc. ha scritto: > > I am new to Haskell and I need to find a way to count specific words in a > > file. File could contain spaces between words, no spacing, uppercase, > > lowercase, etc so I've standardized it to once the file is taken in, > > convert to lowercase and remove the spacing. I've also read the postings > > about using ByteString instead of [Char] so I am trying to use that. > But, > > as it still seems to either view all elements as fused or each letter as > > individual, I'm not entirely sure how to tackle this. The input after > > transforming would be something like "theblueskyisveryblue" for > uniformity > > and would need to count "the" and "blue". Feels like I should be able to > > do a map and foldr(?) but I'm not sure how to get Haskell to recognize > > 'the' for example and not count all the t's, h's, e's etc in the file, > nor > > am I entirely sure how to properly compose a map-fold for character > arrays > > like this. > > Have you considered using `words`? > > λ> :t words > words :: String -> [String] > λ> words "Chiare, fresche e dolci acque" > ["Chiare,","fresche","e","dolci","acque"] > > > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > ------------------------------ > > End of Beginners Digest, Vol 150, Issue 10 > ****************************************** > -------------- next part -------------- An HTML attachment was scrubbed... URL: From 47dragonfyre at gmail.com Mon Jan 25 01:59:34 2021 From: 47dragonfyre at gmail.com (A. Mc.) Date: Sun, 24 Jan 2021 17:59:34 -0800 Subject: [Haskell-beginners] Viewing a Definition Message-ID: Hello, It looks like the best way for me to tackle my problem is to create a modified form of Data.Text isInfixOf where the return is an int rather than a bool, to be able to sum words that appear twice or more, but ignoring the whitespace delimitor. Does anyone know where to go to view the actual definition rather than the type signature? Thanks in advance and thank you for your time. -------------- next part -------------- An HTML attachment was scrubbed... URL: From fa-ml at ariis.it Mon Jan 25 02:16:55 2021 From: fa-ml at ariis.it (Francesco Ariis) Date: Mon, 25 Jan 2021 03:16:55 +0100 Subject: [Haskell-beginners] Viewing a Definition In-Reply-To: References: Message-ID: <20210125021655.GA2799@extensa> Hello A., Il 24 gennaio 2021 alle 17:59 A. Mc. ha scritto: > It looks like the best way for me to tackle my problem is to create a > modified form of Data.Text isInfixOf where the return is an int rather than > a bool, to be able to sum words that appear twice or more, but ignoring the > whitespace delimitor. Does anyone know where to go to view the actual > definition rather than the type signature? In the docs [1], when you click on «Source» button (on the right), you will get the source —F [1] https://hackage.haskell.org/package/text-1.2.4.1/docs/Data-Text.html#v:isInfixOf From borgauf at gmail.com Mon Jan 25 18:16:05 2021 From: borgauf at gmail.com (Lawrence Bottorff) Date: Mon, 25 Jan 2021 12:16:05 -0600 Subject: [Haskell-beginners] $ versus . Message-ID: I've got this > init $ tail [1,2,3] [2] and this > chopEnds = init $ tail > chopEnds [1,2,3] [1,2] What happened? Why is it not just init $ tail [1,2,3] ? This works fine > chopEnds2 = init . tail > chopEnds2 [1,2,3] [2] What am I missing? LB -------------- next part -------------- An HTML attachment was scrubbed... URL: From bob at redivi.com Mon Jan 25 19:18:20 2021 From: bob at redivi.com (Bob Ippolito) Date: Mon, 25 Jan 2021 11:18:20 -0800 Subject: [Haskell-beginners] $ versus . In-Reply-To: References: Message-ID: I think what you're missing is what you actually typed in the first case. This is a type error, it will not compile or run: chopEnds = init $ tail The $ operator can always be rewritten as parentheses, in this case: chopEnds = init (tail) Which has the same incorrectly typed meaning as: chopEnds = init tail The "result" you pasted looks equivalent to: chopEnds = init Perhaps this is what you typed? In this case the argument tail it will shadow the existing binding of the Prelude tail, which would be confusing so with -Wall it would issue a compiler warning: chopEnds tail = init $ tail :2:10: warning: [-Wname-shadowing] This binding for ‘tail’ shadows the existing binding imported from ‘Prelude’ (and originally defined in ‘GHC.List’) On Mon, Jan 25, 2021 at 10:16 AM Lawrence Bottorff wrote: > I've got this > > > init $ tail [1,2,3] > [2] > > and this > > > chopEnds = init $ tail > > chopEnds [1,2,3] > [1,2] > > What happened? Why is it not just init $ tail [1,2,3] ? > > This works fine > > > chopEnds2 = init . tail > > chopEnds2 [1,2,3] > [2] > > What am I missing? > > LB > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ky3 at atamo.com Tue Jan 26 01:58:11 2021 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Tue, 26 Jan 2021 08:58:11 +0700 Subject: [Haskell-beginners] $ versus . In-Reply-To: References: Message-ID: init $ tail [1,2,3] = init (tail ([1,2,3])) -- a la Lisp Now, functional programming is awesomest at abstractions. What if we could abstract out "init (tail"? Then we could write chopEnds = init (tail But that looks weird. It's only got the left half of a parens pair! Does that explain why you should not expect the same result? A separate question is why the compiler even type-checks "init $ tail" in the first place. What do you think is going on there? On Tue, Jan 26, 2021 at 1:16 AM Lawrence Bottorff wrote: > I've got this > > > init $ tail [1,2,3] > [2] > > and this > > > chopEnds = init $ tail > > chopEnds [1,2,3] > [1,2] > > What happened? Why is it not just init $ tail [1,2,3] ? > > This works fine > > > chopEnds2 = init . tail > > chopEnds2 [1,2,3] > [2] > > What am I missing? > > LB > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -- -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From borgauf at gmail.com Tue Jan 26 16:23:12 2021 From: borgauf at gmail.com (Lawrence Bottorff) Date: Tue, 26 Jan 2021 10:23:12 -0600 Subject: [Haskell-beginners] $ versus . In-Reply-To: References: Message-ID: > :t (init tail) : error: : * Couldn't match expected type `[a]' : with actual type `[a0] -> [a0]' : * Probable cause: `tail' is applied to too few arguments : In the first argument of `init', namely `tail' : In the expression: (init tail) > :t (init . tail) : (init . tail) :: [a] -> [a] > :t init $ tail : error: * Couldn't match expected type `[a]' with actual type `[a0] -> [a0]' * Probable cause: `tail' is applied to too few arguments In the second argument of `($)', namely `tail' In the expression: init $ tail > chopEnds = init $ tail > chopEnds [1,2,3] error: ... * Variable not in scope: chopEnds1 :: [Integer] -> t ... but then > init $ tail [1,2,3] [2] Not sure what I'm missing here. It doesn't make sense to me that the last expression works, but no version of a closure chopEnds = init $ tail does. On Mon, Jan 25, 2021 at 7:58 PM Kim-Ee Yeoh wrote: > init $ tail [1,2,3] > = init (tail ([1,2,3])) -- a la Lisp > > Now, functional programming is awesomest at abstractions. What if we could > abstract out "init (tail"? > > Then we could write > > chopEnds = init (tail > > But that looks weird. It's only got the left half of a parens pair! > > Does that explain why you should not expect the same result? > > A separate question is why the compiler even type-checks "init $ tail" in > the first place. What do you think is going on there? > > On Tue, Jan 26, 2021 at 1:16 AM Lawrence Bottorff > wrote: > >> I've got this >> >> > init $ tail [1,2,3] >> [2] >> >> and this >> >> > chopEnds = init $ tail >> > chopEnds [1,2,3] >> [1,2] >> >> What happened? Why is it not just init $ tail [1,2,3] ? >> >> This works fine >> >> > chopEnds2 = init . tail >> > chopEnds2 [1,2,3] >> [2] >> >> What am I missing? >> >> LB >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> > -- > -- Kim-Ee > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bob at redivi.com Tue Jan 26 17:25:03 2021 From: bob at redivi.com (Bob Ippolito) Date: Tue, 26 Jan 2021 09:25:03 -0800 Subject: [Haskell-beginners] $ versus . In-Reply-To: References: Message-ID: The difference is that one of them is: — unnecessary parentheses for emphasis (init tail) [1, 2, 3] And the other is — parentheses required for correct evaluation init (tail [1, 2, 3]) On Tue, Jan 26, 2021 at 08:23 Lawrence Bottorff wrote: > > :t (init tail) > : error: > : * Couldn't match expected type `[a]' > : with actual type `[a0] -> [a0]' > : * Probable cause: `tail' is applied to too few arguments > : In the first argument of `init', namely `tail' > : In the expression: (init tail) > > > :t (init . tail) > : (init . tail) :: [a] -> [a] > > > :t init $ tail > : error: > * Couldn't match expected type `[a]' > with actual type `[a0] -> [a0]' > * Probable cause: `tail' is applied to too few arguments > In the second argument of `($)', namely `tail' > In the expression: init $ tail > > > chopEnds = init $ tail > > chopEnds [1,2,3] > error: ... > * Variable not in scope: chopEnds1 :: [Integer] -> t > ... > > but then > > > init $ tail [1,2,3] > [2] > > Not sure what I'm missing here. It doesn't make sense to me that the last > expression works, but no version of a closure > > chopEnds = init $ tail > > does. > > On Mon, Jan 25, 2021 at 7:58 PM Kim-Ee Yeoh wrote: > >> init $ tail [1,2,3] >> = init (tail ([1,2,3])) -- a la Lisp >> >> Now, functional programming is awesomest at abstractions. What if we >> could abstract out "init (tail"? >> >> Then we could write >> >> chopEnds = init (tail >> >> But that looks weird. It's only got the left half of a parens pair! >> >> Does that explain why you should not expect the same result? >> >> A separate question is why the compiler even type-checks "init $ tail" in >> the first place. What do you think is going on there? >> >> On Tue, Jan 26, 2021 at 1:16 AM Lawrence Bottorff >> wrote: >> >>> I've got this >>> >>> > init $ tail [1,2,3] >>> [2] >>> >>> and this >>> >>> > chopEnds = init $ tail >>> > chopEnds [1,2,3] >>> [1,2] >>> >>> What happened? Why is it not just init $ tail [1,2,3] ? >>> >>> This works fine >>> >>> > chopEnds2 = init . tail >>> > chopEnds2 [1,2,3] >>> [2] >>> >>> What am I missing? >>> >>> LB >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>> >> -- >> -- Kim-Ee >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From borgauf at gmail.com Tue Jan 26 21:31:22 2021 From: borgauf at gmail.com (Lawrence Bottorff) Date: Tue, 26 Jan 2021 15:31:22 -0600 Subject: [Haskell-beginners] Defined list data type compared to Haskell List Message-ID: I'm following this and yet I see this solution data NestedList a = Elem a | List [NestedList a] deriving (Show) flatten1 :: NestedList a -> [a] flatten1 (Elem a ) = [a] flatten1 (List (x:xs)) = flatten1 x ++ flatten1 (List xs) flatten1 (List []) = [] What I find puzzling is this line flatten1 (List (x:xs)) = flatten1 x ++ flatten1 (List xs) where I see (List (x:xs)) as an argument. How is the NestedList type also able to be expressed as a normal consed list with x:xs argument? How is (:) interacting with NestedList? LB -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Tue Jan 26 21:42:07 2021 From: toad3k at gmail.com (David McBride) Date: Tue, 26 Jan 2021 16:42:07 -0500 Subject: [Haskell-beginners] Defined list data type compared to Haskell List In-Reply-To: References: Message-ID: In NestedList, the List constructor takes a regular list of NestedLists. Therefore when pattern matching on it you can get access to those nested lists. In your code, x is the first NestedList, and xs is the rest of the NestedLists. On Tue, Jan 26, 2021 at 4:32 PM Lawrence Bottorff wrote: > I'm following this > and yet I see this solution > > data NestedList a = Elem a | List [NestedList a] deriving (Show) > > flatten1 :: NestedList a -> [a] > flatten1 (Elem a ) = [a] > flatten1 (List (x:xs)) = flatten1 x ++ flatten1 (List xs) > flatten1 (List []) = [] > > What I find puzzling is this line > > flatten1 (List (x:xs)) = flatten1 x ++ flatten1 (List xs) > > where I see > > (List (x:xs)) as an argument. How is the NestedList type also able to be > expressed as a normal consed list with x:xs argument? How is (:) > interacting with NestedList? > > LB > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From borgauf at gmail.com Tue Jan 26 21:49:26 2021 From: borgauf at gmail.com (Lawrence Bottorff) Date: Tue, 26 Jan 2021 15:49:26 -0600 Subject: [Haskell-beginners] Defined list data type compared to Haskell List In-Reply-To: References: Message-ID: So NestedList is using regular List? So in data NestedList a = Elem a | List [NestedList a] the second data constructor List [NestedList a] we see a "regular" list because of the square brackets? On Tue, Jan 26, 2021 at 3:42 PM David McBride wrote: > In NestedList, the List constructor takes a regular list of NestedLists. > Therefore when pattern matching on it you can get access to those nested > lists. In your code, x is the first NestedList, and xs is the rest of the > NestedLists. > > On Tue, Jan 26, 2021 at 4:32 PM Lawrence Bottorff > wrote: > >> I'm following this >> and yet I see this solution >> >> data NestedList a = Elem a | List [NestedList a] deriving (Show) >> >> flatten1 :: NestedList a -> [a] >> flatten1 (Elem a ) = [a] >> flatten1 (List (x:xs)) = flatten1 x ++ flatten1 (List xs) >> flatten1 (List []) = [] >> >> What I find puzzling is this line >> >> flatten1 (List (x:xs)) = flatten1 x ++ flatten1 (List xs) >> >> where I see >> >> (List (x:xs)) as an argument. How is the NestedList type also able to be >> expressed as a normal consed list with x:xs argument? How is (:) >> interacting with NestedList? >> >> LB >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Tue Jan 26 22:04:54 2021 From: toad3k at gmail.com (David McBride) Date: Tue, 26 Jan 2021 17:04:54 -0500 Subject: [Haskell-beginners] Defined list data type compared to Haskell List In-Reply-To: References: Message-ID: Right that is a plain list of NestedLists. So if you were to rewrite [a] as (Regularlist a) so to speak (not a real type), the definition of NestedList would be List (RegularList (NestedList a)). Keep in mind that List is a constructor, everything after it is types. On Tue, Jan 26, 2021 at 4:50 PM Lawrence Bottorff wrote: > So NestedList is using regular List? So in > > data NestedList a = Elem a | List [NestedList a] > > the second data constructor List [NestedList a] we see a "regular" list > because of the square brackets? > > On Tue, Jan 26, 2021 at 3:42 PM David McBride wrote: > >> In NestedList, the List constructor takes a regular list of NestedLists. >> Therefore when pattern matching on it you can get access to those nested >> lists. In your code, x is the first NestedList, and xs is the rest of the >> NestedLists. >> >> On Tue, Jan 26, 2021 at 4:32 PM Lawrence Bottorff >> wrote: >> >>> I'm following this >>> and yet I see this solution >>> >>> data NestedList a = Elem a | List [NestedList a] deriving (Show) >>> >>> flatten1 :: NestedList a -> [a] >>> flatten1 (Elem a ) = [a] >>> flatten1 (List (x:xs)) = flatten1 x ++ flatten1 (List xs) >>> flatten1 (List []) = [] >>> >>> What I find puzzling is this line >>> >>> flatten1 (List (x:xs)) = flatten1 x ++ flatten1 (List xs) >>> >>> where I see >>> >>> (List (x:xs)) as an argument. How is the NestedList type also able to be >>> expressed as a normal consed list with x:xs argument? How is (:) >>> interacting with NestedList? >>> >>> LB >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From borgauf at gmail.com Tue Jan 26 22:39:11 2021 From: borgauf at gmail.com (Lawrence Bottorff) Date: Tue, 26 Jan 2021 16:39:11 -0600 Subject: [Haskell-beginners] Defined list data type compared to Haskell List In-Reply-To: References: Message-ID: Why can I not do this? data NestedList1 a = Elem a | NList (NestedList1 a) that is, with parens rather than square brackets, then myList2 = (NList (Elem 1, NList (Elem 2, NList (Elem 3, Elem 4), Elem 5))) It gives the error :383:18-73: error: ,* Couldn't match expected type `NestedList1 a' with actual type `(NestedList1 Integer, NestedList1 a0)' ,* In the first argument of `NList', namely `(Elem 1, NList (Elem 2, NList (Elem 3, Elem 4), Elem 5))' In the expression: (NList (Elem 1, NList (Elem 2, NList (Elem 3, Elem 4), Elem 5))) In an equation for `myList2': myList2 = (NList (Elem 1, NList (Elem 2, NList (Elem 3, Elem 4), Elem 5))) ,* Relevant bindings include myList2 :: NestedList1 a (bound at :383:1) etc., etc. On Tue, Jan 26, 2021 at 4:05 PM David McBride wrote: > Right that is a plain list of NestedLists. So if you were to rewrite [a] > as (Regularlist a) so to speak (not a real type), the definition of > NestedList would be List (RegularList (NestedList a)). > > Keep in mind that List is a constructor, everything after it is types. > > On Tue, Jan 26, 2021 at 4:50 PM Lawrence Bottorff > wrote: > >> So NestedList is using regular List? So in >> >> data NestedList a = Elem a | List [NestedList a] >> >> the second data constructor List [NestedList a] we see a "regular" list >> because of the square brackets? >> >> On Tue, Jan 26, 2021 at 3:42 PM David McBride wrote: >> >>> In NestedList, the List constructor takes a regular list of >>> NestedLists. Therefore when pattern matching on it you can get access to >>> those nested lists. In your code, x is the first NestedList, and xs is the >>> rest of the NestedLists. >>> >>> On Tue, Jan 26, 2021 at 4:32 PM Lawrence Bottorff >>> wrote: >>> >>>> I'm following this >>>> and yet I see this solution >>>> >>>> data NestedList a = Elem a | List [NestedList a] deriving (Show) >>>> >>>> flatten1 :: NestedList a -> [a] >>>> flatten1 (Elem a ) = [a] >>>> flatten1 (List (x:xs)) = flatten1 x ++ flatten1 (List xs) >>>> flatten1 (List []) = [] >>>> >>>> What I find puzzling is this line >>>> >>>> flatten1 (List (x:xs)) = flatten1 x ++ flatten1 (List xs) >>>> >>>> where I see >>>> >>>> (List (x:xs)) as an argument. How is the NestedList type also able to >>>> be expressed as a normal consed list with x:xs argument? How is (:) >>>> interacting with NestedList? >>>> >>>> LB >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Tue Jan 26 23:14:27 2021 From: toad3k at gmail.com (David McBride) Date: Tue, 26 Jan 2021 18:14:27 -0500 Subject: [Haskell-beginners] Defined list data type compared to Haskell List In-Reply-To: References: Message-ID: You can, you have to write it slightly differently. But defined that way it can only ever have one element because NList only takes one. mylist = NList (NList (NList (Elem 1))) You could also change it to data NestedList a = Elem a | NList a (NestedList a) But at that point you have a nonempty list type (always at least one element) which already exists. On Tue, Jan 26, 2021, 17:40 Lawrence Bottorff wrote: > Why can I not do this? > > data NestedList1 a = Elem a | NList (NestedList1 a) > > that is, with parens rather than square brackets, then > > myList2 = (NList (Elem 1, NList (Elem 2, NList (Elem 3, Elem 4), Elem 5))) > > It gives the error > > :383:18-73: error: > ,* Couldn't match expected type `NestedList1 a' > with actual type `(NestedList1 Integer, NestedList1 a0)' > ,* In the first argument of `NList', namely > `(Elem 1, NList (Elem 2, NList (Elem 3, Elem 4), Elem 5))' > In the expression: > (NList (Elem 1, NList (Elem 2, NList (Elem 3, Elem 4), Elem 5))) > In an equation for `myList2': > myList2 > = (NList (Elem 1, NList (Elem 2, NList (Elem 3, Elem 4), Elem > 5))) > ,* Relevant bindings include > myList2 :: NestedList1 a (bound at :383:1) > > etc., etc. > > On Tue, Jan 26, 2021 at 4:05 PM David McBride wrote: > >> Right that is a plain list of NestedLists. So if you were to rewrite [a] >> as (Regularlist a) so to speak (not a real type), the definition of >> NestedList would be List (RegularList (NestedList a)). >> >> Keep in mind that List is a constructor, everything after it is types. >> >> On Tue, Jan 26, 2021 at 4:50 PM Lawrence Bottorff >> wrote: >> >>> So NestedList is using regular List? So in >>> >>> data NestedList a = Elem a | List [NestedList a] >>> >>> the second data constructor List [NestedList a] we see a "regular" list >>> because of the square brackets? >>> >>> On Tue, Jan 26, 2021 at 3:42 PM David McBride wrote: >>> >>>> In NestedList, the List constructor takes a regular list of >>>> NestedLists. Therefore when pattern matching on it you can get access to >>>> those nested lists. In your code, x is the first NestedList, and xs is the >>>> rest of the NestedLists. >>>> >>>> On Tue, Jan 26, 2021 at 4:32 PM Lawrence Bottorff >>>> wrote: >>>> >>>>> I'm following this >>>>> and yet I see this solution >>>>> >>>>> data NestedList a = Elem a | List [NestedList a] deriving (Show) >>>>> >>>>> flatten1 :: NestedList a -> [a] >>>>> flatten1 (Elem a ) = [a] >>>>> flatten1 (List (x:xs)) = flatten1 x ++ flatten1 (List xs) >>>>> flatten1 (List []) = [] >>>>> >>>>> What I find puzzling is this line >>>>> >>>>> flatten1 (List (x:xs)) = flatten1 x ++ flatten1 (List xs) >>>>> >>>>> where I see >>>>> >>>>> (List (x:xs)) as an argument. How is the NestedList type also able to >>>>> be expressed as a normal consed list with x:xs argument? How is (:) >>>>> interacting with NestedList? >>>>> >>>>> LB >>>>> _______________________________________________ >>>>> Beginners mailing list >>>>> Beginners at haskell.org >>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From borgauf at gmail.com Wed Jan 27 06:01:23 2021 From: borgauf at gmail.com (Lawrence Bottorff) Date: Wed, 27 Jan 2021 00:01:23 -0600 Subject: [Haskell-beginners] As pattern, @ Message-ID: I'm looking at this and wondering how the "as pattern" actually is working compress (x:ys@(y:_)) | x == y = compress ys | otherwise = x : compress ys compress ys = ys I'm sure it's just some version of my stab at eliminating consecutive duplicates in a list compress :: Eq a => [a] -> [a] compress [] = [] compress [x] = [x] compress (x:y:xs) = if x == y then compress (y:xs) else x : compress (y:xs) only smarter. Could someone walk me through the (x:ys@(y:_)) part? LB -------------- next part -------------- An HTML attachment was scrubbed... URL: From utprimum at gmail.com Wed Jan 27 06:14:35 2021 From: utprimum at gmail.com (Ut Primum) Date: Wed, 27 Jan 2021 07:14:35 +0100 Subject: [Haskell-beginners] As pattern, @ In-Reply-To: References: Message-ID: Hi, between the arguments of a function, ys@(y:yss) would mean that we call that argument ys, we call its head y and its tail yss. In your case the tail is replaced by _ , because is never used inside the function. Il mer 27 gen 2021, 07:02 Lawrence Bottorff ha scritto: > I'm looking at this > and wondering how the "as pattern" actually is working > > compress (x:ys@(y:_)) > | x == y = compress ys > | otherwise = x : compress ys > compress ys = ys > > I'm sure it's just some version of my stab at eliminating consecutive > duplicates in a list > > compress :: Eq a => [a] -> [a] > compress [] = [] > compress [x] = [x] > compress (x:y:xs) = if x == y then compress (y:xs) else x : compress (y:xs) > > only smarter. Could someone walk me through the (x:ys@(y:_)) part? > > LB > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From 47dragonfyre at gmail.com Fri Jan 29 06:04:10 2021 From: 47dragonfyre at gmail.com (A. Mc.) Date: Thu, 28 Jan 2021 22:04:10 -0800 Subject: [Haskell-beginners] GHCi Debugging Message-ID: Hello, I suspect that the function recursion composition I have used is not working very well, so I'm trying to learn how to use the debugger to understand what is going on, except I don't really understand the output of the GHCi debugger or what is going on very well. I have a main function called 'Analysis' in a library that analyzes a string. I've used :break Analysis and then called :trace and then repeatedly typed :continue, but there are a number of steps between one continue and the next return-and-call of output that I'm not sure I understand. Suggestions on better debugging methods, or even how to fully understand this output, would be very helpful. Thanks in advance. _result :: Bool = _ key :: Int = -18 message :: [Char] = _ [Lib.hs:17:8-69] *Main> :continue Stopped in Main.Analysis, Lib.hs:18:8-84 _result :: Bool = _ key :: Int = -18 message :: [Char] = _ [Lib.hs:18:8-84] *Main> :continue Stopped in Main.Analysis, Lib.hs:19:8-65 _result :: Bool = _ key :: Int = -18 message :: [Char] = "esleespmwfpdvjtdgpcjmwfpzgpcespcp" [Lib.hs:19:8-65] *Main> :continue Stopped in Main.Analysis, Lib.hs:19:70-127 _result :: (Int, Text) = _ key :: Int = -18 message :: [Char] = "esleespmwfpdvjtdgpcjmwfpzgpcespcp" [Lib.hs:19:70-127] *Main> :continue Stopped in Main.Analysis, Lib.hs:15:8-14 _result :: Bool = _ key :: Int = _ [Lib.hs:15:8-14] *Main> :continue Stopped in Main.Analysis, Lib.hs:16:8-70 -------------- next part -------------- An HTML attachment was scrubbed... URL: From fa-ml at ariis.it Fri Jan 29 06:19:46 2021 From: fa-ml at ariis.it (Francesco Ariis) Date: Fri, 29 Jan 2021 07:19:46 +0100 Subject: [Haskell-beginners] GHCi Debugging In-Reply-To: References: Message-ID: <20210129061946.GA7871@extensa> Il 28 gennaio 2021 alle 22:04 A. Mc. ha scritto: > I suspect that the function recursion composition I have used is not > working very well, so I'm trying to learn how to use the debugger to > understand what is going on, except I don't really understand the output of > the GHCi debugger or what is going on very well. I have a main function > called 'Analysis' in a library that analyzes a string. I've used :break > Analysis and then called :trace and then repeatedly typed :continue, but > there are a number of steps between one continue and the next > return-and-call of output that I'm not sure I understand. Suggestions on > better debugging methods, or even how to fully understand this output, > would be very helpful. Thanks in advance. For pure functions, debug [1] is Godsent. [1] https://hackage.haskell.org/package/debug