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:  Antw.:  Question about CFloat and Float (Alberto Ruiz)
   2.  Longest common prefix of a list of lists (philipp siegmantel)
   3. Re:  Longest common prefix of a list of lists (Daniel Fischer)
   4. Re:  Looking for more information about Pattern Matching
      (Mike Meyer)
   5. Re:  Looking for more information about Pattern   Matching
      (Daniel Fischer)
   6. Re:  Longest common prefix of a list of lists (Aai)
   7. Re:  Longest common prefix of a list of lists (Daniel Fischer)


----------------------------------------------------------------------

Message: 1
Date: Fri, 29 Apr 2011 14:20:47 +0200
From: Alberto Ruiz <[email protected]>
Subject: Re: [Haskell-beginners] Antw.:  Question about CFloat and
        Float
To: "[email protected]" <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed

On 04/29/2011 08:20 AM, [email protected] wrote:
> Hi Alberto,
> thanks for the hint and the link. Do you have any information on whether
> that will continue to work in the future? I somehow have a bad feeling
> about it because the C* versions of the types would then be unnecessary.

I don't have this information, but I find it very convenient that Float 
and Double are the standard IEEE 754 floating types. I hope this is not 
changed any time soon... :)

http://en.wikipedia.org/wiki/Floating_point#IEEE_754:_floating_point_in_modern_computers

Alberto

> Thanks again,
> Christian
>
> ----- Reply message -----
> Von: "Alberto Ruiz" <[email protected]>
> An: "C Gosch" <[email protected]>
> Cc: <[email protected]>
> Betreff: [Haskell-beginners] Question about CFloat and Float
> Datum: Do., Apr. 28, 2011 15:27
>
>
> Hi Christian,
>
> On 04/28/2011 02:27 PM, C Gosch wrote:
>  > Dear list members,
>  > I have the following problem: I have wrapped a C library using the FFI
>  > and c2hs.
>  > The C functions all take a bunch of pointers to C arrays of floats,
>  > doubles, or ints as arguments.
>  > Naturally, the Haskell representation of these is e.g.
>  > Ptr CFloat
>  > or, for single values, CFloat.
>  > My question is: if I have an array, e.g. allocated with mallocArray, to
>  > Haskell Float values (Ptr Float),
>  > is the representation of Float exactly the same as for CFloat? Can I
>  > safely use Ptr Float where the C function
>  > expects the C type float* ?
>  > I assume that I can not, and in that case I would like to find a way to
>  > have Float on the Haskell side and CFloat on the C side.
>  > Any hint on how I would do that without copying data and without too
>  > large performance penalties?
>  >
>  > Thanks a lot,
>  > Christian
>  >
>
> I think that in ghc you can assume the same representation for CFloat
> and CDouble, they are newtypes of Float and Double:
>
> http://www.haskell.org/ghc/docs/7.0.3/html/libraries/base-4.3.1.0/Foreign-C-Types.html#g:4
>
> (I directly use arrays of Float and Double with C libraries without any
> problem). But Int may be different from CInt depending on the
> architecture, so we need explicit conversions.
>
> Alberto




------------------------------

Message: 2
Date: Fri, 29 Apr 2011 16:07:00 +0200
From: philipp siegmantel <[email protected]>
Subject: [Haskell-beginners] Longest common prefix of a list of lists
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi,

For one of my toy project I had to find the longest common prefix of a list
of strings.
I figured, that this function should only work on list-structure, meaning it
should have type

> longestCommonPrefix :: [[a]] -> [a]

Sadly the best I could come up with was using explicit recursion. It works
as far as I tested it, but I have the feeling that there's a better way to
do it.
Here is the code:

> longestCommonPrefix :: Eq a => [[a]] -> [a]
> longestCommonPrefix []   = []
> longestCommonPrefix xss | any null xss = []
>                                        | otherwise    = loop xss []
>  where loop ::Eq b => [[b]] -> [b] -> [b]
>            loop xss acc =
>              let xs = concatMap (take 1) xss
>              in if any (\x -> x /= head xs) (tail xs)
>                  then reverse acc
>                  else loop (map tail xss) (head xs : acc)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110429/28412382/attachment-0001.htm>

------------------------------

Message: 3
Date: Fri, 29 Apr 2011 16:32:39 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Longest common prefix of a list of
        lists
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="utf-8"

On Friday 29 April 2011 16:07:00, philipp siegmantel wrote:
> Hi,
> 
> For one of my toy project I had to find the longest common prefix of a
> list of strings.
> I figured, that this function should only work on list-structure,
> meaning it should have type
> 
> > longestCommonPrefix :: [[a]] -> [a]
> 
> Sadly the best I could come up with was using explicit recursion. It
> works as far as I tested it, but I have the feeling that there's a
> better way to do it.
> 
> Here is the code:
> > longestCommonPrefix :: Eq a => [[a]] -> [a]
> > longestCommonPrefix []   = []
> > longestCommonPrefix xss | any null xss = []
> > 
> >                                        | otherwise    = loop xss []
> >  
> >  where loop ::Eq b => [[b]] -> [b] -> [b]
> >  
> >            loop xss acc =
> >            
> >              let xs = concatMap (take 1) xss
> >              in if any (\x -> x /= head xs) (tail xs)
> >              
> >                  then reverse acc
> >                  else loop (map tail xss) (head xs : acc)

A different point of view:

given at least two lists, lists@(list1:more@(list2:_)), the longest common 
prefix of lists is the common prefix of list1 and the longest common prefix 
of more, so

longestCommonPrefix :: Eq a => [[a]] -> [a]
longestCommonPrefix [] = []  -- what else?
longestCommonPrefix lists = foldr1 commonPrefix lists

-- to get the common prefix of two lists, we use explicit recursion, I 
don't see an elegant way to avoid that.

commonPrefix :: Eq a => [a] -> [a] -> [a]
commonPrefix (x:xs) (y:ys)
    | x == y    = x : commonPrefix xs ys
commonPrefix _ _ = []

produces the result incrementally.



------------------------------

Message: 4
Date: Fri, 29 Apr 2011 12:43:51 -0400
From: Mike Meyer <[email protected]>
Subject: Re: [Haskell-beginners] Looking for more information about
        Pattern Matching
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

On Fri, 29 Apr 2011 03:27:48 +0200
Daniel Fischer <[email protected]> wrote:
> On Friday 29 April 2011 02:35:13, Graham Hopper wrote:
> Basically, a pattern is
[...]
> Or
> 
> - a constructor application, (Con arg1 arg2), (x:xs), (a,b,c,d), ((:) x xs)
[...]
> Also there are 
[...]

I've been wondering about this case. Since : is "just another
function", then if things were consistent, I ought to be able to use
other functions in constructor applications. But my attempts to do so
always seem to result in syntax errors.

So my question is - what are the rules for constructor applications?
Is the list above complete? Are there just a few more? Is there some
rule to determine which functions are ok in patterns and which aren't?

     Thanks,
     <mike
-- 
Mike Meyer <[email protected]>             http://www.mired.org/consulting.html
Independent Software developer/SCM consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org



------------------------------

Message: 5
Date: Fri, 29 Apr 2011 20:00:38 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Looking for more information about
        Pattern Matching
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="iso-8859-1"

On Friday 29 April 2011 18:43:51, Mike Meyer wrote:
> On Fri, 29 Apr 2011 03:27:48 +0200
> 
> Daniel Fischer <[email protected]> wrote:
> > On Friday 29 April 2011 02:35:13, Graham Hopper wrote:
> > Basically, a pattern is
> 
> [...]
> 
> > Or
> > 
> > - a constructor application, (Con arg1 arg2), (x:xs), (a,b,c,d), ((:)
> > x xs)
> 
> [...]
> 
> > Also there are
> 
> [...]
> 
> I've been wondering about this case. Since : is "just another
> function",

It's "just another function" in the same sense as Just or Right are "just 
another function".

They all are (non-nullary) constructors.
They are "just another function" in the sense that they take arguments and 
produce values, and you can pass them to any higher order function like 
every other function (that is, if the type matches) or have them as results 
of other functions.

They are however special functions in the sense that, as they are 
constructors, you can pattern-match on them (fully applied, you can't do

foo :: (Char -> Either Char Int) -> Bool
foo Left = True
foo _ = False
).

> then if things were consistent, I ought to be able to use
> other functions in constructor applications. But my attempts to do so
> always seem to result in syntax errors.
> 
> So my question is - what are the rules for constructor applications?
> Is the list above complete?

I think this time I haven't forgotten anything, but don't be too sure, 
better check the report.

> Are there just a few more? Is there some
> rule to determine which functions are ok in patterns and which aren't?

You can match on constructor applications, nothing else, except for the few 
special cases,

- number literals (sugar for an equality test)
- character literals ('a' is not really a constructor, matching on 
character literals also gives rise to an equality test)

- string literals ("foo")
- list patterns ([x1,x2,x3])

Special syntax for convenience, reduces to ('f' : 'o' : 'o' : []) resp. (x1 
: x2 : x3 : []).

- tuples are constructor applications, but with special (wired-in) syntax, 
so those patterns don't match the normal cases of

Con pat1 pat2 ... patN  -- prefix constructor application
pat1 :$%& pat2    -- infix constructor application

- labelled patterns, Con{ field1 = pat1, ..., fieldN = patN } (N >= 0)

You can tell constructors apart from "ordinary functions" by case, if the 
name begins with an upper-case letter, it's a constructor, if it begins 
with a lower-case letter or an underscore, it's an "ordinary function".

For operators, the rule is the same if you regard ':' as an upper-case-
symbol (and the only one); an infix operator is a constructor if and only 
if it starts with ':'.

So, (x : xs), infix application of the operator (:), : obviously starts 
with ':', hence a constructor ~> valid pattern.
(x && y); infix application of the operator (&&), && doesn't start with 
':', no constructor ~> not a valid pattern.

> 
>      Thanks,
>      <mike



------------------------------

Message: 6
Date: Fri, 29 Apr 2011 20:17:27 +0200
From: Aai <[email protected]>
Subject: Re: [Haskell-beginners] Longest common prefix of a list of
        lists
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

I don't know for sure if the following is elegant or not, but:

a = "aaabbbbbbcccccc"
b = "aaabbbccccccd"
c = "aaabbbbbbbbbccccffffdd"

takeCommonPrefix xs =  flip take xs. length . filter (`isPrefixOf`xs). drop 1. 
inits

*Main> foldl1 takeCommomPrefix  [a,b,c]
"aaabbb"

*Main> foldl1 takeCommonPrefix  [a,b,c,""]
""

May be the use of 'length' is a bit ugly. :-)



Hallo Daniel Fischer, je schreef op 29-04-11 16:32:
>
> -- to get the common prefix of two lists, we use explicit recursion, I
> don't see an elegant way to avoid that.
>

-- 
Met vriendelijke groet,
=@@i




------------------------------

Message: 7
Date: Fri, 29 Apr 2011 21:15:13 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Longest common prefix of a list of
        lists
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="iso-8859-1"

On Friday 29 April 2011 20:17:27, Aai wrote:
> I don't know for sure if the following is elegant or not, but:

In a way, yes. It's a composition of library functions, so you delegate the 
explicit recursion to those. There are a few points where you can increase 
elegance and performance here, see inline comments.

But it's very inefficient and the suggestions don't change that.

> 
> a = "aaabbbbbbcccccc"
> b = "aaabbbccccccd"
> c = "aaabbbbbbbbbccccffffdd"
> 
> takeCommonPrefix xs =
>   flip take xs. length . filter (`isPrefixOf`xs) . drop 1. inits

Don't use filter here, as soon as you hit an initial segment which isn't a 
prefix of xs, no later one will be, so it's a job for takeWhile.

Also, you already have the common prefixes, so why count and take from xs?
Just take the longest:

takeCommonPrefix xs = last . takeWhile (`isPrefixOf` xs) . inits

> 
> *Main> foldl1 takeCommomPrefix  [a,b,c]
> "aaabbb"
> 
> *Main> foldl1 takeCommonPrefix  [a,b,c,""]
> ""
> 
> May be the use of 'length' is a bit ugly. :-)

Yes, and unneeded. But perforance-wise, that is not what hurts.

The trouble is, if xs and ys have a longest common prefix of length p, you 
check whether (take k ys) is a prefix of xs for k <- [0 .. p+1], so that is 
an O(p^2) algorithm. Also, you can't start delivering until the longest 
common prefix has been determined, which may cause high memory requirements 
and bad performance.

Consider

a = replicate 1000000 'a'
b = replicate 1000001 'a'
c = "abacab"

foldl1 takeCommonPrefix [a,b,c] =
    takeCommonPrefix ab c
      where
        ab = takeCommonPrefix a b

If the common prefix is delivered incrementally, all we need is the first 
two characters to determine the result. That's fast and needs very little 
memory. If the common prefix has to be entirely determined before it can be 
delivered, you need to have two one-million characters long Strings in 
memory at the same time, which needs a couple dozen MB, and to determine 
the longest common prefix of a and b by the above method, you need about 
5*10^11 Char comparisons, so it'll need a bit of time too.

> 
> Hallo Daniel Fischer, je schreef op 29-04-11 16:32:
> > -- to get the common prefix of two lists, we use explicit recursion, I
> > don't see an elegant way to avoid that.



------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 34, Issue 42
*****************************************

Reply via email to