Yet Another Haskell Tutorial Question

I've been working through the "Yet Anther Haskell Tutorial" at http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf , and I've gotten up to Excercise 4.6. It says: === YAHT === Write a datatype Tuple which can hold one, two, three or four elements, depending on the constructor (that is, there should be four constructors, one for each number of arguments). Also provide functions tuple1 through tuple4 which take a tuple and return Just the value in that position, or Nothing if the number is invalid (i.e., you ask for the tuple4 on a tuple holding only two elements). === /YAHT === I've got a brute-force answer, but I'm sure that there's a better way, and I can't find it. The code I've currently got is at http://paste.lisp.org/display/27806 . Anybody want to point me in the right direction? Thanks Bill Mill bill.mill at gmail.com

On Wed, Oct 11, 2006 at 09:16:57PM -0400, Bill Mill wrote:
I've been working through the "Yet Anther Haskell Tutorial" at http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf , and I've gotten up to Excercise 4.6. It says:
Have you checked the solution? Page 167. Tamas

On 10/11/06, Tamas K Papp
On Wed, Oct 11, 2006 at 09:16:57PM -0400, Bill Mill wrote:
I've been working through the "Yet Anther Haskell Tutorial" at http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf , and I've gotten up to Excercise 4.6. It says:
Have you checked the solution? Page 167.
Tamas
Yeah... sorry 'bout that. Thanks Bill

That's about right, though you could shorten a few of the definitions
by matching with the wildcard pattern _ like this:
tuple4 :: Tuple a b c d -> Maybe d
tuple4 (QuadrupleTuple a b c d) = Just d
tuple4 _ = Nothing
It's also possible to use case:
tuple3 :: Tuple a b c d -> Maybe d
tuple3 x = case x of
(QuadrupleTuple a b c d) -> c
(TripleTuple a b c) -> c
_ -> Nothing
On 11/10/06, Bill Mill
I've been working through the "Yet Anther Haskell Tutorial" at http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf , and I've gotten up to Excercise 4.6. It says:
=== YAHT === Write a datatype Tuple which can hold one, two, three or four elements, depending on the constructor (that is, there should be four constructors, one for each number of arguments). Also provide functions tuple1 through tuple4 which take a tuple and return Just the value in that position, or Nothing if the number is invalid (i.e., you ask for the tuple4 on a tuple holding only two elements). === /YAHT ===
I've got a brute-force answer, but I'm sure that there's a better way, and I can't find it. The code I've currently got is at http://paste.lisp.org/display/27806 .
Anybody want to point me in the right direction?
Thanks Bill Mill bill.mill at gmail.com _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (3)
-
Bill Mill
-
Cale Gibbard
-
Tamas K Papp