
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
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
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"}
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 ? 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
participants (3)
-
Chaddaï Fouché
-
Edward Z. Yang
-
legajid