From alexander at chenjia.nl Mon Feb 3 08:14:24 2020
From: alexander at chenjia.nl (Alexander Chen)
Date: Mon, 3 Feb 2020 09:14:24 +0100 (CET)
Subject: [Haskell-beginners] Need some understanding on this behavior.
Message-ID: <1418257784.758267.1580717664665@ichabod.co-bxl>
hi,
I need some understanding on the following behavior:
a = [1..]
[x | x <-a, x <= 100]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
(note the missing ']' which results in the command not finishing)
b = take 1000 a
[x | x <-b, x <= 100]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]
Note, I took more for b just for ease sake. But now the command finishes correctly.
Question:
why can i not use a lazy list directly in a list comprehension?
best,
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From alexander at klink.name Mon Feb 3 08:59:47 2020
From: alexander at klink.name (Alexander Klink)
Date: Mon, 3 Feb 2020 09:59:47 +0100
Subject: [Haskell-beginners] Need some understanding on this behavior.
In-Reply-To: <1418257784.758267.1580717664665@ichabod.co-bxl>
References: <1418257784.758267.1580717664665@ichabod.co-bxl>
Message-ID: <20200203085947.GB11475@putna.0x90.eu>
Hi Alexander,
On Mon, Feb 03, 2020 at 09:14:24AM +0100, Alexander Chen wrote:
> I need some understanding on the following behavior:
>
> a = [1..]
> [x | x <-a, x <= 100]
> [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
>
> (note the missing ']' which results in the command not finishing)
It does not finish because the list comprehension does not know that a
is ordered, so it has to go through *every* element and take those that
are less than or equal to 100.
HTH,
Alex
From toad3k at gmail.com Tue Feb 4 13:44:27 2020
From: toad3k at gmail.com (David McBride)
Date: Tue, 4 Feb 2020 08:44:27 -0500
Subject: [Haskell-beginners] Need some understanding on this behavior.
In-Reply-To: <1418257784.758267.1580717664665@ichabod.co-bxl>
References: <1418257784.758267.1580717664665@ichabod.co-bxl>
Message-ID:
You can absolutely use a lazy list in a list comprehension. What you
cannot do is evaluate an infinitely long list.
When you evaluate it so that it will be printed you are saying, print the
*entire* list. You may know that this list will never contain an element
over 100, but as far as haskell is concerned, there could be an element
after 100 and it will keep crunching to find it, by incrementing x and
testing it against x <= 100 forever, even though it will never succeed.
On Mon, Feb 3, 2020 at 3:14 AM Alexander Chen wrote:
>
> hi,
>
> I need some understanding on the following behavior:
>
> a = [1..]
> [x | x <-a, x <= 100]
>
> [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
>
> (note the missing ']' which results in the command not finishing)
>
> b = take 1000 a
> [x | x <-b, x <= 100]
>
> [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]
>
> Note, I took more for b just for ease sake. But now the command finishes
> correctly.
>
>
> Question:
> why can i not use a lazy list directly in a list comprehension?
>
> best,
>
>
>
>
> _______________________________________________
> 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 alexander at chenjia.nl Tue Feb 4 14:00:11 2020
From: alexander at chenjia.nl (Alexander Chen)
Date: Tue, 4 Feb 2020 15:00:11 +0100 (CET)
Subject: [Haskell-beginners] Need some understanding on this behavior.
In-Reply-To:
References: <1418257784.758267.1580717664665@ichabod.co-bxl>
Message-ID: <1102090692.858814.1580824811789@ichabod.co-bxl>
Hi,
thank you for the explanation. I know now how not to use it but for deeper understanding.
question:
Can a lazy list be ordered or is that just not possible? And how can you create a lazy list like this [1..] without there being any implicit order in it?
I don't want to be nitt-picking, i am trying to reconcile this paradox for deeper understanding.
thank you.
best,
February 4, 2020 2:44:27 PM CET David McBride wrote:
You can absolutely use a lazy list in a list comprehension. What you cannot do is evaluate an infinitely long list.
When you evaluate it so that it will be printed you are saying, print the *entire* list. You may know that this list will never contain an element over 100, but as far as haskell is concerned, there could be an element after 100 and it will keep crunching to find it, by incrementing x and testing it against x <= 100 forever, even though it will never succeed.
On Mon, Feb 3, 2020 at 3:14 AM Alexander Chen wrote:
hi,
I need some understanding on the following behavior:
a = [1..]
[x | x <-a, x <= 100]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
(note the missing ']' which results in the command not finishing)
b = take 1000 a
[x | x <-b, x <= 100]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]
Note, I took more for b just for ease sake. But now the command finishes correctly.
Question:
why can i not use a lazy list directly in a list comprehension?
best,
_______________________________________________
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 utprimum at gmail.com Tue Feb 4 15:23:18 2020
From: utprimum at gmail.com (Ut Primum)
Date: Tue, 4 Feb 2020 16:23:18 +0100
Subject: [Haskell-beginners] Need some understanding on this behavior.
In-Reply-To: <1102090692.858814.1580824811789@ichabod.co-bxl>
References: <1418257784.758267.1580717664665@ichabod.co-bxl>
<1102090692.858814.1580824811789@ichabod.co-bxl>
Message-ID:
Hi,
You can't sort an infinite list. It is easy to see this because you can't
even decide what the first element of the ordered list should be, without
checking all the list elements. Since they are infinite, you can't do that.
Best,
Ut
Il mar 4 feb 2020, 15:00 Alexander Chen ha scritto:
> Hi,
>
> thank you for the explanation. I know now how not to use it but for deeper
> understanding.
>
> question:
> Can a lazy list be ordered or is that just not possible? And how can you
> create a lazy list like this [1..] without there being any implicit order
> in it?
>
> I don't want to be nitt-picking, i am trying to reconcile this paradox for
> deeper understanding.
>
> thank you.
>
> best,
>
>
> February 4, 2020 2:44:27 PM CET David McBride wrote:
>
> You can absolutely use a lazy list in a list comprehension. What you
> cannot do is evaluate an infinitely long list.
>
> When you evaluate it so that it will be printed you are saying, print the
> *entire* list. You may know that this list will never contain an element
> over 100, but as far as haskell is concerned, there could be an element
> after 100 and it will keep crunching to find it, by incrementing x and
> testing it against x <= 100 forever, even though it will never succeed.
>
> On Mon, Feb 3, 2020 at 3:14 AM Alexander Chen
> wrote:
>
>
> hi,
>
> I need some understanding on the following behavior:
>
> a = [1..]
> [x | x <-a, x <= 100]
>
> [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
>
> (note the missing ']' which results in the command not finishing)
>
> b = take 1000 a
> [x | x <-b, x <= 100]
>
> [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]
>
> Note, I took more for b just for ease sake. But now the command finishes
> correctly.
>
>
> Question:
> why can i not use a lazy list directly in a list comprehension?
>
> best,
>
>
>
>
> _______________________________________________
> 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 daniel.trstenjak at gmail.com Wed Feb 5 14:35:33 2020
From: daniel.trstenjak at gmail.com (Daniel Trstenjak)
Date: Wed, 5 Feb 2020 15:35:33 +0100
Subject: [Haskell-beginners] Need some understanding on this behavior.
In-Reply-To: <1102090692.858814.1580824811789@ichabod.co-bxl>
References: <1418257784.758267.1580717664665@ichabod.co-bxl>
<1102090692.858814.1580824811789@ichabod.co-bxl>
Message-ID: <20200205143533.GA7274@octa>
Hi Alexander,
> And how can you create a lazy list like this [1..] without there being
> any implicit order in it?
to be able to know that the list [1..] is sorted, you would have to
encode this information into the list type and very user of the
list - like the list comprehension - would have to check this information.
At the end sorting just isn't a property of the list type.
Greetings,
Daniel
From revollat at gmail.com Mon Feb 10 09:56:40 2020
From: revollat at gmail.com (Olivier Revollat)
Date: Mon, 10 Feb 2020 10:56:40 +0100
Subject: [Haskell-beginners] Hello (First message on the mailing list)
Message-ID:
Hi everybody,
it's my first message on this ML :)
I don't know if it's appropriate to post this here but I would like to have
some feedback with one of my first Haskell code.
I've been inspired by a recent Numberphile video (
https://www.youtube.com/watch?v=HJ_PP5rqLg0) how explain the "Russian
Peasant" algorithm to do multiplication (here in a nutshell :
https://www.wikihow.com/Multiply-Using-the-Russian-Peasant-Method)
So I decided I give it a go in Haskell, here is my solution, I appreciate
if you give me some feedback on how to improve this code (make it more
"idiomatic Haskell")
NB : I apologize if it's not the right place to ask this kind of review ...
in that case, where can I post this ?
Thanks !
module DivRusse where
main :: IO ()
main = do
putStrLn "13 x 12 is"
print $ russmul 13 12
russmul :: Int -> Int -> Int
russmul a b =
let filteredPair = filter (\pair -> (fst pair) `mod` 2 /= 0 ) $ (a,b) :
russmulList a b
in foldr (\pair acc -> snd pair + acc) 0 filteredPair
russmulList :: Int -> Int -> [(Int, Int)]
russmulList 1 _ = []
russmulList a b =
let a' = a `div` 2
b' = b * 2
in (a', b') : russmulList a' b'
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From fa-ml at ariis.it Mon Feb 10 11:53:14 2020
From: fa-ml at ariis.it (Francesco Ariis)
Date: Mon, 10 Feb 2020 12:53:14 +0100
Subject: [Haskell-beginners] Hello (First message on the mailing list)
In-Reply-To:
References:
Message-ID: <20200210115314.GB1097@aspire.station>
Hello Olivier,
On Mon, Feb 10, 2020 at 10:56:40AM +0100, Olivier Revollat wrote:
> I don't know if it's appropriate to post this here but I would like to have
> some feedback with one of my first Haskell code.
It is an appropriate post in the appropriate list!
> So I decided I give it a go in Haskell, here is my solution, I appreciate
> if you give me some feedback on how to improve this code (make it more
> "idiomatic Haskell")
Ok, the problems I see with russmulList are:
> russmulList :: Int -> Int -> [(Int, Int)]
> russmulList 1 _ = []
> russmulList a b =
> let a' = a `div` 2
> b' = b * 2
> in (a', b') : russmulList a' b'
- russmulList does not handle 0 gracefully (try `russmulList 0 10`)
- russmulList should _not_ discard the factors from the top of the list
(or you have to awkwardly re-add them as you did in filteredPair)
This or similar will do:
russmulList :: Int -> Int -> [(Int, Int)]
russmulList 0 b = []
russmulList a b =
let a' = a `div` 2
b' = b * 2
in (a, b) : russmulList a' b'
Now let's go through `russmul`:
> russmul :: Int -> Int -> Int
> russmul a b =
> let filteredPair = filter (\pair -> (fst pair) `mod` 2 /= 0 ) $ (a,b) :
> russmulList a b
> in foldr (\pair acc -> snd pair + acc) 0 filteredPair
- `(a,b) :` is needed no more
- in filteredPair you can drop the parentheses around `fst pair`
- use `odd` instead of "`mod` 2 /= 0`"
- in any case you should express the predicate in point-free style as
`even . fst`
- `foldr` part can be made much clearer with sum (map snd ...)
So:
russmul :: Int -> Int -> Int
russmul a b =
let filteredPair = filter (odd . fst) (russmulList a b)
in sum (map snd filteredPair)
Was this clear/useful? If not, fire again and welcome to the functional
world!
-F
From revollat at gmail.com Mon Feb 10 11:58:43 2020
From: revollat at gmail.com (Olivier Revollat)
Date: Mon, 10 Feb 2020 12:58:43 +0100
Subject: [Haskell-beginners] Hello (First message on the mailing list)
In-Reply-To: <20200210115314.GB1097@aspire.station>
References:
<20200210115314.GB1097@aspire.station>
Message-ID:
Hello Francesco,
This is very clear. Thanks for your help !
Your version is much more readable and elegant !
Le lun. 10 févr. 2020 à 12:53, Francesco Ariis a écrit :
> Hello Olivier,
>
> On Mon, Feb 10, 2020 at 10:56:40AM +0100, Olivier Revollat wrote:
> > I don't know if it's appropriate to post this here but I would like to
> have
> > some feedback with one of my first Haskell code.
>
> It is an appropriate post in the appropriate list!
>
> > So I decided I give it a go in Haskell, here is my solution, I appreciate
> > if you give me some feedback on how to improve this code (make it more
> > "idiomatic Haskell")
>
> Ok, the problems I see with russmulList are:
>
> > russmulList :: Int -> Int -> [(Int, Int)]
> > russmulList 1 _ = []
> > russmulList a b =
> > let a' = a `div` 2
> > b' = b * 2
> > in (a', b') : russmulList a' b'
>
> - russmulList does not handle 0 gracefully (try `russmulList 0 10`)
> - russmulList should _not_ discard the factors from the top of the list
> (or you have to awkwardly re-add them as you did in filteredPair)
>
> This or similar will do:
>
> russmulList :: Int -> Int -> [(Int, Int)]
> russmulList 0 b = []
> russmulList a b =
> let a' = a `div` 2
> b' = b * 2
> in (a, b) : russmulList a' b'
>
>
> Now let's go through `russmul`:
>
> > russmul :: Int -> Int -> Int
> > russmul a b =
> > let filteredPair = filter (\pair -> (fst pair) `mod` 2 /= 0 ) $ (a,b) :
> > russmulList a b
> > in foldr (\pair acc -> snd pair + acc) 0 filteredPair
>
> - `(a,b) :` is needed no more
> - in filteredPair you can drop the parentheses around `fst pair`
> - use `odd` instead of "`mod` 2 /= 0`"
> - in any case you should express the predicate in point-free style as
> `even . fst`
> - `foldr` part can be made much clearer with sum (map snd ...)
>
> So:
>
> russmul :: Int -> Int -> Int
> russmul a b =
> let filteredPair = filter (odd . fst) (russmulList a b)
> in sum (map snd filteredPair)
>
>
> Was this clear/useful? If not, fire again and welcome to the functional
> world!
> -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 maydwell at gmail.com Thu Feb 13 04:25:21 2020
From: maydwell at gmail.com (Lyndon Maydwell)
Date: Thu, 13 Feb 2020 15:25:21 +1100
Subject: [Haskell-beginners] Hello (First message on the mailing list)
In-Reply-To:
References:
Message-ID:
Hi Olivier,
I had a quick look, I think it's a great first try. Here are my thoughts!
module DivRusse where
{-
Comments:
* This seems unsafe in that it doesn't handle negative numbers well.
* This can be evidenced by needing a guard on our property.
* Could be addressed by using a safe newtype.
* Define properties and use quickcheck to test them.
* Favor pattern-matching over use of `fst`, `snd`.
* Use `where` over `let` to highlight what the final result is.
* Rewrite folds to more wholemeal approach. e.g. `sum $ map snd
filteredPair`
* Use standard functions and composition to eliminate lambdas like this:
`(\(x, _) -> x `mod` 2 /= 0 )` = `(odd . fst)`.
* `russmulList` could go into an infinite loop for negative numbers.
Either prevent this with types (preferred), or return an error somehow.
-}
main :: IO ()
main = do
putStrLn "13 x 12 is"
print $ russmul 13 12
-- Property: Does russmul = *?
prop_russmul :: Int -> Int -> Bool
prop_russmul a b
| a > 0 && b > 0 = russmul a b == a * b
| otherwise = True
russmul :: Int -> Int -> Int
russmul a b = sum $ map snd filteredPair
where
filteredPair = filter (odd . fst) $ (a,b) : russmulList a b
russmulList :: Int -> Int -> [(Int, Int)]
russmulList 1 _ = []
russmulList a b = (a', b') : russmulList a' b'
where
a' = a `div` 2
b' = b * 2
Warm Regards,
- Lyndon
On Mon, Feb 10, 2020 at 8:55 PM Olivier Revollat wrote:
> Hi everybody,
> it's my first message on this ML :)
>
> I don't know if it's appropriate to post this here but I would like to
> have some feedback with one of my first Haskell code.
> I've been inspired by a recent Numberphile video (
> https://www.youtube.com/watch?v=HJ_PP5rqLg0) how explain the "Russian
> Peasant" algorithm to do multiplication (here in a nutshell :
> https://www.wikihow.com/Multiply-Using-the-Russian-Peasant-Method)
>
> So I decided I give it a go in Haskell, here is my solution, I appreciate
> if you give me some feedback on how to improve this code (make it more
> "idiomatic Haskell")
>
> NB : I apologize if it's not the right place to ask this kind of review
> ... in that case, where can I post this ?
>
> Thanks !
>
> module DivRusse where
>
> main :: IO ()
> main = do
> putStrLn "13 x 12 is"
> print $ russmul 13 12
>
> russmul :: Int -> Int -> Int
> russmul a b =
> let filteredPair = filter (\pair -> (fst pair) `mod` 2 /= 0 ) $ (a,b) :
> russmulList a b
> in foldr (\pair acc -> snd pair + acc) 0 filteredPair
>
>
> russmulList :: Int -> Int -> [(Int, Int)]
> russmulList 1 _ = []
> russmulList a b =
> let a' = a `div` 2
> b' = b * 2
> in (a', b') : russmulList a' b'
>
> _______________________________________________
> 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 mianorsi at ciencias.unam.mx Fri Feb 14 08:21:06 2020
From: mianorsi at ciencias.unam.mx (=?UTF-8?Q?Miguel_Angel_Ordo=C3=B1ez_Silis?=)
Date: Fri, 14 Feb 2020 02:21:06 -0600
Subject: [Haskell-beginners] Memory usage of if-then-else vs guards
Message-ID:
Hí everybody,
Earlier today I was testing some code in ghci with ":set +s" enabled. For
some reason I changed an if-then-else expressions to guards and I was
surprised to find out that the memory usage declined significantly (around
20%).
Here is a function call output using if-then-else:
λ> inarow1 ls
1
(1.28 secs, 401,690,400 bytes)
And here is a function call output using guards:
λ> inarow1 ls
1
(1.18 secs, 313,690,576 bytes)
I ran both versions many times and this difference was consistent. Could
you help me understand why?
Thanks
Miguel
P.S. I don't think it should matter but maybe it does, here is the function
definition:
inarow1 :: forall a. Eq a => [a] -> Int
inarow1 [] = 0
inarow1 (x:[]) = 1
inarow1 ls = aux 0 1 ls where
aux :: Int -> Int -> [a] -> Int
aux top curr (x:y:[]) = max top $ if x == y then curr + 1 else curr
aux top curr (x:xs@(y:ys))
| x == y = aux top (curr + 1) xs
| otherwise = aux (max top curr) 1 xs
And the list:
ls = [1..1000000]
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From revollat at gmail.com Fri Feb 14 08:37:53 2020
From: revollat at gmail.com (Olivier Revollat)
Date: Fri, 14 Feb 2020 09:37:53 +0100
Subject: [Haskell-beginners] Hello (First message on the mailing list)
In-Reply-To:
References:
Message-ID:
Thank you Lyndon,
Your comments will help me a lot !
I'm glad the haskell community is kind with beginner !
Le jeu. 13 févr. 2020 à 05:25, Lyndon Maydwell a
écrit :
> Hi Olivier,
>
> I had a quick look, I think it's a great first try. Here are my thoughts!
>
>
>
> module DivRusse where
>
> {-
> Comments:
>
> * This seems unsafe in that it doesn't handle negative numbers well.
> * This can be evidenced by needing a guard on our property.
> * Could be addressed by using a safe newtype.
> * Define properties and use quickcheck to test them.
> * Favor pattern-matching over use of `fst`, `snd`.
> * Use `where` over `let` to highlight what the final result is.
> * Rewrite folds to more wholemeal approach. e.g. `sum $ map snd
> filteredPair`
> * Use standard functions and composition to eliminate lambdas like this:
> `(\(x, _) -> x `mod` 2 /= 0 )` = `(odd . fst)`.
> * `russmulList` could go into an infinite loop for negative numbers.
> Either prevent this with types (preferred), or return an error somehow.
>
> -}
>
> main :: IO ()
> main = do
> putStrLn "13 x 12 is"
> print $ russmul 13 12
>
> -- Property: Does russmul = *?
> prop_russmul :: Int -> Int -> Bool
> prop_russmul a b
> | a > 0 && b > 0 = russmul a b == a * b
> | otherwise = True
>
> russmul :: Int -> Int -> Int
> russmul a b = sum $ map snd filteredPair
> where
> filteredPair = filter (odd . fst) $ (a,b) : russmulList a b
>
> russmulList :: Int -> Int -> [(Int, Int)]
> russmulList 1 _ = []
> russmulList a b = (a', b') : russmulList a' b'
> where
> a' = a `div` 2
> b' = b * 2
>
>
>
>
> Warm Regards,
>
> - Lyndon
>
> On Mon, Feb 10, 2020 at 8:55 PM Olivier Revollat
> wrote:
>
>> Hi everybody,
>> it's my first message on this ML :)
>>
>> I don't know if it's appropriate to post this here but I would like to
>> have some feedback with one of my first Haskell code.
>> I've been inspired by a recent Numberphile video (
>> https://www.youtube.com/watch?v=HJ_PP5rqLg0) how explain the "Russian
>> Peasant" algorithm to do multiplication (here in a nutshell :
>> https://www.wikihow.com/Multiply-Using-the-Russian-Peasant-Method)
>>
>> So I decided I give it a go in Haskell, here is my solution, I appreciate
>> if you give me some feedback on how to improve this code (make it more
>> "idiomatic Haskell")
>>
>> NB : I apologize if it's not the right place to ask this kind of review
>> ... in that case, where can I post this ?
>>
>> Thanks !
>>
>> module DivRusse where
>>
>> main :: IO ()
>> main = do
>> putStrLn "13 x 12 is"
>> print $ russmul 13 12
>>
>> russmul :: Int -> Int -> Int
>> russmul a b =
>> let filteredPair = filter (\pair -> (fst pair) `mod` 2 /= 0 ) $ (a,b) :
>> russmulList a b
>> in foldr (\pair acc -> snd pair + acc) 0 filteredPair
>>
>>
>> russmulList :: Int -> Int -> [(Int, Int)]
>> russmulList 1 _ = []
>> russmulList a b =
>> let a' = a `div` 2
>> b' = b * 2
>> in (a', b') : russmulList a' b'
>>
>> _______________________________________________
>> 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 revollat at gmail.com Fri Feb 14 08:47:31 2020
From: revollat at gmail.com (Olivier Revollat)
Date: Fri, 14 Feb 2020 09:47:31 +0100
Subject: [Haskell-beginners] 2D drawing library for Beginner
Message-ID:
HI,
I would like to experiment with 2D graphics with Haskell (to do for example
generative art, ...)
I found Diagrams library which seem very powerful but I find it a bit
overwhelming for beginner, so do you have anything I can start with ? maybe
even a game engine ....
Thanks ;)
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From hawu.bnu at gmail.com Fri Feb 14 12:12:31 2020
From: hawu.bnu at gmail.com (Jean Lopes)
Date: Fri, 14 Feb 2020 09:12:31 -0300
Subject: [Haskell-beginners] 2D drawing library for Beginner
In-Reply-To:
References:
Message-ID:
Hi,
Not sure what you have tried already, but take a look at gloss. It does a
lot more than 2d drawing, but the API is quite nice
On Fri, Feb 14, 2020, 05:48 Olivier Revollat wrote:
> HI,
>
> I would like to experiment with 2D graphics with Haskell (to do for
> example generative art, ...)
> I found Diagrams library which seem very powerful but I find it a bit
> overwhelming for beginner, so do you have anything I can start with ? maybe
> even a game engine ....
>
> Thanks ;)
>
> _______________________________________________
> 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 revollat at gmail.com Fri Feb 14 14:55:36 2020
From: revollat at gmail.com (Olivier Revollat)
Date: Fri, 14 Feb 2020 15:55:36 +0100
Subject: [Haskell-beginners] 2D drawing library for Beginner
In-Reply-To:
References:
Message-ID:
OK I will try gloss. Thanks
Le ven. 14 févr. 2020 à 13:13, Jean Lopes a écrit :
> Hi,
> Not sure what you have tried already, but take a look at gloss. It does a
> lot more than 2d drawing, but the API is quite nice
>
> On Fri, Feb 14, 2020, 05:48 Olivier Revollat wrote:
>
>> HI,
>>
>> I would like to experiment with 2D graphics with Haskell (to do for
>> example generative art, ...)
>> I found Diagrams library which seem very powerful but I find it a bit
>> overwhelming for beginner, so do you have anything I can start with ? maybe
>> even a game engine ....
>>
>> Thanks ;)
>>
>> _______________________________________________
>> 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 francisco.garau at gmail.com Wed Feb 26 08:36:05 2020
From: francisco.garau at gmail.com (Francisco Garau)
Date: Wed, 26 Feb 2020 08:36:05 +0000
Subject: [Haskell-beginners] Prelude source code
Message-ID:
I want to see the source code of the Prelude. Is that available in the
standard distribution? Any hints in which directory should I be looking?
I've installed stack on OSX 10.11 with the below command
curl -sSL https://get.haskellstack.org/ | sh
Thanks,
Francisco
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From utprimum at gmail.com Wed Feb 26 08:48:55 2020
From: utprimum at gmail.com (Ut Primum)
Date: Wed, 26 Feb 2020 09:48:55 +0100
Subject: [Haskell-beginners] Prelude source code
In-Reply-To:
References:
Message-ID:
If you only want to see the code, you can look here
https://hackage.haskell.org/package/base-4.12.0.0/docs/Prelude.html
Cheers,
Ut
Il mer 26 feb 2020, 09:37 Francisco Garau ha
scritto:
> I want to see the source code of the Prelude. Is that available in the
> standard distribution? Any hints in which directory should I be looking?
>
> I've installed stack on OSX 10.11 with the below command
>
> curl -sSL https://get.haskellstack.org/ | sh
>
> Thanks,
> Francisco
>
> _______________________________________________
> 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 a.vigneron at protonmail.com Wed Feb 26 08:52:15 2020
From: a.vigneron at protonmail.com (A. Vigneron)
Date: Wed, 26 Feb 2020 08:52:15 +0000
Subject: [Haskell-beginners] Prelude source code
In-Reply-To:
References:
Message-ID:
Also Hoogle is a good resource, here is the link to the[Prelude module](https://hackage.haskell.org/package/base-4.12.0.0/docs/Prelude.html).
From there you can access the functions' source.
Cheers,
Alex
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From francisco.garau at gmail.com Wed Feb 26 09:56:32 2020
From: francisco.garau at gmail.com (Francisco Garau)
Date: Wed, 26 Feb 2020 09:56:32 +0000
Subject: [Haskell-beginners] Prelude source code
In-Reply-To:
References:
Message-ID: <204D4737-3FC9-40A7-9F2E-06B1D6A590A8@gmail.com>
Thank you. I’ve also found other references online, but I want to see the one installed locally in my system.
I come from Smalltalk where everything is accessible and modifiable.
Is the Prelude and other base modules distributed in compiled form only?
Hack age and hoogle are great but it is not clear which of the results is giving me are immediately available or if I have to install and import a package
- Francisco
> On 26 Feb 2020, at 08:52, A. Vigneron wrote:
>
> Also Hoogle is a good resource, here is the link to the Prelude module.
> From there you can access the functions' source.
>
> Cheers,
> Alex
> _______________________________________________
> 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 a.vigneron at protonmail.com Wed Feb 26 10:11:52 2020
From: a.vigneron at protonmail.com (A. Vigneron)
Date: Wed, 26 Feb 2020 10:11:52 +0000
Subject: [Haskell-beginners] Prelude source code
In-Reply-To: <204D4737-3FC9-40A7-9F2E-06B1D6A590A8@gmail.com>
References:
<204D4737-3FC9-40A7-9F2E-06B1D6A590A8@gmail.com>
Message-ID: <5LySif98j9NbHL5N2ZKD4fWpHHPHHz_RYDZtctovX6ONppaDb_fO5IpN3MCIH4Qt07d_NLDvd__49o-GrhCMomYnCmA9g2hYHbrTuGmPaxQ=@protonmail.com>
For locally available I'm not sure how to do it but I know it exists, it was mentioned in one of the videos I watched, I think it's [this one](https://www.youtube.com/watch?v=QpDQhGYPqkU&list=PLxj9UAX4Em-Ij4TKwKvo-SLp-Zbv-hB4B&index=3) but I'm not sure.
As for knowing whether it's included in the prelude or not, I usually check the address bar on Hoogle or the header.
But there are probably more efficient ways to deal with this.
Goodluck!
Cheers,
Alex
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From simon.jakobi at googlemail.com Wed Feb 26 10:57:19 2020
From: simon.jakobi at googlemail.com (Simon Jakobi)
Date: Wed, 26 Feb 2020 11:57:19 +0100
Subject: [Haskell-beginners] Prelude source code
In-Reply-To: <204D4737-3FC9-40A7-9F2E-06B1D6A590A8@gmail.com>
References:
<204D4737-3FC9-40A7-9F2E-06B1D6A590A8@gmail.com>
Message-ID:
You can find the official releases including sources at
http://downloads.haskell.org/~ghc/.
Am Mi., 26. Feb. 2020 um 10:57 Uhr schrieb Francisco Garau
:
>
> Thank you. I’ve also found other references online, but I want to see the one installed locally in my system.
>
> I come from Smalltalk where everything is accessible and modifiable.
>
> Is the Prelude and other base modules distributed in compiled form only?
>
> Hack age and hoogle are great but it is not clear which of the results is giving me are immediately available or if I have to install and import a package
>
> - Francisco
>
>
> On 26 Feb 2020, at 08:52, A. Vigneron wrote:
>
> Also Hoogle is a good resource, here is the link to the Prelude module.
> From there you can access the functions' source.
>
> Cheers,
> Alex
>
> _______________________________________________
> 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
From toad3k at gmail.com Wed Feb 26 12:53:04 2020
From: toad3k at gmail.com (David McBride)
Date: Wed, 26 Feb 2020 07:53:04 -0500
Subject: [Haskell-beginners] Prelude source code
In-Reply-To: <204D4737-3FC9-40A7-9F2E-06B1D6A590A8@gmail.com>
References:
<204D4737-3FC9-40A7-9F2E-06B1D6A590A8@gmail.com>
Message-ID:
Most libraries end up on your system when you use them. You can find them
if you installed via stack, by just typing find ~/.stack -name '*tar*'
However base is special. It is distributed with ghc and if you were to
build ghc from source it would take hours, so it and its primary libraries
are distributed in already compiled form, so you'll have to look at the ghc
source code to see those libraries.
On Wed, Feb 26, 2020 at 4:57 AM Francisco Garau
wrote:
> Thank you. I’ve also found other references online, but I want to see the
> one installed locally in my system.
>
> I come from Smalltalk where everything is accessible and modifiable.
>
> Is the Prelude and other base modules distributed in compiled form only?
>
> Hack age and hoogle are great but it is not clear which of the results is
> giving me are immediately available or if I have to install and import a
> package
>
> - Francisco
>
>
> On 26 Feb 2020, at 08:52, A. Vigneron wrote:
>
> Also Hoogle is a good resource, here is the link to the Prelude module
> .
> From there you can access the functions' source.
>
> Cheers,
> Alex
>
> _______________________________________________
> 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 doug at cs.dartmouth.edu Thu Feb 27 23:53:19 2020
From: doug at cs.dartmouth.edu (Doug McIlroy)
Date: Thu, 27 Feb 2020 18:53:19 -0500
Subject: [Haskell-beginners] Prelude source code
Message-ID: <202002272353.01RNrJK4030561@tahoe.cs.Dartmouth.EDU>
> Thank you. Ive also found other references online, but I want to see the
> one installed locally in my system.
>
> I come from Smalltalk where everything is accessible and modifiable.
>
> Is the Prelude and other base modules distributed in compiled form only?
One of the joys of Hugs is that it includes the H98 Prelude,
a real vade mecum for both beginners and experts. It is one
of the reasons that for everyday use I resort more often to
Hugs than to ghci.
The modern Prelude, spread over dozens of files, and programmed
at a higher level of abstraction, will not be as useful to a
beginner as the H98 Prelude is. Nevertheless it's a valuable
and fundamental resource. And it's real code, snatches of which
one may borrow as a basis for special modification. That is
not true of stuff served up by Hoogle.
Quite properly, the ghc distribution contains a compiled
version of the Prelude. But that doesn't preclude including
the source. It would be a public service to do so.
["You can always get it with Cabal" is unreasonably
dismissive. Why should beginners have to master another arcane
subject just to get a handbook for their newest toy?]
Doug McIlroy