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 to correctly define data types? (Brent Yorgey)
2. Re: Error in converting the function from let in clause to
where (Yitzchak Gale)
3. Re: Can fields in a record be optional? (David Place)
4. Re: Can fields in a record be optional? (austin seipp)
5. Re: Can fields in a record be optional? (David Place)
6. (Implicit) equality testing using multiple function
definitions (Tom Murphy)
7. Re: (Implicit) equality testing using multiple function
definitions (austin seipp)
8. Re: (Implicit) equality testing using multiple function
definitions (Arlen Cuss)
9. Re: (Implicit) equality testing using multiple function
definitions (David Place)
----------------------------------------------------------------------
Message: 1
Date: Mon, 18 Jul 2011 15:36:14 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] How to correctly define data types?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
On Mon, Jul 18, 2011 at 02:26:33PM -0300, Davi Santos wrote:
> Hello (first post),
> I have spent so many time learning typeclasses and thinking it was part of
> Haskell essence... and suddenly I read the discussion "[Haskell-beginners]
> Can fields in a record be optional?".
> So typeclasses are not always recomended?
>
> I'm implementing a Machine Learning framework and I am in a sort of related
> dilemma.
>
> I found three ways of implementing the same distance function between
> "examples" (aka "attribute vectors" or simply "Float vectors" for mere
> mortals :) ):
>
> [obs: "Example" datatype will be added more fields later]
>
> --------------first------------------------------------
> module ML where
>
> data Example =
> Example [Float] deriving (Show)
>
> class ExampleClass a where
> (distance) :: a ? a ? Float
>
> instance ExampleClass Example where
> (Example atts1) distance (Example atts2) =
> sqrt $ sum $ map (?(x, y) ? (x-y)?2) $ zip atts1 atts2
> =================================
This is an unnecessary use of type classes, unless you plan to make
additional instances of ExampleClass later.
> --------------second------------------------------------
> module ML where
>
> data Example =
> Example {attributes :: [Float]} deriving (Show)
>
> distance :: Example ? Example ? Float
> distance ex1 ex2 =
> sqrt $ sum $ map (?(x, y) ? (x-y)?2) $
> zip (attributes ex1) (attributes ex2)
> =================================
>
>
> --------------third------------------------------------
> module ML where
>
> data Example =
> Example [Float] deriving (Show)
>
> distance :: Example ? Example ? Float
> distance (Example att1) (Example att2) =
> sqrt $ sum $ map (?(x, y) ? (x-y)?2) $
> zip (att1) (att2)
> =================================
I'd say these are about the same style-wise, it's a matter of
preference. But if more fields will be added to Example later, using
record labels may be a good idea.
> All three reserves the word "distance" for itself and the second reserves
> also the word "attributes".
> How could I implement the module ML and which would be the best way to set
> "attributes" outside the module?
I'm not sure what you mean by "set 'attributes' outside the module",
can you clarify?
-Brent
------------------------------
Message: 2
Date: Mon, 18 Jul 2011 22:56:20 +0300
From: Yitzchak Gale <[email protected]>
Subject: Re: [Haskell-beginners] Error in converting the function from
let in clause to where
To: mukesh tiwari <[email protected]>
Cc: [email protected]
Message-ID:
<CAOrUaLYaTxe59yW8vo3h6tcfeF4hdBvJe3oho9jNYac-=fg...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
Hi Mukesh,
mukesh tiwari wrote:
> ...I am trying to implement
> elliptic curve prime factorisation and initially wrote
> function "addPoints" using let in clause. It works fine. When i? converted
> the "addPoint" using where clause then its not working and not adding the
> points correctly ....
> In case of indentation problem
> , source code on http://hpaste.org/49174 .
Your suspicion is correct. It is an indentation problem.
The first "where" is indented more than "(_, d') -> Right d'"
so it is part of that case. It has no effect, because none
of the variables defined in the "where" is used in that case,
only d'.
The second "where" is indented less than the last case,
so it applies to the entire "addPoints" function, all the
way back to the beginning!
Regards,
Yitz
------------------------------
Message: 3
Date: Mon, 18 Jul 2011 16:47:14 -0400
From: David Place <[email protected]>
Subject: Re: [Haskell-beginners] Can fields in a record be optional?
To: [email protected]
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Thanks for the pointer to Data.Default. I don't see how it applies in this
case, though. The goal is to avoid mentioning the defaulted fields when
constructing a record instance. Example?
____________________
David Place
Owner, Panpipes Ho! LLC
http://panpipesho.com
[email protected]
On Jul 18, 2011, at 1:45 AM, Christopher Done wrote:
> On 18 July 2011 00:18, David Place <[email protected]> wrote:
>> The way I often do this is to create an "ur" instance where all the fields
>> have default values. Then to create an instance, I just do a "record
>> update" of this instance.
>
> For this there is Data.Default in data-default:
>
> http://hackage.haskell.org/packages/archive/data-default/0.2.0.1/doc/html/Data-Default.html
------------------------------
Message: 4
Date: Mon, 18 Jul 2011 16:33:49 -0500
From: austin seipp <[email protected]>
Subject: Re: [Haskell-beginners] Can fields in a record be optional?
To: David Place <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID:
<caes96n1fhsroqaipnwm3xywz3nxmz7+172r9bzzw_hubvei...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
It's effectively the exact same thing as your example but with the
default record put into a typeclass for convenience. Refactoring your
original post:
data Contract = Contract {
currency :: Currency
, payments :: Double
, contracts :: [Contract]
}
deriving (Show)
instance Default Contract where
def = Contract { currency = undefined, payments = undefined, contracts = [] }
one :: Currency -> Contract
one c = def { currency = c, payments = 1}
and :: Contract -> Contract -> Contract
(and) c1 c2 = def { contracts = [c1, c2] }
It's a very simple package and provides some default instances for
your convenience. Use it if you like.
On Mon, Jul 18, 2011 at 3:47 PM, David Place <[email protected]> wrote:
> Thanks for the pointer to Data.Default. ?I don't see how it applies in this
> case, though. ?The goal is to avoid mentioning the defaulted fields when
> constructing a record instance. ?Example?
>
> ____________________
> David Place
> Owner, Panpipes Ho! LLC
> http://panpipesho.com
> [email protected]
>
>
>
> On Jul 18, 2011, at 1:45 AM, Christopher Done wrote:
>
>> On 18 July 2011 00:18, David Place <[email protected]> wrote:
>>> The way I often do this is to create an "ur" instance where all the fields
>>> have default values. ?Then to create an instance, I just do a "record
>>> update" of this instance.
>>
>> For this there is Data.Default in data-default:
>>
>> http://hackage.haskell.org/packages/archive/data-default/0.2.0.1/doc/html/Data-Default.html
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
Regards,
Austin
------------------------------
Message: 5
Date: Mon, 18 Jul 2011 17:42:02 -0400
From: David Place <[email protected]>
Subject: Re: [Haskell-beginners] Can fields in a record be optional?
To: austin seipp <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Thanks, Austin, for taking the time to explain. I guess I don't find it gives
any advantage over the way I currently do it.
_____________________
David F. Place
Owner, Panpipes Ho!, LLC
http://panpipesho.com
On Jul 18, 2011, at 5:33 PM, austin seipp <[email protected]> wrote:
> It's effectively the exact same thing as your example but with the
> default record put into a typeclass for convenience.
------------------------------
Message: 6
Date: Mon, 18 Jul 2011 20:44:18 -0400
From: Tom Murphy <[email protected]>
Subject: [Haskell-beginners] (Implicit) equality testing using
multiple function definitions
To: beginners <[email protected]>
Message-ID:
<CAO9Q0tX0hVZpDGUR=85++dxo_+e45rxbxmugtwuihr9eqea...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
Hi list!
When I define an algebraic datatype without an instance for Eq,
I'm obviously unable to use the (==) function on it. I can
pattern-match with a series of function definitions (f [] = False; f x
= True) on the expression, though. Why is that?
I understand that in the second case I'm not literally using the
(==) function, but it seems like there would be instances where you'd
intentionally not want to be able to test for equality, and
pattern-matching with multiple function definitions circumvents that.
Thanks for your time,
Tom
------------------------------
Message: 7
Date: Mon, 18 Jul 2011 19:59:31 -0500
From: austin seipp <[email protected]>
Subject: Re: [Haskell-beginners] (Implicit) equality testing using
multiple function definitions
To: Tom Murphy <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
<CAEs96n3rEYgHQU8AZULwD=sY3pey=zscu_dnjn0ero3qruv...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
Pattern matching 'works' simply because all it does is what it
describes: it scrutinizes or 'matches' a value of a specific type
against a set constructors of that type.
The classic way to circumvent this is to put the datatype behind a
module, where only the type is exported, but none of the constructors
are exported:
-- Note the difference between the export 'Foo(..)' and just 'Foo' -
-- one exposes the type and all its constructors, the other exposes only
-- the type.
module HiddenType ( Foo, mySpecialEquality ) where
data Foo = Bar ... -- no 'deriving Eq'
-- a special, super secret equality function, so clients can't create
-- their own or try to.
mySpecialEquality :: Foo -> Foo -> Bool
mySpecialEquality = ...
module Main where
import Foo
-- this is invalid, as 'Bar' is not exported and thus not in scope, no
pattern matching allowed.
f :: Foo -> ...
f Bar = ...
More generally this kind of idiom is found when you deal with what you
call 'smart constructors' - functions which, like a constructor,
create a value of a specific data type. But instead of doing it via
the constructor itself, you make the type opaque, and export functions
that construct values in the sensible or correct way:
module Even (Even, makeFoo) where
-- the underlying Int should only be even
data Even = Even Int deriving (Eq, Show)
-- safe, smart constructor for the Even datatype
makeEven :: Int -> Maybe Even
makeEven x = if not (x `mod` 2 == 0) then Nothing else Just x
Hope it helps.
On Mon, Jul 18, 2011 at 7:44 PM, Tom Murphy <[email protected]> wrote:
> Hi list!
> ? ? When I define an algebraic datatype without an instance for Eq,
> I'm obviously unable to use the (==) function on it. I can
> pattern-match with a series of function definitions (f [] = False; f x
> = True) on the expression, though. Why is that?
> ? ? I understand that in the second case I'm not literally using the
> (==) function, but it seems like there would be instances where you'd
> intentionally not want to be able to test for equality, and
> pattern-matching with multiple function definitions circumvents that.
>
> Thanks for your time,
> Tom
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
Regards,
Austin
------------------------------
Message: 8
Date: Tue, 19 Jul 2011 11:05:06 +1000
From: Arlen Cuss <[email protected]>
Subject: Re: [Haskell-beginners] (Implicit) equality testing using
multiple function definitions
To: Tom Murphy <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
19/07/2011 10:44 AM, Tom Murphy kirjutas:
> Hi list!
> When I define an algebraic datatype without an instance for Eq,
> I'm obviously unable to use the (==) function on it. I can
> pattern-match with a series of function definitions (f [] = False; f x
> = True) on the expression, though. Why is that?
> I understand that in the second case I'm not literally using the
> (==) function, but it seems like there would be instances where you'd
> intentionally not want to be able to test for equality, and
> pattern-matching with multiple function definitions circumvents that.
My take on this (may not be canonical, or even right!) is that pattern
matching is matching on the shape of the object. E.g. with the data type:
> data Foo a = Bar a | Quux a
.. then it's not possible to stop someone matching on a Foo a to
determine if it was constructed with Bar or Quux. If you couldn't know
that, it'd be impossible to do .. well, anything at all with it.
But Foo a isn't an Eq instance, even if a happens to be, because we
haven't defined the rules by which that happens. Deriving Eq is an easy
way to do that, but it simply may not make sense to do so.
Pattern matching is a different beast to testing for equality. Though I
have few good examples, you can't match on 'x' for an arbitrary 'x' at
run-time: the shape has to be determined at compile-time. That means you
can only use (nested) constructors, so e.g. you can't match the contents
of a Data.Map.
Cheers,
Arlen
------------------------------
Message: 9
Date: Mon, 18 Jul 2011 21:23:01 -0400
From: David Place <[email protected]>
Subject: Re: [Haskell-beginners] (Implicit) equality testing using
multiple function definitions
To: Arlen Cuss <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Jul 18, 2011, at 9:05 PM, Arlen Cuss wrote:
> My take on this (may not be canonical, or even right!) is that pattern
> matching is matching on the shape of the object.
Perhaps we could say that pattern matching implements Structural Induction
rather than set equality.
> http://en.wikipedia.org/wiki/Structural_induction
____________________
David Place
Owner, Panpipes Ho! LLC
http://panpipesho.com
[email protected]
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 37, Issue 35
*****************************************