I've looked all around but haven't found a nice syntax for this -- anyone have suggestions?: What I'd like to do is assign toplevel variable names to inner elements of a structure. Pseudocode for what I'd like to do is: things = [ foo@"thing1" , bar@"thing2" ] The major difference from just `foo = "thing1" ; things = [foo, ...` is that it's much more visually clear which values are in `things` (imagine I create a `baz` but forget to add it to `things`). The best I've found (which is not at all ideal) is to say e.g.: things@ [ foo , bar , baz ] = ["thing1", "thing2", "thing3"] But of course, for starters, I run off the page very quickly. I don't see anything in the usual places (e.g. the Haskell Report), but has anyone come across this and found an elegant solution? Thanks! Tom
On Dec 30, 2015 10:05 PM, <amindfv@gmail.com> wrote:
The major difference from just `foo = "thing1" ; things = [foo, ...` is that it's much more visually clear which values are in `things` (imagine I create a `baz` but forget to add it to `things`).
The best I've found is to use `where` profusely: ``` things = [ foo , bar , baz ] where foo = _ bar = _ baz = _ ``` If you forget to define `baz` but include it in the list, you get an error for a name not bound, and if you define it but forget to include it in the list, you get an error with `-Wall -Werror` for an unused binding. Now, if you want some of them at the top level, you do this ugly thing: ``` ( foo, bar, things ) = ( foo, bar, things ) where things = [ foo , bar , baz ] foo = _ bar = _ baz = _ ``` Just don't get the order wrong on the tuples for things with the same types. Welp! It's a bit less awful if you always want all the items in e. g. a list exported to the top level, as in your example, but this is a bit more general. It's still awful, but using tuples instead of list patterns allows you heterogeneous toplevel exports and more structures than just lists. You could also define a record: ``` {-# LANGUAGE RecordWildCards #-} data R = R { foo :: {- something -} , bar :: {- something else -} , things :: {- stuff -} } R {..} = R {..} where foo = _ bar = _ baz = _ things = [ foo , bar , baz ] ``` This costs you explicit type signatures in the record declaration, but `-Wall` would ask for those on toplevel bindings anyway. I don't think this could be solved easily with syntax; consider ``` things x = foo@(x, x + 1) ``` `foo` can't be made toplevel easily.
participants (2)
-
amindfv@gmail.com -
Manuel Gómez