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:  Pattern Matching & Binding (Emmanuel Surleau)
   2. Re:  Pattern Matching & Binding (David McBride)
   3.  How do I do inheritance in haskell? (Dimitri DeFigueiredo)
   4. Re:  How do I do inheritance in haskell? (Dan Serban)


----------------------------------------------------------------------

Message: 1
Date: Thu, 8 May 2014 08:13:23 +0200
From: Emmanuel Surleau <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Pattern Matching & Binding
Message-ID: <20140508061323.GD26863@emm-laptop>
Content-Type: text/plain; charset=us-ascii

On Tue, May 06, 2014 at 02:29:26PM -0400, Ari King wrote:
> >
> > I don't have pg installed so I can't run your code but I assume you are
> > breaking on the vector pattern matching.  Pattern matching using : only
> > works because the : operator is a constructor in lists.
> >
> > >:i (:)
> > data [] a = ... | a : [a]
> >
> > Remember that : is an infix operator, but it is comparable to Just, Left,
> > or Right.  You are trying to use a list constructor to pattern match on a
> > vector which looks nothing like a list.
> >
> > However you can do this sort of pattern matching by using vector's (or
> > almost any other collection's) toList function:
> >
> > hostaddr:port:dbname:username:password:rest = toList vec
> >
> >
> Thanks for the clarification; I was under the impression that vectors were
> essentially re-implemented lists.
> 
> 
> > <- is a monadic binding. You use it when you are dealing with a datatype
> > that happens to be an instance of Monad.  It happens to be the only way to
> > get anything out of an IO type, which is what you are using it for here.
> > If something is just
> >
> using plain types, Int, String, etc, just use lets.
> >
> >
> So, <- is more like extract (from IO type) and bind, but scoping wise is
> the same as let or where, correct?

It's more complicated than that. Haskell uses monads for IO, this is true, but
it is used in many other places. The first example of monads you're likely to
find is actually Maybe. For instance:

   example :: String -> Maybe String
   example = do
      -- if someFunctionReturningMaybeInt returns (Just anInt),
      -- binds 'anInt' to a, otherwise if None just exit the function and return
      -- None
      a <- someFunctionReturningMaybeInt
      b <- someFunctionTakingIntAndReturningMaybeString a
      -- if someFunctionTakingIntAndReturningMaybeString returns (Just str),
      -- binds 'str' to b, otherwise if None just exit the function and return
      -- None
      Just $ b ++ "_example"
      -- equivalent to: return $ b ++ "_example"

This brings me to the second point: monads are used for sequencing. Here, 'b' is
not available in the first line of the function, it hasn't been bound yet. On
the other hand, the 'let x = foo in'/'where x = foo' syntax makes the name
available in the scope of the function.
  
> Lastly, the code fails to read (see line 16 @ http://pastebin.com/R5MPNaHs)
> the provided file into a ByteString, ByteString readFile complains:
> 
> Failed reading: conversion error: expected Char, got "127.0.0.1"
> 
> The file contents are: 127.0.0.1,5432,sample,test_user,test_pwd
> 
> Am I misusing, ByteString's readFile? Thanks.
> 
> -Ari

> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners



------------------------------

Message: 2
Date: Thu, 8 May 2014 02:47:10 -0400
From: David McBride <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Pattern Matching & Binding
Message-ID:
        <can+tr40+v5zwcss5sst_scadi8vz_v7yl-_sx2l0ntvftde...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

The reason cassava is giving you an error is that you are telling it to
expect a Vector of Strings.  You happen to only have one, but a csv has
multiple lines.  So that should be a Vector of Vectors of Strings.

The reason is says char is that it is assuming what is actually in the file
is a Vector of a List of Chars which also fits the pattern, because String
is a list of chars.  Your original type would work with an input of

a,b,c
d,e,f

which is potentially valid, but for what you actually want, try this:

      csvData <- DBL.readFile $ head args
      case decode NoHeader csvData of
        Left err -> putStrLn err
        Right vec -> print (vec :: DV.Vector (DV.Vector String))



On Tue, May 6, 2014 at 2:29 PM, Ari King <[email protected]>wrote:

> I don't have pg installed so I can't run your code but I assume you are
>> breaking on the vector pattern matching.  Pattern matching using : only
>> works because the : operator is a constructor in lists.
>>
>> >:i (:)
>> data [] a = ... | a : [a]
>>
>> Remember that : is an infix operator, but it is comparable to Just, Left,
>> or Right.  You are trying to use a list constructor to pattern match on a
>> vector which looks nothing like a list.
>>
>> However you can do this sort of pattern matching by using vector's (or
>> almost any other collection's) toList function:
>>
>> hostaddr:port:dbname:username:password:rest = toList vec
>>
>>
> Thanks for the clarification; I was under the impression that vectors were
> essentially re-implemented lists.
>
>
>> <- is a monadic binding. You use it when you are dealing with a datatype
>> that happens to be an instance of Monad.  It happens to be the only way to
>> get anything out of an IO type, which is what you are using it for here.
>> If something is just
>>
> using plain types, Int, String, etc, just use lets.
>>
>>
> So, <- is more like extract (from IO type) and bind, but scoping wise is
> the same as let or where, correct?
>
> Lastly, the code fails to read (see line 16 @ http://pastebin.com/R5MPNaHs)
> the provided file into a ByteString, ByteString readFile complains:
>
> Failed reading: conversion error: expected Char, got "127.0.0.1"
>
> The file contents are: 127.0.0.1,5432,sample,test_user,test_pwd
>
> Am I misusing, ByteString's readFile? Thanks.
>
> -Ari
>
>
>
> _______________________________________________
> 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/20140508/d0372ce8/attachment-0001.html>

------------------------------

Message: 3
Date: Thu, 08 May 2014 02:26:27 -0600
From: Dimitri DeFigueiredo <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] How do I do inheritance in haskell?
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

 

Hi!

 I am trying to understand how to "factor out" common functionality in
haskell.
 (All the code is available here
https://github.com/defigueiredo/inheritance-in-haskell [1])

 I am receiving orderbook data from a Bitcoin Exchange, through their
JSON API
 (available at https://www.bitstamp.net/api/order_book/ [2])

 I have a generic JSON parser that parses the text response and gives me
back a generic 
 JSON object I called Jvalue. This data type represents any JSON object
and is defined as follows.

 (see json.org for full JSON specs)
 ------------------

 import qualified Data.Map as Map

 data Jvalue = Jobject (Map.Map String Jvalue)
 | Jarray [Jvalue] 
 | Jstring String
 | Jnumber Double
 | Jbool Bool
 | Jnull
 deriving Show
 -------------------

 Now, I have to convert this (if possible) into an OrderBook type. An
orderbook is basically two lists. One list of the orders placed by
people who want to buy, the "bids", and the other with the orders of
people who want to sell, the "asks". Each order specifies what price
should be paid and how many bitcoins to buy/sell (the volume).

 The functions for generating the lists of bids and asks are essentially
the same. However, they might need to know if they are creating bids or
asks. So, I may need to pass a parameter with that information. I can
make that parameter a function or a type (or I may not need it). These
three different ideas give rise to 3 different ways to define the
orderbook:

 1) We can make each Order a type with two constructors (my initial
idea):
 -------------------
 newtype Volume = Volume Double deriving (Show)
 newtype Price = Price Double deriving (Show)

 data Order = Bid {price::Price, volume::Volume} 
 | Ask {price::Price, volume::Volume}
 deriving (Show)

 data OrderBook = OrderBook{ bids::[Order] , asks::[Order] } deriving
(Show)
 -------------------

 2) We can make Bid and Ask types themselves
 -------------------
 newtype Volume = Volume Double deriving (Show)
 newtype Price = Price Double deriving (Show)

 data Order = Order {price::Price, volume::Volume} deriving (Show)

 newtype Bid = Bid Order deriving Show
 newtype Ask = Ask Order deriving Show

 data OrderBook = OrderBook{ bids::[Bid] , asks::[Ask] } deriving (Show)
 -------------------

 3) We can make Order a class 
 -------------------
 newtype Volume = Volume Double deriving (Show)
 newtype Price = Price Double deriving (Show)

 class Order a where
 makeOrder :: String -> String -> a

 data Ask = Ask {aprice::Price, avolume::Volume} deriving (Show)
 instance Order Ask where
 makeOrder = makeAsk

 data Bid = Bid {bprice::Price, bvolume::Volume} deriving (Show)
 instance Order Bid where
 makeOrder = makeBid

 data OrderBook = OrderBook{ bids::[Bid] , asks::[Ask] } deriving (Show)

 -- makeAsk and MakeBid defined later
 -------------------

 It is not clear to me which one of these should be used and, most
importantly, which criteria should be used to choose between them. I
wrote up the code to do the conversion of the Jvalue object into an
OrderBook in the 3 different ways. (The third one came out of a
discussion from a haskell meetup group.)

 (All the code is available here
https://github.com/defigueiredo/inheritance-in-haskell [1])

 Any insights on how to choose between them would be *greatly*
appreciated!

 Thanks,

 Dimitri

 

Links:
------
[1] https://github.com/defigueiredo/inheritance-in-haskell
[2] https://www.bitstamp.net/api/order_book/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140508/a39b1eb9/attachment-0001.html>

------------------------------

Message: 4
Date: Thu, 8 May 2014 11:58:28 +0300
From: Dan Serban <[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 do inheritance in haskell?
Message-ID:
        <cahapvseq8ivqv6xvyz5h5t-4whsyvkvcml-q5wrcgizor5w...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

>The functions for generating the lists of bids and asks are essentially the 
>same.

Create a typeclass umbrella for them.
Your option 3 looks good, but it doesn't go far enough.

Whenever you're presented with an opportunity to "harden" your design
by encoding problem domain invariants into the type system, you should
seize it - on principled grounds.

This goes back to one of the unique selling point of Haskell, which is
that by careful type design you can eliminate entire classes of bugs.

I tend to favor designs that look like this:

newtype AskVolume = AskVolume Double deriving (Show)
newtype AskPrice  = AskPrice  Double deriving (Show)

newtype BidVolume = BidVolume Double deriving (Show)
newtype BidPrice  = BidPrice  Double deriving (Show)

data AskOrder = AskOrder { aprice :: AskPrice, avolume :: AskVolume }
deriving (Show)
data BidOrder = BidOrder { bprice :: BidPrice, bvolume :: BidVolume }
deriving (Show)

data OrderBook = OrderBook { bids :: [BidOrder], asks :: [AskOrder] }
deriving (Show)

This is Haskell, so there are likely opportunities to move even more
stuff into the type system, but I'm not aware of them.

-Dan


------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 71, Issue 10
*****************************************

Reply via email to