From popavdan at yahoo.com Mon Nov 5 13:53:17 2007 From: popavdan at yahoo.com (Dan Popa) Date: Mon Nov 5 13:50:23 2007 Subject: [Haskell-fr] Fractional etc... In-Reply-To: <18a05e6b0710300149w36ae5389l63747faf5ef09aa9@mail.gmail.com> Message-ID: <484234.32385.qm@web36511.mail.mud.yahoo.com> --- Dupont Corentin wrote: > There must be a way to divide a real by an integer?? > My integer is eventually a "number of elements" as > in the exemple of Dan. As you can see below it's impossible to divide a Float by an Integer directly. Haskell is "strongly-typed" . But there exist a conversion function: fromInt :: Int->Float Prelude> (10.2::Float)/(2::Integer) ERROR - Type error in application *** Expression : 10.2 / 2 *** Term : 10.2 *** Type : Float *** Does not match : Integer Prelude> (10.2::Float)/(2::Int) ERROR - Type error in application *** Expression : 10.2 / 2 *** Term : 10.2 *** Type : Float *** Does not match : Int Solution: Prelude> (10.2::Float)/fromInt(2::Int) 5.1 References: Simon Thompson, Craft of Functional Programming, second edition, page 44 - basic types and definitions. > How would you compute a mean then? > > > On 10/29/07, Dan Popa wrote: > > > > Why don't you try : > > > map (/5.0) [0.0 .. 10.0] > > > > > > let m = 5.0 > > > map (/m) [0.0 .. 10.0] > > > > > I have a similar problem trying a divison by a > length > > of a list. Finaly I have to build a version of > length > > called len: > > > > len [] = 0.0 > > len (h:t) = 1.0 + len t > > > > Dan > > > > > > --- Dupont Corentin > wrote: > > > > > Salut, > > > J'ai vraiment du mal ? mettre au point un petit > > > programme... > > > Il me sort souvent des probl?mes du style : > > > > > > No instance for (Fractional Integer) > > > arising from use of `/' at > :1:4-7 > > > Possible fix: add an instance declaration for > > > (Fractional Integer) > > > > > > Par exemple sous ghci si je fait: > > > > > > map (/5) [0..10] > > > > > > tout se passe bien. > > > Mais si je me dit "tiens je voudrais bien > param?trer > > > le 5": > > > > > > let m = 5 > > > map (/m) [0..10] > > > > > > il me sort: > > > No instance for (Fractional Integer) > > > arising from use of `/' at > :1:4-7 > > > Possible fix: add an instance declaration > for > > > (Fractional Integer) > > > In the first argument of `map', namely `(/ > m)' > > > In the expression: map ((/ m)) ([0 .. 10]) > > > In the definition of `it': it = map ((/ m)) > ([0 > > > .. 10]) > > > > > > > > > Pourtant les 2 codes me sembles assez > ?quivalents!!! > > > Je peux utiliser des "fromInteger", ce qui > r?sout > > > temporairement le probl?me. > > > > > > Je me retrouve par la suite avec des (de > m?moire) > > > Infered type : Int > > > Expected type : Integer > > > > > > Ce qui me laisse dans le flou... > > > > > > a+ > > > Corentin > > > _______________________________________________ > > > Haskell-fr mailing list > > > Haskell-fr@haskell.org > > > > http://www.haskell.org/mailman/listinfo/haskell-fr > > > > > > > > > __________________________________________________ > > Do You Yahoo!? > > Tired of spam? Yahoo! Mail has the best spam > protection around > > http://mail.yahoo.com > > _______________________________________________ > > Haskell-fr mailing list > > Haskell-fr@haskell.org > > http://www.haskell.org/mailman/listinfo/haskell-fr > > > > _______________________________________________ > Haskell-fr mailing list > Haskell-fr@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-fr > __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From popavdan at yahoo.com Mon Nov 5 14:01:55 2007 From: popavdan at yahoo.com (Dan Popa) Date: Mon Nov 5 13:59:01 2007 Subject: [Haskell-fr] Fractional etc... In-Reply-To: Message-ID: <710432.95753.qm@web36503.mail.mud.yahoo.com> The stoty of divison may continue: The function may be named as an operator (///):: .... we can declare it's level of priority and how can it may associate values: left associative and priority level 7 infixl 7 /// And the required operator is ready to be used. Don't forget: Haskell allow the programmer to define it's own operators, using 10 levels of priority, being left, or right or no-associative. Also, data constructors like : of the lists can be (re)defined as you wish. Including their priority and associativity! Dan --- Chadda? Fouch? wrote: > Le 30/10/07, Dupont > Corentin a ?crit : > > > > There must be a way to divide a real by an > integer?? > > My integer is eventually a "number of elements" as > in the exemple of Dan. > > How would you compute a mean then? > > > > No, you can't "divide a real by an integer", but you > can introduce the > integer in the reals and divide by this. > > divideByI :: (Fractional a, Integral b) => a -> b -> > a > divideByI a b = a / fromIntegral b > > -- > Jeda? > _______________________________________________ > Haskell-fr mailing list > Haskell-fr@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-fr > __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com From bzine19 at yahoo.fr Thu Nov 15 21:25:29 2007 From: bzine19 at yahoo.fr (BOUNEB ZINE EL ABIDINE) Date: Thu Nov 15 21:22:13 2007 Subject: [Haskell-fr] a function which return the type of element Message-ID: <20071116022210.A71A7324134@www.haskell.org> Dear All ; I look for the function which return the type of un element in Haskell : For example If type(a) = Int then f else g Regards -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/haskell-fr/attachments/20071116/665aac0f/attachment.htm From westondan at imageworks.com Thu Nov 15 22:38:41 2007 From: westondan at imageworks.com (Dan Weston) Date: Thu Nov 15 22:35:16 2007 Subject: [Haskell-fr] a function which return the type of element In-Reply-To: <20071116022210.A71A7324134@www.haskell.org> References: <20071116022210.A71A7324134@www.haskell.org> Message-ID: <473D10C1.8040105@imageworks.com> There are several possibilities. The one that maps most closely to the expression you wrote is: > import Data.Typeable(typeOf) > if typeOf a == typeOf (undefined :: Int) then f else g Of course, f and g must have the same type. See http://haskell.org/ghc/docs/latest/html/libraries/base-3.0.0.0/Data-Typeable.html for more details. Dan BOUNEB ZINE EL ABIDINE wrote: > > Dear All ; > > I look for the function which return the type of un element in Haskell : > > For example > > If type(a) = Int then f else g > > Regards From chaddai.fouche at gmail.com Thu Nov 15 22:38:53 2007 From: chaddai.fouche at gmail.com (=?ISO-8859-1?Q?Chadda=EF_Fouch=E9?=) Date: Thu Nov 15 22:35:25 2007 Subject: [Haskell-fr] a function which return the type of element In-Reply-To: <20071116022210.A71A7324134@www.haskell.org> References: <20071116022210.A71A7324134@www.haskell.org> Message-ID: 2007/11/16, BOUNEB ZINE EL ABIDINE : > I look for the function which return the type of un element in Haskell : > For example > If type(a) = Int then f else g Pourquoi poser une question en anglais sur haskell-_fr_ ?? Il y a plusieurs fa?on de faire ce que tu demandes sans avoir besoin d'une telle fonction, par exemple les typeclass, mais d'abord j'aimerais savoir pourquoi tu penses en avoir besoin, il est tr?s rare qu'on ait vraiment besoin de ceci dans un programme normal. -- Jeda? From bzine19 at yahoo.fr Thu Nov 15 23:09:33 2007 From: bzine19 at yahoo.fr (BOUNEB ZINE EL ABIDINE) Date: Thu Nov 15 23:06:13 2007 Subject: [Haskell-fr] a function which return the type of element In-Reply-To: Message-ID: <20071116040611.D2EEF324153@www.haskell.org> Merci sur votre courier , et je demande pardon sur l'anglais, mon objectif et de d?velopper un langage orient? objet et fonctionnelle, je vais faire une translation d'un langage de haut niveau qui support les notions de l'orient? objet implicitement typ? vers un langage qui est purement fonctionnelle et avec un typage explicite , comme la notion du type class en Haskell et je veut faire comme premi?re ?tape une translation d'un exemple de type class. merci -----Original Message----- From: haskell-fr-bounces@haskell.org [mailto:haskell-fr-bounces@haskell.org] On Behalf Of Chadda? Fouch? Sent: vendredi 16 novembre 2007 11:39 To: La liste Haskell Francophone Subject: Re: [Haskell-fr] a function which return the type of element 2007/11/16, BOUNEB ZINE EL ABIDINE : > I look for the function which return the type of un element in Haskell : > For example > If type(a) = Int then f else g Pourquoi poser une question en anglais sur haskell-_fr_ ?? Il y a plusieurs fa?on de faire ce que tu demandes sans avoir besoin d'une telle fonction, par exemple les typeclass, mais d'abord j'aimerais savoir pourquoi tu penses en avoir besoin, il est tr?s rare qu'on ait vraiment besoin de ceci dans un programme normal. -- Jeda? _______________________________________________ Haskell-fr mailing list Haskell-fr@haskell.org http://www.haskell.org/mailman/listinfo/haskell-fr From popavdan at yahoo.com Fri Nov 16 13:02:25 2007 From: popavdan at yahoo.com (Dan Popa) Date: Fri Nov 16 12:58:57 2007 Subject: [Haskell-fr] a function which return the type of element In-Reply-To: <20071116022210.A71A7324134@www.haskell.org> Message-ID: <626383.62965.qm@web36514.mail.mud.yahoo.com> --- BOUNEB ZINE EL ABIDINE wrote: > I look for the function which return the type of un > element in Haskell : > For example > > If type(a) = Int then f else g > Hello,I am Dan P. and I try to answer: If you want to get different effects, let's say different expression being evaluated according to the TYPE of the entity involved you may try to use: - a class - some instances of that class, every one having it's own definition of 'type' Let's start: Let's suppose f and g are two functions. First of all you can try to define your own class: class MyClass a b c where type :: a -> (b->c) So, for every element of type a, the value of type(a) will be a function. Let's suppose we know the signatures of f and g: something like (the same for both f and g) f:: Float -> Char g:: Float -> Char ----------------------------------------------------- | TO define the behaviour of type for a being an Int| | YOU HAVE TO INSTANTIATE the class MyClass and tell| | how can type will work ! | ----------------------------------------------------- instance MyClass Int Float Char where type _ = f For the other types (you did not specify them,sorry) for example for String other instances can be defined: instance MyClass String Float Char where type _ = g In this way the function 'type' became a polimorphically overrloaded function. -------------------------------------------- Simplified version. f::X and g :: X , X being a specific type. class SimpleClass a where type :: a -> X instance SimpleClass Int where type _ = f instance SimpleClass Char where type _ = g and so one for other types which can be the type of a. ----------------------------------------------- May be more solutions. Let's hope this will help. Dan ____________________________________________________________________________________ Be a better pen pal. Text or chat with friends inside Yahoo! Mail. See how. http://overview.mail.yahoo.com/ From westondan at imageworks.com Fri Nov 16 14:25:11 2007 From: westondan at imageworks.com (Dan Weston) Date: Fri Nov 16 14:21:47 2007 Subject: [Haskell-fr] a function which return the type of element In-Reply-To: <20071116040611.D2EEF324153@www.haskell.org> References: <20071116040611.D2EEF324153@www.haskell.org> Message-ID: <473DEE97.5030904@imageworks.com> Pri?re aussi d'excuser ma r?ponse en anglais. En g?n?ral, j'essaie de r?pondre dans la m?me langue que celle de la question pos?e. Si tu es content de poser tes questions (et d'y recevoir les r?ponses) en anglais, tu trouveras une communaut? beaucoup plus vaste et plus diverse ? la liste haskell-cafe@haskell.org. Dan BOUNEB ZINE EL ABIDINE wrote: > Merci sur votre courier , et je demande pardon sur l'anglais, mon objectif > et de d?velopper un langage orient? objet et fonctionnelle, je vais faire > une translation d'un langage de haut niveau qui support les notions de > l'orient? objet implicitement typ? vers un langage qui est purement > fonctionnelle et avec un typage explicite , comme la notion du type class en > Haskell et je veut faire comme premi?re ?tape une translation d'un exemple > de type class. merci > > -----Original Message----- > From: haskell-fr-bounces@haskell.org [mailto:haskell-fr-bounces@haskell.org] > On Behalf Of Chadda? Fouch? > Sent: vendredi 16 novembre 2007 11:39 > To: La liste Haskell Francophone > Subject: Re: [Haskell-fr] a function which return the type of element > > 2007/11/16, BOUNEB ZINE EL ABIDINE : >> I look for the function which return the type of un element in Haskell : >> For example >> If type(a) = Int then f else g > > Pourquoi poser une question en anglais sur haskell-_fr_ ?? > Il y a plusieurs fa?on de faire ce que tu demandes sans avoir besoin > d'une telle fonction, par exemple les typeclass, mais d'abord > j'aimerais savoir pourquoi tu penses en avoir besoin, il est tr?s rare > qu'on ait vraiment besoin de ceci dans un programme normal. >