[Haskell-beginners] In Search Of a clue... (Defining and making use of a type)
David McBride
toad3k at gmail.com
Mon Dec 12 23:02:05 CET 2011
Your main problem and the only reason I can see why what you have
wouldn't work is that you have created a list of ScrUple's, but then
you tried to access it with [1] notation, which works in like every
other language, but not haskell. That notation is for creating a
list, so when you type [1] that is basically of type Integral n =>
[n], so ghc has no clue what you are trying to do and blows up. The
way to access by index is to use the !! operator. You will have to
import Data.List to get that.
main = putStrLn $ show $ sc1 !! 1
should work. Just keep in mind that it is unsafe in that
On Mon, Dec 12, 2011 at 4:31 PM, Allen S. Rout <asr at ufl.edu> wrote:
>
>
> So, I'm approaching a problem which I think I understand pretty well.
> I'm a novice to Haskell, though, and I'm having difficulty even
> getting started with e.g. data types and building more complex
> structures.
>
> Here's what I'm trying to do; this is an xmonad
> customization/extension.
>
> My end goal is thus:
>
> Innermost: a 'Screen Tuple' , which I'm calling a 'ScrUple'. This
> represents a statement like 'screen 0 is displaying workspace "mail"'.
>
> Next: A variable-length list of these, I'm calling 'ScrConfig'. It
> means something like 'display this workspace on screen 0, that one on
> 1, etc'.
>
> Next: [ haven't gotten here ] a hash, or something: pairs of 'label :
> ScrConfig'. I don't know if the most haskelly way to do that is to
> build another type, and then another aggregate of that type..
>
>
> In PERL, it'd be something vaguely like:
>
> $configs =
> {
> 'initial' => {
> \(0,"mail"),
> \(1,"web"),
> \(2,"jabber")
> }
>
> 'project' => {
> \(4,"editor"),
> \(1,"compile"),
> \(2,"jabber")
> }
>
> };
>
>
> Here's what I'm doing so far,
>
> ----------------
>
> data ScrUple = ScrUple { xineramascreen :: Integer
> , workspace :: String
> } deriving (Show)
>
> data ScrConfig = ScrConfig [ ScrUple ] deriving (Show)
>
>
>
> s1 = ScrUple 0 "mail"
> s2 = ScrUple 1 "web"
>
>
> ScrConfig sc1 =ScrConfig( [s2 s1] ) ;
>
> main = putStrLn $ show sc1[1]
>
> ------------------
>
> and I get errors like:
>
> play.hs:17:27:
> Couldn't match expected type `ScrUple -> ScrUple'
> against inferred type `ScrUple'
> In the expression: s2 s1
> In the first argument of `ScrConfig', namely `([s2 s1])'
> In the expression: ScrConfig ([s2 s1])
>
> play.hs:21:19:
> Couldn't match expected type `[t] -> String'
> against inferred type `String'
> In the second argument of `($)', namely `show sc1 [1]'
> In the expression: putStrLn $ show sc1 [1]
> In the definition of `main': main = putStrLn $ show sc1 [1]
>
> -------------------
>
>
> ... I feel like I'm thinking about the problem wrong, because this
> kind of "Here's how you build up a data structure" doesn't seem to be
> in the tutorials. I've been working through LYAH and Gentle
> Introduction, but so far haven't found things that feel related.
>
>
> I'd be delighted with pointers to the right parts of the Fine Manual,
> and similarly pleased with discursion on how to think about this data
> storage problem from a haskelly point of view.
>
> - Allen S. Rout
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
More information about the Beginners
mailing list