[Haskell-cafe] Re: Trouble with record syntax and classes
Steve Schafer
steve at fenestra.com
Tue Feb 27 02:09:34 EST 2007
On Mon, 26 Feb 2007 23:41:14 -0600 (CST), you wrote:
>Here's my second attempt at the code:
>...
You've left out a bunch of constructors, and there are various other
errors here and there. I think this will do what you want:
> data ISine =
> Sine Integer Integer Integer [Char] |
> MetaSine Integer Integer Integer [ISine]
>
> letter (Sine _ _ _ l) = l
> sub_sines (MetaSine _ _ _ xs) = xs
> period (Sine p _ _ _) = p
> period (MetaSine p _ _ _) = p
> offset (Sine _ o _ _) = o
> offset (MetaSine _ o _ _) = o
> threshold (Sine _ _ t _) = t
> threshold (MetaSine _ _ t _) = t
>
> on :: Integer -> ISine -> Bool
> on time iSine = (mod (time-(offset iSine)) (period iSine)) < (threshold iSine)
>
> act :: Integer -> ISine -> [[Char]]
> act time (MetaSine p o t s) =
> if on time (MetaSine p o t s)
> then foldr1 (++) (map (act time) (sub_sines (MetaSine p o t s)))
> else []
>
> act time (Sine p o t l) =
> if on time (Sine p o t l)
> then [letter (Sine p o t l)]
> else []
But note that you have to write the equations for period, offset and
threshold twice. If you want to avoid that, you can move the
Sine/MetaSine discrimination into the "tail" of the data structure,
something like this:
> data ISineTail =
> SineTail [Char] |
> MetaTail [ISine]
>
> data ISine = ISine Integer Integer Integer ISineTail
>
> letter (ISine _ _ _ (SineTail l)) = l
> sub_sines (ISine _ _ _ (MetaTail xs)) = xs
> period (ISine p _ _ _) = p
> offset (ISine _ o _ _) = o
> threshold (ISine _ _ t _) = t
>
> on :: Integer -> ISine -> Bool
> on time iSine = (mod (time-(offset iSine)) (period iSine)) < (threshold iSine)
>
> act :: Integer -> ISine -> [[Char]]
> act time (ISine p o t (MetaTail s)) =
> if on time (ISine p o t (MetaTail s))
> then foldr1 (++) (map (act time) (sub_sines (ISine p o t (MetaTail s))))
> else []
>
> act time (ISine p o t (SineTail l)) =
> if on time (ISine p o t (SineTail l))
> then [letter (ISine p o t (SineTail l))]
> else []
Steve Schafer
Fenestra Technologies Corp.
http://www.fenestra.com/
More information about the Haskell-Cafe
mailing list