Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Help in pattern matching (Magnus Therning)
2. Translating a while (Nicolas Couture-Grenier)
3. Re: Help in pattern matching (Stephen Tetley)
4. Re: Help in pattern matching (Stephen Tetley)
5. Re: Translating a while (Daniel Fischer)
6. Re: Translating a while (Nicolas Couture-Grenier)
7. Re: Translating a while (Yitzchak Gale)
----------------------------------------------------------------------
Message: 1
Date: Mon, 8 Mar 2010 12:06:48 +0000
From: Magnus Therning <[email protected]>
Subject: Re: [Haskell-beginners] Help in pattern matching
To: Stephen Tetley <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
On Mon, Mar 8, 2010 at 11:28, Stephen Tetley <[email protected]> wrote:
> Hi Magnus
>
> That's not quite equivalent to Joe Fox's original code, although it is
> an improvement as test in the original won't type check:
>
> original...
>
> test (DataConst1 x y) = x  -- returns Int
> test (DataConst2 x y) = x  -- returns Int
> test (DataConst3 x y) = y  -- returns String
> test (DataConst4 x y) = y  -- returns Int
>
>
> This one does type check...
>
> test2 :: TestData -> Either Int String
> test2 (DataConst1 x y) = Left x
> test2 (DataConst2 x y) = Left x
> test2 (DataConst3 x y) = Right y
> test2 (DataConst4 x y) = Left y
I don't see where it's different from my code (i.e. modulo the
ordering of 'Either ...').
Now I may be missing something, but defining a conversion function and
running smallcheck (or I guess quickcheck) shows that
'prop_test2EqTestM' holds (I took the liberty of swapping the Either
around compared to your 'test2' above):
data TestData = DataConst1 Int String
| DataConst2 Int Int
| DataConst3 String String
| DataConst4 String Int
test2 (DataConst1 x _) = Right x
test2 (DataConst2 x _) = Right x
test2 (DataConst3 _ y) = Left y
test2 (DataConst4 _ y) = Right y
type TestDataM = (Either String Int, Either String Int)
testM :: TestDataM -> Either String Int
testM (x@(Right _), _) = x
testM (Left _, y) = y
conv2Tuple :: TestDataM -> TestData
conv2Tuple (Right x, Left y) = (DataConst1 x y)
conv2Tuple (Right x, Right y) = (DataConst2 x y)
conv2Tuple (Left x, Left y) = (DataConst3 x y)
conv2Tuple (Left x, Right y) = (DataConst4 x y)
prop_test2EqTestM tdm = let
td = conv2Tuple tdm
in
test2 td == testM tdm
What am I missing?
/M
--
Magnus Therning (OpenPGP: 0xAB4DFBA4)
magnusï¼ therningï¼org Jabber: magnusï¼ therningï¼org
http://therning.org/magnus identi.ca|twitter: magthe
------------------------------
Message: 2
Date: Mon, 8 Mar 2010 08:49:15 -0500
From: Nicolas Couture-Grenier <[email protected]>
Subject: [Haskell-beginners] Translating a while
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
I'm learning Haskell and I'm trying to translate a pseudocode algorithm to
generate the next permutation from a previous permutation.
A permutation is a list of n numbers (let's call it a) in {1 .. n} appearing
once in arbitrary order.
The first step is to find the largest index j in the list for which a[j] <
a[j+1].
The pseudocode is simple:
j:= n-1
while a[j] > a[j+1]
j:=j-1
I've coded a haskell function to do this, but it is much uglier than the
pseudocode :
j :: Integral a => [a] -> Int
j [] = 0
j xs = if (head (tail (reverse xs)) < last xs)
then (length xs)-2
else j (take (length xs - 1) xs)
Does anyone has a more elegant solution for this first step?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100308/596fe75c/attachment-0001.html
------------------------------
Message: 3
Date: Mon, 8 Mar 2010 14:14:09 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Help in pattern matching
To: Joe Fox <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Joe - I've cc'ed back to list...
I'm supposing from what you've said that only DataConst1 & DataConst2
have ids - so I've made a type synonym VId
type VId = Int
data TestData = DataConst1 VId String
| DataConst2 VId Int
| DataConst3 Sting String
| DataConst4 String Int
If both DataConst1 & DataConst2 have VId's the you right - you have to
pattern match for both:
setVid_to_4 :: TestData -> TestData
setVid_to_4 (DataConst1 _ y) = DataConst1 4 y
setVid_to_4 (DataConst2 _ y) = DataConst2 4 y
setVid_to_4 var = var
If all the cases of TestData had a VId e.g.:
data TestData = DataConst1 VId String
| DataConst2 VId Int
| DataConst3 VId Sting String
| DataConst4 Vid String Int
... then you could factor out VId to get these two data types
data FactoredTD = FactoredTD VId TD
data TD = DC1 String
| DC2 Int
| DC3 String String
| DC4 String Int
setVid_to_4 :: FactoredTD -> FactoredTD
setVid_to_4 (FactoredTD _ y) = FactoredTD 4 y
This factoring transformation is quite common.
For your particular data type you could factor the other way:
data TestDataX = DataConst1_or_2 VId (Either String Int)
| DataConst3 Sting String
| DataConst4 String Int
setVid_to_4 :: TestDataX -> TestDataX
setVid_to_4 (DataConst1_or_2 _ y) = DataConst1_or_2 4 y
setVid_to_4 y = y
However this factoring is quite horrible - I can't think of anywhere
where I've seen it.
Generally I wouldn't be too concerned about redundancy in pattern
matching. If its easy to consistently name all the constructors in an
algebraic data type, then that's a strong indication that you've put
it into a good form.
If you're still taxed by duplicate pattern matching you can then write
projection and modification functions to do it once and once only:
-- Has to return a Maybe as DC3 and DC4 have no ID
getVId :: TestData -> Maybe VId
getVid (DataConst1 iden _) = Just iden
getVid (DataConst2 iden _) = Just iden
getVid _ = Nothing
-- note - uses an update function that accesses the original value
-- rather than a simple replace
--
updateVid :: (Vid -> Vid) -> TestData -> TestData
updateVid f (DataConst1 iden y) = DataConst1 (f iden) y
updateVid f (DataConst2 iden y) = DataConst2 (f iden) y
updateVid f var = var
incrementVid :: TestData -> TestData
incrementVid = updateVid (\x -> x+1)
Best wishes
Stephen
On 8 March 2010 12:19, Joe Fox <[email protected]> wrote:
> Hi Guys,
>
> First thanks for all yours inputs.
>
> Actually test is just a simple function , actually my actual function is
> pretty complex and huge ...
>
> let me redefine Test function , i guess i took a bad example which created
> lot of confusion
>
> the type of the test functions will be something like this , (i don't have
> any ambiguity on type of the function, )
>
> test:: TestData -> TestData
>
> My question is like is there any way i can match multiple pattern in a
> function statement .
>
> like this (I know this doesnt work , but just trying to explain whats on my
> mind)
>
>
> test var@(DataConst1 x y) == var@(DataConst2 x y) = var{id=4} -- let's say
> id is the record holder for first record
> test var@(_) = var --- any for other two its returns the same
>
> the working code would be like
> test var@(DataConst1 x y) = var{id=4}
> test var@(DataConst2 x y) = var{id=4} --- I want these two lines in one
> line, because the logic is same
> test var@(_) = var
>
> some thing of this sort.
> I hope i didnt add to the confusion
>
>
> Thanks
> Joe
>
------------------------------
Message: 4
Date: Mon, 8 Mar 2010 14:16:45 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Help in pattern matching
To: Magnus Therning <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
On 8 March 2010 12:06, Magnus Therning <[email protected]> wrote:
[SNIP]
>
> What am I missing?
>
Hi Magnus
I was restating what Jonas mentioned but didn't elaborate - that Joe's
original program had a type error.
The solutions posted did two things:
1. Rewrite Joe's original (algebraic) data type to encode it with
products (aka pair - (,) ) and sums (Either) - this allowed some of
the pattern matching to be factored, reducing the number of cases in
the function test from 4 to 2
2. Correct the answer type to use a proper sum (Either Int String),
where previously some of declarations of test returned Int others
String (and so wouldn't type check).
Strictly speaking your program wasn't "equivalent" to the original
because it did step 2 and would actually work!
Best wishes
Stephen
------------------------------
Message: 5
Date: Mon, 8 Mar 2010 15:29:32 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Translating a while
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Am Montag 08 März 2010 14:49:15 schrieb Nicolas Couture-Grenier:
> I'm learning Haskell and I'm trying to translate a pseudocode algorithm
> to generate the next permutation from a previous permutation.
Don't try to translate it directly. In Haskell, generally a different
approach than for imperative (pseudo-) code is better.
>
> A permutation is a list of n numbers (let's call it a) in {1 .. n}
> appearing once in arbitrary order.
>
> The first step is to find the largest index j in the list for which a[j]
> < a[j+1].
>
> The pseudocode is simple:
>
> j:= n-1
>
> while a[j] > a[j+1]
> j:=j-1
>
>
> I've coded a haskell function to do this, but it is much uglier than the
> pseudocode :
It's not appropriate for lists, therefore, it's ugly. You can work with
arrays and have a fairly direct correspondence:
import Data.Array
fun :: Array Int Int -> Int
fun a = go (hi-1)
where
(lo,hi) = bounds a
go i
| i < lo = i
| a!i > a!(i+1) = go (i-1)
| otherwise = i
The local "go" is our while-loop, additionally, it checks that we don't
fall off the front of the array.
When working with lists, one would typically not produce the next
permutation from the previous, but generate the list of all permutations
(take a look at the code of "permutations" in Data.List).
>
> j :: Integral a => [a] -> Int
> j [] = 0
> j xs = if (head (tail (reverse xs)) < last xs)
> then (length xs)-2
> else j (take (length xs - 1) xs)
>
>
> Does anyone has a more elegant solution for this first step?
------------------------------
Message: 6
Date: Mon, 8 Mar 2010 09:37:08 -0500
From: Nicolas Couture-Grenier <[email protected]>
Subject: Re: [Haskell-beginners] Translating a while
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
I hadn't noticed the permutation function. It's not listed in the synopis...
Nice.
Anyway, I found a better way around my little exercise.
import List
import Maybe
j :: Integral a => [a] -> Int
j xs = (fromMaybe 0 (findIndex (==False) (zipWith (<) (init xs) (tail xs))))
- 1
On Mon, Mar 8, 2010 at 9:29 AM, Daniel Fischer <[email protected]>wrote:
> Am Montag 08 März 2010 14:49:15 schrieb Nicolas Couture-Grenier:
> > I'm learning Haskell and I'm trying to translate a pseudocode algorithm
> > to generate the next permutation from a previous permutation.
>
> Don't try to translate it directly. In Haskell, generally a different
> approach than for imperative (pseudo-) code is better.
>
> >
> > A permutation is a list of n numbers (let's call it a) in {1 .. n}
> > appearing once in arbitrary order.
> >
> > The first step is to find the largest index j in the list for which a[j]
> > < a[j+1].
> >
> > The pseudocode is simple:
> >
> > j:= n-1
> >
> > while a[j] > a[j+1]
> > j:=j-1
> >
> >
> > I've coded a haskell function to do this, but it is much uglier than the
> > pseudocode :
>
> It's not appropriate for lists, therefore, it's ugly. You can work with
> arrays and have a fairly direct correspondence:
>
> import Data.Array
>
> fun :: Array Int Int -> Int
> fun a = go (hi-1)
> where
> (lo,hi) = bounds a
> go i
> | i < lo = i
> | a!i > a!(i+1) = go (i-1)
> | otherwise = i
>
> The local "go" is our while-loop, additionally, it checks that we don't
> fall off the front of the array.
>
> When working with lists, one would typically not produce the next
> permutation from the previous, but generate the list of all permutations
> (take a look at the code of "permutations" in Data.List).
>
> >
> > j :: Integral a => [a] -> Int
> > j [] = 0
> > j xs = if (head (tail (reverse xs)) < last xs)
> > then (length xs)-2
> > else j (take (length xs - 1) xs)
> >
> >
> > Does anyone has a more elegant solution for this first step?
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100308/d34126b8/attachment-0001.html
------------------------------
Message: 7
Date: Mon, 8 Mar 2010 16:38:27 +0200
From: Yitzchak Gale <[email protected]>
Subject: Re: [Haskell-beginners] Translating a while
To: Nicolas Couture-Grenier <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Nicolas Couture-Grenier <[email protected]> wrote:
> The first step is to find the largest index j in the list for which a[j] <
> a[j+1].
> The pseudocode is simple...
The pseudo-code represents how you would write this
algorithm in an imperative language. If you take the whole
algorithm and express it naturally in Haskell, that would likely
not be the first step at all.
But in any case, I might write that version of the first step
like this:
j xs = maybeLast . filter snd . zip [0..] $ zipWith (<) xs (drop 1 xs)
where
maybeLast [] = Nothing
maybeLast xs = Just (last xs)
Notice my use of the Maybe type - there might not be
any such index j at all.
Regards,
Yitz
>
> j:= n-1
>
> while a[j] > a[j+1]
> j:=j-1
>
>
> I've coded a haskell function to do this, but it is much uglier than the
> pseudocode :
>
> j :: Integral a => [a] -> Int
> j [] = 0
> j xs = if (head (tail (reverse xs)) < last xs)
> then (length xs)-2
> else j (take (length xs - 1) xs)
>
>
> Does anyone has a more elegant solution for this first step?
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 21, Issue 6
****************************************