Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: How do I use Guards in record syntax? (Tom Murphy)
2. Re: How do I use Guards in record syntax? (Gesh)
3. Re: what is "Gesh"? (Re: function not working as expected,
type issues) (John M. Dlugosz)
4. Re: recursive 'let' ? (John M. Dlugosz)
5. Re: general structuring of "foreach" logic in IO
(John M. Dlugosz)
6. Re: general structuring of "foreach" logic in IO
(Brandon Allbery)
7. Re: set multiplication (Jacek Dudek)
8. Re: How do I use Guards in record syntax? (Dimitri DeFigueiredo)
----------------------------------------------------------------------
Message: 1
Date: Mon, 14 Apr 2014 05:22:41 -0700
From: Tom Murphy <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] How do I use Guards in record syntax?
Message-ID:
<CAO9Q0tWTkFd2y4CA-t0ohQ1-UG86DNniFQpC=wemah0b3tt...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
The closest thing to guards that'll work is multi-way-if (you'll have to
put "{-# LANGUAGE MultiWayIf #-}" at the top of your source file)
e.g.
buysell = if | oldbuysell ot == "buy" = Buy
| oldbuysell ot == "sell" = Sell
| otherwise = Unknown
but a 'case' is slightly shorter here:
buysell = case oldbuysell ot of
"buy" -> Buy
"sell" -> Sell
_ -> Unknown
Tom
On Mon, Apr 14, 2014 at 4:22 AM, Kim-Ee Yeoh <[email protected]> wrote:
>
> On Mon, Apr 14, 2014 at 1:03 PM, Dimitri DeFigueiredo <
> [email protected]> wrote:
>
>> oldbuysell :: String -- "buy", "sell" or ""
>
>
> Why is this String when you've defined the perfectly cromulent BuyOrSell
> datatype?
>
> How can you be sure there won't be some accidental buggy code that sets
> the field to "buyy"?
>
> -- Kim-Ee
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140414/5401042e/attachment-0001.html>
------------------------------
Message: 2
Date: Mon, 14 Apr 2014 18:04:50 +0300
From: Gesh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] How do I use Guards in record syntax?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On April 14, 2014 9:03:20 AM GMT+03:00, Dimitri DeFigueiredo
<[email protected]> wrote:
>
>I'm having some trouble understanding where I can or cannot use guards
>inside record syntax. I'm writing a simple conversion routine, but I am
>
>not able to write it without inserting an extra let. Do I need a let
>expression here? Am I missing something?
>
>--------------
>data OldTrade = OldTrade {
> oldprice :: Double ,
> oldamount :: Double ,
> oldbuysell :: String -- "buy", "sell" or ""
> } deriving( Eq, Show)
>
>
>data BuyOrSell = Buy | Sell | Unknown deriving(Eq, Show)
>
>data Trade = Trade {
> price :: Double ,
> amount :: Double ,
> buysell :: BuyOrSell
> } deriving( Eq, Show)
>
>convert :: OldTrade -> Trade
>
>convert ot = Trade { price = oldprice ot,
> amount = oldamount ot,
> buysell = let x | oldbuysell ot == "buy" = Buy
> | oldbuysell ot == "sell" = Sell
> | otherwise = Unknown
> in x
> }
>
>-- how do I eliminate the 'let' expression here?
>-- I wanted to write something like:
>--
>-- buysell | oldbuysell ot == "buy" = Buy
>-- | oldbuysell ot == "sell" = Sell
>-- | otherwise = Unknown
>
>--------------
>
>Thanks!
>
>Dimitri
>
>
>
>
>_______________________________________________
>Beginners mailing list
>[email protected]
>http://www.haskell.org/mailman/listinfo/beginners
Note that this has nothing to do with record syntax specifically. Rather, what
you're asking is how to write a multi-way if expression. Your way is to
introduce a local binding using a let statement, which allows you to use
pattern guards as you did.
Usually, bowever, you'd use a case statement to avoid the binding. However, you
could use the MultiWayIf LANGUAGE pragma, as suggested elsewhere in this
thread. Or you could float out the binding to a where clause, except that
doesn't seem to be what you're looking for.
Hoping to help,
Gesh
------------------------------
Message: 3
Date: Mon, 14 Apr 2014 10:18:56 -0500
From: "John M. Dlugosz" <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] what is "Gesh"? (Re: function not
working as expected, type issues)
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
On 4/13/2014 8:55 AM, Gesh wrote:
> Sorry, my autocorrect must have put that in instead of "the". And it's the
> nickname I use, not jargon.
> Sorry.
>
Sorry to bother you.
------------------------------
Message: 4
Date: Mon, 14 Apr 2014 10:24:36 -0500
From: "John M. Dlugosz" <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] recursive 'let' ?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
I don't mind recursive *functions*. It's recursive definition of single
computed values
that looked odd to me. Lazy evaluation makes all the difference. Looking at
b= f a c
c = f a b
I was thinking, "how can it figure out what b and c need to be?" because I'm
used to this
meaning that it needs to come up with an actual value right now.
------------------------------
Message: 5
Date: Mon, 14 Apr 2014 10:41:20 -0500
From: "John M. Dlugosz" <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] general structuring of "foreach"
logic in IO
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252; format=flowed
On 4/14/2014 4:34 AM, Patrick Wheeler wrote:
> @John
> That way you can write code that looks like:
>
> main = do
> myargs <- getArgs
> forM_ myargs $ \s -> do
> putStrLn s
> putStrLn $ "Second string" ++ s
That still introduces a throw-away name and extracts the args on another line.
Is there a
way to do something like:
main = do
forM_ (dereference getArgs) $ \s -> do ...
or
mforM_ getArgs $ \s -> do ...
> You can read the discussion around om:
> http://www.reddit.com/r/haskell/comments/1i2zmq/a_useful_function_om/
>
Thanks, I will.
I guess it bothers me that "wrapped" values in the IO monad can't be
independently used in
situ, but need to be given a name on another line first. Isn't the likes of
"lift" and
(even nicer) <*> supposed to address this idea? I can't quite see why that
doesn't work here.
(And that begs the question of why getArgs needs to be monadic in the first
place. It
doesn't change its value; it's a strict constant at run-time, and not knowing
it at
compile time is my problem how?)
?John
------------------------------
Message: 6
Date: Mon, 14 Apr 2014 11:47:40 -0400
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] general structuring of "foreach"
logic in IO
Message-ID:
<CAKFCL4Wtg=ehOTyq=a6i09bdj73uebngctu_44g7hdcvx3u...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Mon, Apr 14, 2014 at 11:41 AM, John M. Dlugosz
<[email protected]>wrote:
> (And that begs the question of why getArgs needs to be monadic in the
> first place. It doesn't change its value; it's a strict constant at
> run-time, and not knowing it at compile time is my problem how?)
But it is the *compiler's* problem. "Constant" means "known at compile
time" to the compiler.
Although I wonder if you'd be happier with getArgs >>= mapM_ (do ...).
--
brandon s allbery kf8nh sine nomine associates
[email protected] [email protected]
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140414/52d31327/attachment-0001.html>
------------------------------
Message: 7
Date: Mon, 14 Apr 2014 13:53:40 -0400
From: Jacek Dudek <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] set multiplication
Message-ID:
<CAJxg2_FOd8T1SbZBR0zNCYA7zYstgWKwnSidnMJ=a79je8h...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
Well it looks I was beat to the answer by Mariano, but here's my
solution anyway. It's a little more expository, hope that helps. Note
that we both decided to use a right fold in our solutions.
{-
Define a binary operation. Note the type signature.
When used in the fold operation below, the 2nd argument
denotes the set product of the first couple of lists
(counting from the right-hand side) that were in your
initial list.
-}
f :: [a] -> [[a]] -> [[a]]
f [] _ = []
f (x : xs) yss = map (x :) yss ++ f xs yss
{-
Now for the product, we use a right fold operation to get
the product of the first two lists (counting from the right),
then distribute the elements of the next list over the result,
then the elements of the next list, and so on.
-}
setProd :: [[a]] -> [[a]]
setProd [] = []
setProd xss = foldr f (map (: []) (last xss)) (init xss)
test = [[1,2,3],[4,5],[6,7]]
On 4/8/14, Christian Maeder <[email protected]> wrote:
> Some time ago I discovered that the monadic "sequence" function operates
> quite interestingly on lists. I don't know why you call it
> multiplication. I was looking for something like combinations:
>
> Prelude> sequence [[1,2,3], [4,5], [6,7]]
>
> HTH Christian
>
> Am 08.04.2014 07:00, schrieb Nishant:
>>
>> hi,
>>
>> I am trying to implement a set multiplication program in haskell.
>>
>> Input : [1,2,3] [4,5] [6,7]
>> Ouput : [ [1,4,6] , [1,4,7] , [1,5,6] , [1,5,7] ...]
>>
>>
>> I implemented it for constant number of inputs like
>>
>> setMul xs ys zs = [ [x] ++ [y] ++ [z] | x <- xs , y<-ys ,z <- zs]
>>
>> I am not able to generalize this for any number of lists.
>>
>> type signature would be :
>>
>> setMulMany :: [[a]] -> [[a]]
>>
>> Example :
>>
>> Input : [ [1,2,3] , [4,5] , [6,7]]
>> Ouput : [ [1,4,6] , [1,4,7] , [1,5,6] , [1,5,7] ...]
>>
>>
>> Regards.
>> Nishant
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 8
Date: Mon, 14 Apr 2014 12:36:08 -0600
From: Dimitri DeFigueiredo <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] How do I use Guards in record syntax?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Thanks everyone :-)
I think the "case of" is what I was looking for. I keep thinking of
using "case of" as in pattern matching to find the "Shape" of the result
of an expression and of using guards to evaluate predicates. Forgetting
that True and False are constructors themselves. I just have to change
that mindset.
Cheers,
Dimitri
Em 14/04/14 09:04, Gesh escreveu:
> On April 14, 2014 9:03:20 AM GMT+03:00, Dimitri DeFigueiredo
> <[email protected]> wrote:
>> I'm having some trouble understanding where I can or cannot use guards
>> inside record syntax. I'm writing a simple conversion routine, but I am
>>
>> not able to write it without inserting an extra let. Do I need a let
>> expression here? Am I missing something?
>>
>> --------------
>> data OldTrade = OldTrade {
>> oldprice :: Double ,
>> oldamount :: Double ,
>> oldbuysell :: String -- "buy", "sell" or ""
>> } deriving( Eq, Show)
>>
>>
>> data BuyOrSell = Buy | Sell | Unknown deriving(Eq, Show)
>>
>> data Trade = Trade {
>> price :: Double ,
>> amount :: Double ,
>> buysell :: BuyOrSell
>> } deriving( Eq, Show)
>>
>> convert :: OldTrade -> Trade
>>
>> convert ot = Trade { price = oldprice ot,
>> amount = oldamount ot,
>> buysell = let x | oldbuysell ot == "buy" = Buy
>> | oldbuysell ot == "sell" = Sell
>> | otherwise = Unknown
>> in x
>> }
>>
>> -- how do I eliminate the 'let' expression here?
>> -- I wanted to write something like:
>> --
>> -- buysell | oldbuysell ot == "buy" = Buy
>> -- | oldbuysell ot == "sell" = Sell
>> -- | otherwise = Unknown
>>
>> --------------
>>
>> Thanks!
>>
>> Dimitri
>>
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
> Note that this has nothing to do with record syntax specifically. Rather,
> what you're asking is how to write a multi-way if expression. Your way is to
> introduce a local binding using a let statement, which allows you to use
> pattern guards as you did.
> Usually, bowever, you'd use a case statement to avoid the binding. However,
> you could use the MultiWayIf LANGUAGE pragma, as suggested elsewhere in this
> thread. Or you could float out the binding to a where clause, except that
> doesn't seem to be what you're looking for.
> Hoping to help,
> Gesh
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 70, Issue 30
*****************************************