Hello,
i've some trouble with named fields in data types. data Carnet = Adresse { nom :: String, cp :: Integer, ville :: String } | Telephone {nom::String, telnum::String} deriving Show contact01 = Adresse "Didier" 51100 "Reims" contact02 = Adresse {nom="Laure", cp=0} -- line 1 --contact02 Adresse { ville="Nogent" } -- line 2 --contact02 { ville="Nogent" } -- line 3 contact03=Telephone "Didier" "0326..." -- line 4 When loading this source : line 1 says "warning : Fields not initialized :ville line 2 and 3 when uncommented give parse error (possibly indentation) I'm ok with line 1. Is it possible to write such things as line2 or 3 ? Which syntax ? Thanks for help. Didier. |
On Tue, Nov 17, 2009 at 10:24 PM, legajid <[hidden email]> wrote:
> Hello, > i've some trouble with named fields in data types. > > data Carnet = Adresse ?{ nom :: String, cp :: Integer, ville :: String ?} > ? ? ? | > ? ? ?Telephone {nom::String, telnum::String} > ? deriving Show > > contact01 = Adresse "Didier" 51100 "Reims" > contact02 = Adresse {nom="Laure", cp=0} -- line 1 > --contact02 Adresse { ville="Nogent" } ? ? ? -- line 2 > --contact02 { ville="Nogent" } ? ? ? ? ? ? ? ? ? ? -- line 3 > contact03=Telephone "Didier" "0326..." ? ? ? -- line 4 > > When loading this source : > line 1 says "warning : Fields not initialized :ville > line 2 and 3 when uncommented give parse error (possibly indentation) > > I'm ok with line 1. > Is it possible to write such things as line2 or 3 ? Which syntax ? line 2 appears correct except you forgot the "="... Of course some fields aren't initialized so you may have a problem if you try to access them later on. (La ligne 2 est correcte sauf que tu as oubli? le ?gal "="... Bien s?r certains des champs ne sont pas initialis?s et ?a pourrait poser probl?me si tu essaies d'y acc?der plus tard) Note that you can also create a new value with some fields in common with an old value, for instance : (Note que tu peux aussi cr?er une nouvelle valeur partageant un certain nombre de champs avec une autre valeur d?j? d?clar?e) > contact01 = Adresse "Didier" 51100 "Reims" > contact02 = contact01 { nom = "Charles" } Which leads to the practice of using records to create a "default setting" value where you just modify the field that concern you. (Ce qui a donn? lieu ? l'idiome qui consiste ? utiliser un type enregistrement (avec des champs nomm?s) pour les configuration avec une valeur "r?glage par d?faut" dont on ne modifie que les champs qui nous int?resse) > main = xmonad defaultConfig {terminal = "konsole"} -- Jeda? |
Chadda? Fouch? a ?crit :
> On Tue, Nov 17, 2009 at 10:24 PM, legajid <[hidden email]> wrote: > >> Hello, >> i've some trouble with named fields in data types. >> >> data Carnet = Adresse { nom :: String, cp :: Integer, ville :: String } >> | >> Telephone {nom::String, telnum::String} >> deriving Show >> >> contact01 = Adresse "Didier" 51100 "Reims" >> contact02 = Adresse {nom="Laure", cp=0} -- line 1 >> --contact02 Adresse { ville="Nogent" } -- line 2 >> --contact02 { ville="Nogent" } -- line 3 >> contact03=Telephone "Didier" "0326..." -- line 4 >> >> When loading this source : >> line 1 says "warning : Fields not initialized :ville >> line 2 and 3 when uncommented give parse error (possibly indentation) >> >> I'm ok with line 1. >> Is it possible to write such things as line2 or 3 ? Which syntax ? >> > > line 2 appears correct except you forgot the "="... Of course some > fields aren't initialized so you may have a problem if you try to > access them later on. > (La ligne 2 est correcte sauf que tu as oubli? le ?gal "="... Bien s?r > certains des champs ne sont pas initialis?s et ?a pourrait poser > probl?me si tu essaies d'y acc?der plus tard) > > Note that you can also create a new value with some fields in common > with an old value, for instance : > (Note que tu peux aussi cr?er une nouvelle valeur partageant un > certain nombre de champs avec une autre valeur d?j? d?clar?e) > > >> contact01 = Adresse "Didier" 51100 "Reims" >> contact02 = contact01 { nom = "Charles" } >> > > Which leads to the practice of using records to create a "default > setting" value where you just modify the field that concern you. > (Ce qui a donn? lieu ? l'idiome qui consiste ? utiliser un type > enregistrement (avec des champs nomm?s) pour les configuration avec > une valeur "r?glage par d?faut" dont on ne modifie que les champs qui > nous int?resse) > > >> main = xmonad defaultConfig {terminal = "konsole"} >> > > contact01 = Adresse "Didier" 51100 "Reims" contact02 = Adresse {nom="Laure", cp=0} contact02 = contact02 { ville="Nogent" } but i get an error : multiple declarations of Main.contact02 So, does it mean i can't add values to contact02. Instead, i must create a contact03 based on contact02 ? Didier. |
Excerpts from legajid's message of Wed Nov 18 15:11:01 -0500 2009:
> I've tried these lines : > contact01 = Adresse "Didier" 51100 "Reims" > contact02 = Adresse {nom="Laure", cp=0} > contact02 = contact02 { ville="Nogent" } > > but i get an error : multiple declarations of Main.contact02 > So, does it mean i can't add values to contact02. Instead, i must create > a contact03 based on contact02 ? Yep; part of the point behind a pure functional language is to not allow mutation by default. Cheers, Edward |
Free forum by Nabble | Edit this page |