Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What you have doesn't sound to me like you need a heterogeneous list. Maybe you're looking for something like this?</p> <pre><code>data Foo = Foo Int data Bar = Bar Int data Arg = StringArg String | IntArg Int | DoubleArg Double data Tree = Group Arg Arg [Item] data Item = Item Foo Bar example :: Tree example = Group (StringArg "str") (IntArg 42) [Item (Foo 1) (Bar 2), Item (Foo 12) (Bar 36)] </code></pre> <p>Note that we could even create a list of <code>Arg</code>s of different "sub-types". For example, <code>[StringArg "hello", IntArg 3, DoubleArg 12.0]</code>. It would still be a homogeneous list, though.</p> <p>===== EDIT =====</p> <p>There are a few ways you could handle the "default argument" situation. Suppose the <code>Bar</code> argument in an item is optional. My first thought is that while it may be optional for the user to specify it, when I store the data I want to include the default argument. That way, determining a default is separated from the code that actually does something with it. So, if the user specifies a <code>Foo</code> of 3, but doesn't supply a <code>Bar</code>, and the default is <code>Bar 77</code>, then I create my item as:</p> <pre><code>Item (Foo 3) (Bar 77) </code></pre> <p>This has the advantage that functions that operate on this object don't need to worry about defaults; both parameters will always be present as far as they are concerned.</p> <p>However, if you really want to omit the default arguments in your data structure, you could do somthing like this:</p> <pre><code>data Bar = Bar Int | DefaultBar example = Group (StringArg "str") (IntArg 42) [Item (Foo 1) (Bar 2), Item (Foo 12) DefaultBar] </code></pre> <p>Or even:</p> <pre><code>data Item = Item Foo Bar | ItemWithDefaultBar Foo </code></pre> <p>===== Edit #2 =====</p> <p>So perhaps you could use something like this:</p> <pre><code>data ComplicatedItem = ComplicatedItem { location :: (Double, Double), size :: Int, rotation :: Double, . . . and so on . . . } defaultComplicatedItem = ComplicatedItem { location = (0.0,0.0), size = 1, rotation = 0.0), ... } </code></pre> <p>To create a <code>ComplicatedItem</code>, the user only has to specify the non-default parameters:</p> <pre><code>myComplicatedItem = defaultComplicatedItem { size=3 } </code></pre> <p>If you add new paramters to the <code>ComplicatedItem</code> type, you need to update <code>defaultComplicatedItem</code>, but the definition for <code>myComplicatedItem</code> doesn't change.</p> <p>You could also override the <code>show</code> function so that it omits the default parameters when printing.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload