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: Unit conversion (Tom Murphy)
2. emacs mode? (Rob Nikander)
3. Re: Lazy vs Strict ponderings... (Henk-Jan van Tuyl)
4. Re: emacs mode? (Mark Wright)
5. SQLite3 Row <-> Data models -- Is this a typical Haskell
pattern ? (Stef T)
----------------------------------------------------------------------
Message: 1
Date: Sat, 19 Mar 2011 13:50:54 -0400
From: Tom Murphy <[email protected]>
Subject: Re: [Haskell-beginners] Unit conversion
To: aditya siram <[email protected]>
Cc: Magnus Therning <[email protected]>, [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hi All,
Here's what I have so far:
I haven't tried it with monoids but I'm intrigued...
data Measure = M Volume Float
deriving (Show, Eq)
data Volume = Teaspoon
| Tablespoon
| FluidOunce
| Slice
deriving (Show, Eq, Ord, Bounded) -- Note
the Ord and Bounded typeclasses
stepDown :: Measure -> Measure --
stepDown - convert Measures to one unit smaller
stepDown (M Slice a) = M FluidOunce (a * 120)
stepDown (M FluidOunce a) = M Tablespoon (a * 2)
stepDown (M Tablespoon a) = M Teaspoon (a * 3)
stepUp (M Teaspoon a) = M Tablespoon (a / 3) -- stepUp -
convert Measures to one unit larger
stepUp (M Tablespoon a) = M FluidOunce (a / 2)
stepUp (M FluidOunce a) = M Slice (a / 120)
stepUp (M Slice a) = M Slice (a * 1)
convertDown :: Measure -> Volume -> Measure -- stepDown
multiple times, to a desired unit
convertDown (M unit measure) goalUnit
| unit == goalUnit = M unit measure
| otherwise = convertDown (stepDown (M unit
measure)) goalUnit
convertUp all@(M unit measure) --
stepUp to a unit which "makes sense" for the magnitude of the measure
| (measure > 25) && (unit /= (maxBound :: Volume)) = convertUp $
stepUp all
| otherwise = all
(<+>) :: Measure -> Measure -> Measure -- do the
addition
a@(M unitA measureA) <+> b@(M unitB measureB)
| unitA == unitB = M unitA (measureA + measureB)
| unitA > unitB = convertUp $ ((convertDown a unitB) <+> b)
| unitA < unitB = convertUp $ (a <+> (convertDown b unitA))
This is fully functional:
*Main> (M Slice 30) <+> (M Teaspoon 3)
M Slice 30.004168
*Main> (M FluidOunce 57) <+> (M Slice 0.2)
M Slice 0.675
*Main> stepDown it
M FluidOunce 81.0
etc.
In convertUp, "25" could be replaced by a multiple of the unit "above" if I
had a better data structure for keeping track of conversion amounts.
This is the major kludge that I see in my code right now. Having one stepUp
function and one stepDown function, each of which read off a conversion
list, or even some type of tree, would be more efficient.
Any resources, for solutions to this problem, would definitely be
appreciated.
Again, I'm really surprised that I can't find very much at all, on what
seems to be a common computing problem.
Thanks for your time.
Tom
On Sat, Mar 19, 2011 at 12:11 AM, aditya siram <[email protected]>wrote:
> How about something like this?
>
> import Data.Monoid
>
> data Volumes = Teaspoon | Tablespoon | Slice | FluidOunces
> type Quantity = (Int, Volumes)
>
> instance Monoid Quantity where
> mempty = (0, Teaspoon)
> mappend a b = ((toTeaspoon a) + (toTeaspoon b), Teaspoon)
>
> toTeaspoon :: Quantity -> Int
> toTeaspoon (a, Teaspoon) = a
> toTeaspoon (a, Tablespoon) = undefined
> toTeaspoon (a, Slice) = undefined
> toTeaspoon (a, FluidOunces) = undefined
>
> -deech
>
> On Fri, Mar 18, 2011 at 10:02 PM, Tom Murphy <[email protected]> wrote:
> > On Fri, Mar 18, 2011 at 5:02 AM, Magnus
> > Therning <[email protected]> wrote:
> >>
> >> On Thu, Mar 17, 2011 at 21:17, Tom Murphy <[email protected]> wrote:
> >> > Hi All,
> >> > Is there a good way to easily convert between units?
> >> > For example, let's say I have a data type:
> >> > data Volumes = Teaspoon | Tablespoon | Slice | FluidOunces
> >> > ... and I want to define an infix function '<+>', which adds
> >> > together
> >> > amounts of food:
> >> > (1, Slice, cake) <+> (1, Volume Tablespoon, sugar) <+> (32,
> Volume
> >> > FluidOunces, almondMilk)
> >> > Which would return:
> >> > (3200, Teaspoons)
> >> > What is the most efficient way to define equivalency/conversion
> >> > between
> >> > these measures?
> >> > I remember an interesting method for celsius-farenheit conversion
> >> > in
> >> > Higher-Order Perl, using function composition, but that was between
> only
> >> > 2
> >> > units. I'd like something where I don't have to provide n^2
> definitions.
> >>
> >> I wrote two blog posts on something that sounds related:
> >> http://therning.org/magnus/archives/354
> >> http://therning.org/magnus/archives/355
> >>
> >> Maybe they could help.
> >>
> >> /M
> >>
> >> --
> >> Magnus Therning OpenPGP: 0xAB4DFBA4
> >> email: [email protected] jabber: [email protected]
> >> twitter: magthe http://therning.org/magnus
> >
> >
> >
> >
> > So given
> > data Measure = M Volume Float
> > deriving (Show, Eq)
> > data Volume = Slice
> > | FluidOunce
> > | Tablespoon
> > | Teaspoon
> > deriving (Show, Eq)
> > Method one and two would be to convert all units to the smallest unit:
> > toTsp :: Measure -> Measure
> > [...]
> > toTsp (M Slice a) = M Teaspoon (a * 350)
> > toTsp (M FluidOunce a) = M Teaspoon (a * 34)
> > [...]
> > Perform the addition, then convert it to a more appropriate measure (back
> to
> > Cake Slices...).
> > It seems that there's a potential loss of precision, if I have to
> for
> > example convert Megaliter measures to Teaspoons.
> >
> > Method three involves using typeclasses to define common "meeting points"
> > for the units.
> > So, we say that if we're adding Slices and FluidOunces, both measures
> should
> > be converted to FluidOunces before the addition.
> > The problem there is that, given n measurement units, we have to declare
> n^2
> > typeclass instances by hand.
> >
> > Another way that I can see to do this is to define a "chain" of
> conversions.
> > I'd then "convert down" only to the smallest-common unit.
> > So I could define
> > (warning: extreme kludge ahead)
> > a series of "step-down"s and "step-up"s:
> > Given a largest-to-smallest ordering of Slice, FluidOunce, Tablespoon,
> > Teaspoon, I can define:
> > stepDown :: Measure -> Measure
> > stepDown (M Slice a) = M FluidOunce (a * 120)
> > stepDown (M FluidOunce a) = M Tablespoon (a * 2)
> > stepUp (M FluidOunce a) = M Slice (a / 120)
> > stepUp (M Tablespoon a) = M FluidOunce (a / 2)
> > [...]
> >
> > and then a function to "step" as far down as needed:
> > convertDown :: Measure -> Volume -> Measure
> > convertDown (M unit measure) goalUnit
> > | unit == goalUnit = M unit measure
> > | otherwise = fun5 (stepDown (M unit
> measure))
> > goalUnit
> >
> >
> > And then if I wanted to add a Slice of cake to a Tablespoon of almond
> milk,
> > I'd have to find the smaller unit, convert down, perform the math and
> then
> > do a "convert up" operation.
> > An added benefit to this method is that defining a unit smaller than the
> > current smallest wouldn't involve revising the "base unit" measures.
> > This seems like a pretty common C.S. problem; I'm surprised I haven't
> been
> > able to find more resources about it.
> > Thanks for your time!
> > Tom
> > P.S. Thank you, Magnus, for those resources!
> > _______________________________________________
> > 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/20110319/fe8dfa1b/attachment-0001.htm>
------------------------------
Message: 2
Date: Sat, 19 Mar 2011 14:36:17 -0400
From: Rob Nikander <[email protected]>
Subject: [Haskell-beginners] emacs mode?
To: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi all,
Where can I get emacs mode for Haskell? I tried the following page
but the links from it are hanging forever or dead.
http://www.haskell.org/haskellwiki/Haskell_mode_for_Emacs
Rob
------------------------------
Message: 3
Date: Sat, 19 Mar 2011 22:56:51 +0100
From: "Henk-Jan van Tuyl" <[email protected]>
Subject: Re: [Haskell-beginners] Lazy vs Strict ponderings...
To: [email protected], "Heinrich Apfelmus"
<[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-15; format=flowed;
delsp=yes
On Sat, 19 Mar 2011 12:50:18 +0100, Heinrich Apfelmus
<[email protected]> wrote:
> 2) Don't worry about memory leaks until they actually appear. If they
> do, use a profiling tool to find out what's going on. Most likely, one
> of the two things above happened in an interesting way.
It is likely that, if you develop a medium to large sized application for
a customer, memory leaks will appear at the customers site. It will often
be very difficult to reproduce the situation and find the leak. If you
want to produce quality software, it is best to be sure what you are doing
during the whole development process. There should be a set of guidelines,
on how to prevent space leaks.
See also "On the reliability of programs",
http://www.cs.utexas.edu/users/EWD/transcriptions/EWD03xx/EWD303.html
Regards,
Henk-Jan van Tuyl
--
http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
--
------------------------------
Message: 4
Date: Sun, 20 Mar 2011 09:34:47 +1100
From: Mark Wright <[email protected]>
Subject: Re: [Haskell-beginners] emacs mode?
To: Rob Nikander <[email protected]>, beginners
<[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Sat, 19 Mar 2011 14:36:17 -0400, Rob Nikander <[email protected]> wrote:
> Hi all,
>
> Where can I get emacs mode for Haskell? I tried the following page
> but the links from it are hanging forever or dead.
>
> http://www.haskell.org/haskellwiki/Haskell_mode_for_Emacs
>
> Rob
Hi Rob,
The darcs source code repo link works for me:
http://code.haskell.org/haskellmode-emacs/
or you could obtain the source code from a gentoo mirror:
ftp://mirror.internode.on.net/pub/gentoo/distfiles/haskell-mode-2.8.0.tar.gz
Regards, Mark
------------------------------
Message: 5
Date: Sat, 19 Mar 2011 17:21:56 -0700
From: Stef T <[email protected]>
Subject: [Haskell-beginners] SQLite3 Row <-> Data models -- Is this a
typical Haskell pattern ?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Hey Everyone,
Greetings, I am a total newb to haskell, so, I have a strange question
(perhaps). Consider I have a data model which looks like this;
data Brand = Brand { id :: Int,
name :: String,
created_at :: String,
updated_at :: String
}
Now, I have a sqlite3 function such as
checkout :: Int -> IO (Either String [[Row Value]])
checkout a = do
handle <- openConnection "/Users/stef/haskell/db/development.sqlite3"
execStatement handle $ "SELECT * from brands where id = " ++ show a
From this, I get a Row/Tuple. So far, so good, but, the question _I_ have
is, how do you go about mapping a returned row to the data model ? Is this even
a design pattern that is used in FP languages ? I admit, I have spent numerous
years in the MVC world, so, perhaps this is simply "not done". It would seem to
be a much nicer thing to then do ;
name myBrandModel
Thanks for reading this far, feel free to complain about my design :D
Regards
S.
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 33, Issue 27
*****************************************