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:  [Haskell-cafe] For class Monoid; better names than
      mempty & mappend might have been: mid (mident) & mbinop
      (Antoine Latter)
   2.  Using intersecting ranges to narrow a range yields an "Out
      of Memory" error (Costello, Roger L.)
   3. Re:  Using intersecting ranges to narrow a range yields an
      "Out of Memory" error (Felipe Almeida Lessa)
   4. Re:  [Haskell-cafe] For class Monoid; better names than
      mempty & mappend might have been: mid (mident) & mbinop
      (Maciej Marcin Piechotka)
   5.  Problem with defining functions and guarded      rules
      (This is still my display name)
   6. Re:  Problem with defining functions and guarded rules
      (Mike Meyer)
   7.  lazyness and tail recursion (Ovidiu Deac)


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

Message: 1
Date: Sun, 24 Jul 2011 15:20:51 -0500
From: Antoine Latter <[email protected]>
Subject: Re: [Haskell-beginners] [Haskell-cafe] For class Monoid;
        better names than mempty & mappend might have been: mid (mident) &
        mbinop
To: KC <[email protected]>
Cc: [email protected], haskell-cafe <[email protected]>
Message-ID:
        <cakjsnqgmkvp9skxbhhymkyur9x9f860jnsqm3h_8o7f5mmc...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Sat, Jul 23, 2011 at 1:41 PM, KC <[email protected]> wrote:
> It would be easier for beginners to "grok".
>

I think that assumes that all beginners have a strong foundation in
algebra. Although it does have the advantage that the names are as
abstract as the class.

Antoine


> --
> --
> Regards,
> KC
>
> _______________________________________________
> Haskell-Cafe mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



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

Message: 2
Date: Sun, 24 Jul 2011 17:26:12 -0400
From: "Costello, Roger L." <[email protected]>
Subject: [Haskell-beginners] Using intersecting ranges to narrow a
        range yields an "Out of Memory" error
To: "[email protected]" <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="us-ascii"

Hi Folks,

I am creating an Int range by starting with the full range of Int:

let r1  =  [minBound::Int .. maxBound::Int]

Then I restrict the upper bound of the range, e.g.,

let r2  =  r1 `intersect` [minBound::Int .. 10]

Then I restrict the lower bound of the range, e.g.,

let r3  =  r2 `intersect` [0 .. maxBound::Int]

No error thus far.

However, when I try to display r3 I get an "Out of Memory" error.

Why is that?

I created a function that restricts a range using the above approach:

restrict                                  ::  [Int] -> Restriction-> [Int]
restrict r (MinInclusive v)    =   r `intersect` [v .. maxBound::Int]
restrict r (MaxInclusive v)   =   r `intersect` [minBound::Int .. v]

The arguments to "restrict" is a range, r, and an indication of whether the 
upper bound is to be restricted (MaxInclusive v) or the lower bound is to be 
restricted (MinInclusive v).

Is there a better way to do this?

What can I do to my restrict function to avoid getting an "Out of Memory" error?

/Roger



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

Message: 3
Date: Sun, 24 Jul 2011 22:56:53 -0300
From: Felipe Almeida Lessa <[email protected]>
Subject: Re: [Haskell-beginners] Using intersecting ranges to narrow a
        range yields an "Out of Memory" error
To: "Costello, Roger L." <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID:
        <CANd=OGF4A=avw_ky4vvoykwhuxekkbomq69bomje2whf39n...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Sun, Jul 24, 2011 at 6:26 PM, Costello, Roger L. <[email protected]> wrote:
> Hi Folks,
>
> I am creating an Int range by starting with the full range of Int:
>
> let r1 ?= ?[minBound::Int .. maxBound::Int]
>
> Then I restrict the upper bound of the range, e.g.,
>
> let r2 ?= ?r1 `intersect` [minBound::Int .. 10]
>
> Then I restrict the lower bound of the range, e.g.,
>
> let r3 ?= ?r2 `intersect` [0 .. maxBound::Int]
>
> No error thus far.
>
> However, when I try to display r3 I get an "Out of Memory" error.
>
> Why is that?

While it is not obvious from the documentation alone, 'intersect' is
not lazy enough.  It is defined as

  intersect xs ys = [x | x <- xs, any (== x) ys]

So, supose you have x:xs and ys such that x ? ys.  Then 'intersect
(x:xs) ys' needs to traverse all of ys before deciding that x really
isn't inside ys.

Now, you're doing 'r2 `intersect` [0 .. maxBound::Int]'.  In this
case, 'x:xs = r2' and 'ys = [0 .. maxBound::Int]'.  The first element
of r2 is minBound, which of course is not in ys.  But to find that
out, 'intersect' would need to traverse the whole 'ys' list.  This
should require at least 32 GiB on GHC with a 32 bit platform, and much
much much more on a 64 bit platform.

> I created a function that restricts a range using the above approach:
>
> restrict ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?:: ?[Int] -> Restriction-> [Int]
> restrict r (MinInclusive v) ? ?= ? r `intersect` [v .. maxBound::Int]
> restrict r (MaxInclusive v) ? = ? r `intersect` [minBound::Int .. v]
>
> The arguments to "restrict" is a range, r, and an indication of whether the 
> upper bound is to be restricted (MaxInclusive v) or the lower bound is to be 
> restricted (MinInclusive v).
>
> Is there a better way to do this?

This will give you the same problems.

> What can I do to my restrict function to avoid getting an "Out of Memory" 
> error?

By explicitly representing intervals, instead of using the list of
elements they contain.  Something like:

  data Interval a = Interval {mini :: a, maxi :: a} deriving (Eq, Ord, Show)
  type IntervalUnion a = [Interval a]

Instead of '[x..y]', use '[Interval x y]'.  Instead of '[a..b] ++
[c..d]', use '[Interval a b, Interval c d]'.

HTH,

-- 
Felipe.



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

Message: 4
Date: Mon, 25 Jul 2011 03:45:56 +0100
From: Maciej Marcin Piechotka <[email protected]>
Subject: Re: [Haskell-beginners] [Haskell-cafe] For class Monoid;
        better names than mempty & mappend might have been: mid (mident) &
        mbinop
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

On Sun, 2011-07-24 at 19:29 +0100, Julian Porter wrote:
> 
> 
> 
> On 24 Jul 2011, at 19:19, KC wrote:
> 
> > I like the following but again "+" denotes addition and not a
> > general
> > binary operation.
> > 
> > 
> > > I personally often define the alias:
> > > 
> > > (<+>) = mappend
> > 
> > A lot of math books use "+" or "x" enclosed in a circle to indicate
> > that the usual meaning of "+" nor "x" is intended for the meaning of
> > the binary operation.
> > 
> 
> 
> Er no.  Both symbols have extremely precise meanings.  $\oplus$ is the
> direct sum of two modules and $\otimes$ is their tensor product.
> 

Well. Notation depends on branch. I've seen routinely used + as addition
in Z_{2,3,...} - for example 2 + 2 = 0.

For example ? have different meaning when you would say about
exponential distribution/radioactive decay and different when you talk
about wavelength. If you are using lambda calculus it have yet another
meaning. And according to http://en.wikipedia.org/wiki/Lambda those are
just 3 of many meanings.

I've seen ? used as binary operation when the other were already used
before I learned that it may denote the direct sum.

Regards
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110725/96d9c559/attachment-0001.pgp>

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

Message: 5
Date: Mon, 25 Jul 2011 05:38:40 +0000
From: This is still my display name <[email protected]>
Subject: [Haskell-beginners] Problem with defining functions and
        guarded rules
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"


Hi, I just started learning Haskell. But I have been having a problem with 
defining functions. For example,diff :: Float-> Float  diff = if x>= y then x-y 
else y-x<interactive>:1:29: parse error on input `='It is rather weird as I 
have checked quite a few websites and this was supposed to work.The same occurs 
when I try for guarded rules, except that it is now for |.diff :: Float -> 
Float -> Floatdiff x y | x >= y = x - y| otherwise = y - x<interactive>:1:42: 
parse error on input `|'Please do help!Thanks a lot.                            
                                                  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110725/4858ef6b/attachment-0001.htm>

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

Message: 6
Date: Mon, 25 Jul 2011 00:02:20 -0700
From: Mike Meyer <[email protected]>
Subject: Re: [Haskell-beginners] Problem with defining functions and
        guarded rules
To: This is still my display name <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

On Mon, 25 Jul 2011 05:38:40 +0000
This is still my display name <[email protected]> wrote:

> 
> Hi, I just started learning Haskell. But I have been having a problem with 
> defining functions. For example,diff :: Float-> Float  diff = if x>= y then 
> x-y else y-x<interactive>:1:29: parse error on input `='It is rather weird as 
> I have checked quite a few websites and this was supposed to work.The same 
> occurs when I try for guarded rules, except that it is now for |.diff :: 
> Float -> Float -> Floatdiff x y | x >= y = x - y| otherwise = y - 
> x<interactive>:1:42: parse error on input `|'Please do help!Thanks a lot.     
>                                                              

You're typing at GHCi, right? You can't create values at the top level
in ghci. You need to either put the things in a file and load them, or
use "let "

Prelude> diff = if x > y then x - y else y - x

<interactive>:1:6: parse error on input `='
Prelude> let diff = if x > y then x - y else y - x

<interactive>:1:15: Not in scope: `x'

<interactive>:1:19: Not in scope: `y'

<interactive>:1:26: Not in scope: `x'

<interactive>:1:30: Not in scope: `y'

<interactive>:1:37: Not in scope: `y'

<interactive>:1:41: Not in scope: `x'
Prelude> let diff x y = if x > y then x - y else y - x
Prelude> diff 20 10
10
Prelude> diff 10 20
10
Prelude> 

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

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



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

Message: 7
Date: Mon, 25 Jul 2011 10:53:20 +0300
From: Ovidiu Deac <[email protected]>
Subject: [Haskell-beginners] lazyness and tail recursion
To: [email protected]
Message-ID:
        <CAKVsE7vs9Cgfa=p4qH1xqA3g1AA2SRKC=egneqjscpmhajf...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

I've read the paragraph below from Real World Haskell
(http://book.realworldhaskell.org/read/efficient-file-processing-regular-expressions-and-file-name-matching.html
section "An important aside: writing lazy functions")

I can get a feeling why lazyness compensates for the lack of tail
recursion but I don't really understand why it would work in general.

My understanding is that that a function which returns a list with
non-tail recursion would only work this way with certain usage
patterns. In this case the caller of the funtion will have to use the
string in a stream-like fashion.

Can someone explain this better?

Thanks,
Ovidiu

PS: Here is the paragraph that I'm talking about:
"Looking at the globToRegex' function, we can see that it is not tail
recursive. To see why, examine its final clause again (several of its
other clauses are structured similarly). No comments

-- file: ch08/GlobRegex.hs
globToRegex' (c:cs) = escape c ++ globToRegex' cs

1 comment

It applies itself recursively, and the result of the recursive
application is used as a parameter to the (++) function. Since the
recursive application isn't the last thing the function does,
globToRegex' is not tail recursive. No comments

Why is our definition of this function not tail recursive? The answer
lies with Haskell's non-strict evaluation strategy. Before we start
talking about that, let's quickly talk about why, in a traditional
language, we'd try to avoid this kind of recursive definition. Here is
a simpler definition, of the (++) operator. It is recursivem, but not
tail recursive. 8 comments

-- file: ch08/append.hs
(++) :: [a] -> [a] -> [a]

(x:xs) ++ ys = x : (xs ++ ys)
[]     ++ ys = ys

No comments

In a strict language, if we evaluate "foo" ++ "bar", the entire list
is constructed, then returned. Non-strict evaluation defers much of
the work until it is needed. No comments

If we demand an element of the expression "foo" ++ "bar", the first
pattern of the function's definition matches, and we return the
expression x : (xs ++ ys). Because the (:) constructor is non-strict,
the evaluation of xs ++ ys can be deferred: we generate more elements
of the result at whatever rate they are demanded. When we generate
more of the result, we will no longer be using x, so the garbage
collector can reclaim it. Since we generate elements of the result on
demand, and do not hold onto parts that we are done with, the compiler
can evaluate our code in constant space."



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

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


End of Beginners Digest, Vol 37, Issue 56
*****************************************

Reply via email to