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. Why are the types of the result different? (Russ Abbott)
2. Re: Why are the types of the result different? (Brent Yorgey)
3. Guards as extensions of patterns (Russ Abbott)
4. Re: Guards as extensions of patterns (Brent Yorgey)
5. Haskell.Exts parse failed: FlexibleContexts not enabled
(Daniel Lincke)
6. Re: Guards as extensions of patterns (Brandon S Allbery KF8NH)
7. Re: Haskell.Exts parse failed: FlexibleContexts not enabled
(Christian Maeder)
----------------------------------------------------------------------
Message: 1
Date: Wed, 29 Sep 2010 18:19:33 -0700
From: Russ Abbott <[email protected]>
Subject: [Haskell-beginners] Why are the types of the result
different?
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
testWhere1 = a
where b = 1
a = b+1
> testWhere1
2
testWhere2 = a
where b = 1
c = 1/(b - b)
a = b+1
> testWhere2
2.0
The same thing happens with let.
testLet1 = let a = b+1
b = 1
in a
> testLet1
2
testLet2 = let a = b+1
c = 1/(b - b)
b = 1
in a
> testLet2
2.0
-- Russ Abbott
______________________________________
Professor, Computer Science
California State University, Los Angeles
Google voice: 424-242-USA0 (last character is zero)
blog: http://russabbott.blogspot.com/
vita: http://sites.google.com/site/russabbott/
______________________________________
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100929/b82ab3dc/attachment-0001.html
------------------------------
Message: 2
Date: Wed, 29 Sep 2010 22:57:26 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Why are the types of the result
different?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Wed, Sep 29, 2010 at 06:19:33PM -0700, Russ Abbott wrote:
> testWhere1 = a
> where b = 1
> a = b+1
>
> > testWhere1
> 2
>
> testWhere2 = a
> where b = 1
> c = 1/(b - b)
> a = b+1
>
> > testWhere2
> 2.0
Haskell infers the types of a, b, and c *statically*, at compile time.
In the first example, a and b are simply constrained to be any numeric
type. By the type defaulting mechanism, their type is defaulted to
Integer. In the second example, the division operator (/) only
operates on fractional numeric types, so (b - b) must also have a
fractional type. Since the arguments and result of subtraction all
must have the same type, b must also have a fractional type, and hence
so must a since it is defined by addition on b. Types which are
constrained to be fractional but not otherwise constrained are
defaulted to Double.
Note that this all happens statically at compile-time, so the fact
that c is never evaluated is not relevant.
-Brent
------------------------------
Message: 3
Date: Wed, 29 Sep 2010 20:17:31 -0700
From: Russ Abbott <[email protected]>
Subject: [Haskell-beginners] Guards as extensions of patterns
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
I never realized that a guard can be used as an extension of a pattern. Is
this recommended coding? elem n xs asks whether n is an element of xs
elem :: (Eq a) => a -> [a] -> Bool
elem _ [] = False
elem n (x:_) | n == x = True
elem n (_:xs) = elem n xs
-- Russ Abbott
______________________________________
Professor, Computer Science
California State University, Los Angeles
Google voice: 424-242-USA0 (last character is zero)
blog: http://russabbott.blogspot.com/
vita: http://sites.google.com/site/russabbott/
______________________________________
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100929/897464c2/attachment-0001.html
------------------------------
Message: 4
Date: Wed, 29 Sep 2010 23:45:08 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Guards as extensions of patterns
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Wed, Sep 29, 2010 at 08:17:31PM -0700, Russ Abbott wrote:
> I never realized that a guard can be used as an extension of a pattern. Is
> this recommended coding?
Absolutely. Pattern matching and guards can and should be mixed freely.
> elem n xs asks whether n is an element of xs
>
> elem :: (Eq a) => a -> [a] -> Bool
>
> elem _ [] = False
> elem n (x:_) | n == x = True
> elem n (_:xs) = elem n xs
Yes, this works just fine, although personally I would actually write
elem without a guard:
elem _ [] = False
elem n (x:xs) = n == x || elem n xs
This is efficient (it stops as soon as it finds a match) since || is
lazy.
-Brent
------------------------------
Message: 5
Date: Tue, 28 Sep 2010 23:01:45 +0200
From: Daniel Lincke <[email protected]>
Subject: [Haskell-beginners] Haskell.Exts parse failed:
FlexibleContexts not enabled
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi,
when parsing a file containing a function signature with a context like
test :: (Functor f) => ...
I get the follewing failure:
ParseFailed "Malformed context: FlexibleContexts not enabled"
Actually I enables flexible contexts whereever I could ... any ideas?
Greets, daniel
------------------------------
Message: 6
Date: Thu, 30 Sep 2010 00:21:54 -0400
From: Brandon S Allbery KF8NH <[email protected]>
Subject: Re: [Haskell-beginners] Guards as extensions of patterns
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 9/29/10 23:17 , Russ Abbott wrote:
> I never realized that a guard can be used as an extension of a pattern. Is
> this recommended coding? elem n xs asks whether n is an element of xs
Yep (although perhaps not ideal in this particular case). Moreover, the
same things work in case statements (which function definition by patterns
desugar to); IIRC "if c then t else e" is internally converted to "case ()
of () | c -> t | _ -> e".
- --
brandon s. allbery [linux,solaris,freebsd,perl] [email protected]
system administrator [openafs,heimdal,too many hats] [email protected]
electrical and computer engineering, carnegie mellon university KF8NH
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAkykEGIACgkQIn7hlCsL25WeaACguJXxy2EqO0suNG0KxRVBC2aP
aAEAmwXt6sBk9Unb/hbNxPzP16v6NtFS
=sVav
-----END PGP SIGNATURE-----
------------------------------
Message: 7
Date: Thu, 30 Sep 2010 13:11:31 +0200
From: Christian Maeder <[email protected]>
Subject: [Haskell-beginners] Re: Haskell.Exts parse failed:
FlexibleContexts not enabled
To: Daniel Lincke <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Am 28.09.2010 23:01, schrieb Daniel Lincke:
> Hi,
>
> when parsing a file containing a function signature with a context like
> test :: (Functor f) => ...
This is just a plain (haskell 98) context. Can you hlint your file? Does
to go through ghc?
http://hackage.haskell.org/package/hlint uses
http://hackage.haskell.org/package/haskell-src-exts
>
> I get the follewing failure:
> ParseFailed "Malformed context: FlexibleContexts not enabled"
http://darcs.haskell.org/download/docs/6.12.3/html/users_guide/other-type-extensions.html#flexible-contexts
Cheers Christian
>
> Actually I enables flexible contexts whereever I could ... any ideas?
>
> Greets, daniel
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 27, Issue 63
*****************************************