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: Enum for natural numbers (Daniel Fischer)
2. Re: print all days of calendar ([email protected])
3. Problems with EclipseFP (Maciej Piechotka)
4. Re: print all days of calendar (Daniel Fischer)
5. Type constraints question (David Gordon)
6. Re: Enum for natural numbers ([email protected])
7. Re: print all days of calendar ([email protected])
8. Re: Enum for natural numbers (Deniz Dogan)
----------------------------------------------------------------------
Message: 1
Date: Sun, 20 Dec 2009 21:55:49 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Enum for natural numbers
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Sonntag 20 Dezember 2009 21:35:45 schrieb [email protected]:
> > As a further example,
> >
> > replicate :: Int -> a -> [a]
> > replicate n x
> >
> > | n <= 0 = []
> > | otherwise = x:replicate (n-1) x
> >
> > may help.
>
> my problem is that I don't know how to use Enum correctly and didn't find
> any helpfull example
>
toEnum and fromEnum should give the correspondence
0 <-> Z
1 <-> S Z
2 <-> S (S Z)
3 <-> S (S (S Z))
4 <-> S (S (S (S Z)))
5 <-> S (S (S (S (S Z))))
and so on. Of course, defining toEnum entirely via pattern matching
toEnum 0 = Z
toEnum 1 = S Z
toEnum 2 = S (S Z)
toEnum 3 = S (S (S Z))
...
will take too long to type (and will drive the compiler mad), so define it via
recursion.
It will strongly resemble the definition of replicate above.
The definition of fromEnum will resemble that of genericLength,
http://www.haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/src/Data-
List.html#genericLength
------------------------------
Message: 2
Date: Sun, 20 Dec 2009 22:11:51 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] print all days of calendar
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
I implemented the first step which works if I fix the months (instead of the
Month Enum) and just return the day and not an element of type Date
this works:
calendar year = [ day | month <- [Jan, Feb], day <- [1..31],
legalDate(day,month,year) == True ]
this doesn't work:
calendar year = [ Date(day,month,year) | month <- Month, day <- [1..31],
legalDate(day,month,year) == True ]
-------- Original-Nachricht --------
> Datum: Sat, 19 Dec 2009 11:16:45 +0100
> Von: "Chaddaï Fouché" <[email protected]>
> An: [email protected]
> CC: [email protected]
> Betreff: Re: [Haskell-beginners] print all days of calendar
> On Fri, Dec 18, 2009 at 5:01 PM, <[email protected]> wrote:
> > I also have a function from a previous exercise which checks if a given
> date is valid. Is there a function for a loop that iterates from 1 to n and
> checks if the date is valid. If it's valid it should return the date
> otherwise it should jump to the next month or end at the end of the year? Or
> is
> it better to do it on another way with this data I have?
>
> Since you derived Enum for Month, you can do [Jan..Dec] and get the
> list of the months in order. There are then two options, either you
> generate all cartesian product of [Jan..Dec] and [1..31] and check
> which are valid, or you write a function that for a given month and
> year tells you how many days it counts and then generate for month "m"
> all the pair in combination with [1..daysCount m].
>
> Whatever your decision, list comprehensions are probably the tool of
> choice to do it though it is by no mean harder to do without.
>
> --
> Jedaï
--
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
------------------------------
Message: 3
Date: Sun, 20 Dec 2009 21:23:13 +0000
From: Maciej Piechotka <[email protected]>
Subject: [Haskell-beginners] Problems with EclipseFP
To: [email protected]
Message-ID: <1261344193.5673.1.ca...@picard>
Content-Type: text/plain; charset="UTF-8"
Did anyone used eclipsefp? I installed version 2 but I didn't change
anything in eclipse (I haven't notice a difference - it does not
recognise .hs file, nothing about haskell in help/new project/new file
etc.).
Regards
------------------------------
Message: 4
Date: Sun, 20 Dec 2009 22:25:53 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] print all days of calendar
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Sonntag 20 Dezember 2009 22:11:51 schrieb [email protected]:
> I implemented the first step which works if I fix the months (instead of
> the Month Enum) and just return the day and not an element of type Date
>
> this works:
> calendar year = [ day | month <- [Jan, Feb], day <- [1..31],
> legalDate(day,month,year) == True ]
>
> this doesn't work:
> calendar year = [ Date(day,month,year) | month <- Month, day <- [1..31],
> legalDate(day,month,year) == True ]
Date was a type synonym for the triple, so
calendar year
= [ (day,month,year) | month <- Month, day <- [1..31],
legalDate(day,month,year)]
should be what you want.
Note, "condition == True" is better written as "condition".
------------------------------
Message: 5
Date: Sun, 20 Dec 2009 23:18:11 -0800
From: David Gordon <[email protected]>
Subject: [Haskell-beginners] Type constraints question
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Thanks for this detailed reply, that's helped with a lot of questions I had.
I think the real problem here is that I am still looking for
C++/Java-style subtype polymorphism. I wanted the child to be any type
implementing MyClass, including types declared in other modules,
without having to declare a new MyClass each time to be able to return
that type. I thought that simply promising to return some type
implementing the MyClass interface would be enough to satisfy type
safety.
The thing I am actually trying to build is a dynamically-generated
view on a large tree structure. The generation is done by a chain of
objects built according to a configuration loaded at runtime. Each
object in the chain represents modifications to the tree which it
exposes via a simple getChildren/getParent interface which exposes the
view on the tree generated by the chain. Crucially, it should be
possible to introduce new chain objects, and it should also be
possible to create arbitrary chains at runtime. In some cases the
result of getChildren will vary depending on the runtime-constructed
chain.
It seems like the simplest solution (in pure Haskell 98 at least)
would be to unify my possible child classes into a single Test data
type with different constructors and methods according to the
different types I was trying to implement before. So, I lose the
extensibility but at least I can prototype my design in a simple way.
I am also investigating type families and GADTs - feels like it will
be a few sessions before I've got my head around them. It seems like
being expert in type systems is part of the Haskell lifestyle!
thanks for your help,
David
2009/12/18 Daniel Fischer <[email protected]>
Am Samstag 19 Dezember 2009 00:55:34 schrieb David Gordon:
> Apologies for the spam, I had some difficulty getting my email address set
> up right on the list and wanted to make sure this finally got through (or
> perhaps there are like 5 copies on the list now... sorry.)
>
> thanks
>
> ---------- Forwarded message ----------
>
> Ok, now I will try asking the right question (previous try wasn't actually
> an example of the problem I'm having)
>
> Here's the code:
>
>
> data Test = Test
>
> data Test2 = Test2
>
> class MyClass a where
> getChild :: MyClass b => a -> b
>
> instance MyClass Test where
> getChild a = Test2
>
> instance MyClass Test2 where
> getChild a = Test
>
> On HUGS I get:
>
> Error occurred
> ERROR line 9 - Inferred type is not general enough
> *** Expression : getChild
> *** Expected type : (MyClass Test, MyClass a) => Test -> a
> *** Inferred type : (MyClass Test, MyClass Test2) => Test -> Test2
>
> So, what's the problem with always returning a particular instance of
> MyClass? I just want to constrain it to be an instance of MyClass, nothing
> more.
The signature of getChild promises that *whichever type the caller
wants*, as long as it's
a member of MyClass, it can be provided. The implementation returns
one specific type.
It's different from interfaces in Java, where the callee decides which
type is returned,
here the caller can demand the type that the callee has to return.
The signature of getChild is actually
getChild :: forall b. MyClass b => a -> b
while the implementation has the signature (not legal Haskell)
getChild :: exists b. MyClass b => a -> b.
What you want is, I believe, that each instance a of MyClass specifies
a type b belonging
to MyClass which getChild returns.
You can achieve part of that for example with a multiparameter type class
{-# LANGUAGE MultiParamTypeClasses #-}
class MyClass2 a b where
getChild :: a -> b
However, this allows that a has children of more than one type (
instance MyClass2 Test Test2 where
getChild _ = Test2
instance MyClass2 Test Test where
getChild _ = Test
) and this doesn't enforce that b itself has children.
To enforce that every type has only one type of children, you can use functional
dependencies (or type families, see below):
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-}
class MyClass3 a b | a -> b where
getChild :: a -> b
The functional dependency "a -> b" (separated from the class head by
"|") says that a
uniquely determines b. But that still doesn't enforce that b has
children. For that, you
need
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleContexts #-}
class MyClass4 a b | a -> b where
getChild :: MyClass4 b c => a -> b
Now each type can have only one type of children and they must have
children too.
Another way to achieve the above is via type families:
{-# LANGUAGE TypeFamilies, FlexibleContexts #-}
class MyClass5 a where
type Child a :: *
getChild :: (MyClass5 (Child a)) => a -> Child a
instance MyClass5 Test where
type Child Test = Test2
getChild _ = Test2
instance MyClass5 Test2 where
type Child Test2 = Test
getChild _ = Test
>
> thanks,
>
> David
>
>
> 2009/12/18 David Gordon <[email protected]>
>
> > Hi Folks,
> > Total newbie here. I don't know if I am having a syntactic problem or a
>
> conceptual problem.
>
> > This code:
> > data Test = Test
> > data Test2 = Test2
> > class MyClass a where
> > getChild :: (MyClass b) => a -> b
> > instance MyClass Test where
> > getChild = Test2
> > instance MyClass Test2 where
> > getChild = Test
> > results in:
> > [1 of 1] Compiling Main ( test.hs, interpreted )
> > test.hs:10:15:
> > Couldn't match expected type `Test -> b'
> > against inferred type `Test2'
> > In the expression: Test2
> > In the definition of `getChild': getChild = Test2
> > In the instance declaration for `MyClass Test'
> > test.hs:13:15:
> > Couldn't match expected type `Test2 -> b'
> > against inferred type `Test'
> > In the expression: Test
> > In the definition of `getChild': getChild = Test
> > In the instance declaration for `MyClass Test2'
> > Failed, modules loaded: none.
> > Is this a reasonable thing to try and do in Haskell? If not, I have a lot
>
> more questions... ;)
>
> > many thanks,
> > David
------------------------------
Message: 6
Date: Mon, 21 Dec 2009 10:56:06 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] Enum for natural numbers
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Now everything works but to print Z also for negative Integers. Don't know how
to implement the <=0 or le0 in this case again:
instance Enum Nat where
toEnum 0 = Z
toEnum (n+1) = S(toEnum n)
fromEnum Z = 0
fromEnum (S n) = 1 + fromEnum n
-------- Original-Nachricht --------
> Datum: Sun, 20 Dec 2009 14:45:02 +0100
> Von: Deniz Dogan <[email protected]>
> An: [email protected]
> CC: [email protected]
> Betreff: Re: [Haskell-beginners] Enum for natural numbers
> 2009/12/20 <[email protected]>:
> > Maybe I didn't understand the exercise if have to do. It says:
> > "Write the instance Enum Nat where toEnum is defined as a total function
> that returns Z for negative integers. Some examples:
> > *Main> toEnum (-1) :: Nat
> > Z
> > *Main> toEnum 0 :: Nat
> > Z
> > *Main> toEnum 1 :: Nat
> > S Z
> > *Main> fromEnum (S (S Z))
> > 2
> >
> > so I did:
> > data Nat = Z | S Nat deriving (Eq,Ord,Show)
> > instance Enum Nat where
> > toEnum x|x > 0 = S Z
> > |otherwise = Z
> >
> >
> > somehow it looks really wrong. Do you understand want I have to do and
> how it should look like?
> >
>
> In your current definition of toEnum, you always return "S Z" (in
> other words, "1", "the successor of zero") for any integer i > 0. This
> is clearly incorrect as you said. Your "otherwise" guard is correct,
> but the other one is not. I get the feeling that this exercise is
> about learning how to use recursion, so look into that.
>
> A few relevant links:
> http://en.wikibooks.org/wiki/Haskell/Recursion (the factorial example)
> http://learnyouahaskell.com/recursion
> http://book.realworldhaskell.org/read/functional-programming.html
>
> Hope that helps
>
> --
> Deniz Dogan
--
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser
------------------------------
Message: 7
Date: Mon, 21 Dec 2009 11:05:03 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] print all days of calendar
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
-------- Original-Nachricht --------
> Datum: Sun, 20 Dec 2009 22:25:53 +0100
> Von: Daniel Fischer <[email protected]>
> An: [email protected]
> CC: [email protected]
> Betreff: Re: [Haskell-beginners] print all days of calendar
> Am Sonntag 20 Dezember 2009 22:11:51 schrieb [email protected]:
> > I implemented the first step which works if I fix the months (instead of
> > the Month Enum) and just return the day and not an element of type Date
> >
> > this works:
> > calendar year = [ day | month <- [Jan, Feb], day <- [1..31],
> > legalDate(day,month,year) == True ]
> >
> > this doesn't work:
> > calendar year = [ Date(day,month,year) | month <- Month, day <- [1..31],
> > legalDate(day,month,year) == True ]
>
> Date was a type synonym for the triple, so
> calendar year
> = [ (day,month,year) | month <- Month, day <- [1..31],
> legalDate(day,month,year)]
>
> should be what you want.
> Note, "condition == True" is better written as "condition".
still have the problem that it says:
"Not in scope: data constructor `Month'" but Month is declared as:
data Month = Jan | Feb | Mar | Apr | May | Jun | Jul | Ago | Sep | Oct | Nov |
Dec deriving (Eq,Enum,Show)
--
Preisknaller: GMX DSL Flatrate für nur 16,99 Euro/mtl.!
http://portal.gmx.net/de/go/dsl02
------------------------------
Message: 8
Date: Mon, 21 Dec 2009 11:25:46 +0100
From: Deniz Dogan <[email protected]>
Subject: Re: [Haskell-beginners] Enum for natural numbers
To: [email protected]
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
2009/12/21 <[email protected]>:
> Now everything works but to print Z also for negative Integers. Don't know
> how to implement the <=0 or le0 in this case again:
>
> instance Enum Nat where
> toEnum 0 = Z
> toEnum (n+1) = S(toEnum n)
> fromEnum Z = 0
> fromEnum (S n) = 1 + fromEnum n
>
Again, look into guards. You are definitely on the right track.
Again, you should look into guards.
instance Enum Nat where
toEnum n | n <= 0 = Z
| otherwise = ... -- do what with n?
fromEnum Z = 0
fromEnum (S n) = 1 + fromEnum n
You are on the right track.
--
Deniz Dogan
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 18, Issue 19
*****************************************