A very counterintuitive behaviour of Haskell

In studying Haskell, I produced the following output from GHC: xxx-3:~ xxx$ GHCi GHCi, version 6.12.3: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Loading package ffi-1.0 ... linking ... done. Prelude> let f 7 = "ok" Prelude> let f x = "no" Prelude> f 3 "no" Prelude> f 7 "no" I suppose it is correct. However, for someone who is interested in the language, it seems very counterintuitive... Somebody would be so kind to explain to a neophyte this "feature" of the language? Thank you very much. Renzo

On Thu, Jan 27, 2011 at 14:55, Renzo Orsini
In studying Haskell, I produced the following output from GHC:
xxx-3:~ xxx$ GHCi GHCi, version 6.12.3: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Loading package ffi-1.0 ... linking ... done. Prelude> let f 7 = "ok" Prelude> let f x = "no" Prelude> f 3 "no" Prelude> f 7 "no"
I suppose it is correct. However, for someone who is interested in the language, it seems very counterintuitive... Somebody would be so kind to explain to a neophyte this "feature" of the language?
I suppose it comes down to pattern matching being on form, not on value. /M -- Magnus Therning OpenPGP: 0xAB4DFBA4 email: magnus@therning.org jabber: magnus@therning.org twitter: magthe http://therning.org/magnus

On Thu, Jan 27, 2011 at 15:02, Magnus Therning
On Thu, Jan 27, 2011 at 14:55, Renzo Orsini
wrote: In studying Haskell, I produced the following output from GHC:
xxx-3:~ xxx$ GHCi GHCi, version 6.12.3: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Loading package ffi-1.0 ... linking ... done. Prelude> let f 7 = "ok" Prelude> let f x = "no" Prelude> f 3 "no" Prelude> f 7 "no"
I suppose it is correct. However, for someone who is interested in the language, it seems very counterintuitive... Somebody would be so kind to explain to a neophyte this "feature" of the language?
I suppose it comes down to pattern matching being on form, not on value.
What an amazingly wrong answer by me, sorry for the noise ;-) /M -- Magnus Therning OpenPGP: 0xAB4DFBA4 email: magnus@therning.org jabber: magnus@therning.org twitter: magthe http://therning.org/magnus

Good morning, I need to download SqlLite and then use it with Haskell in GHC environment... I'm using the book "Real World Haskell" as a guide... Do you have any advice in regard to the download onto a Windows pc? Thanks

First off, by doing what you just did, starting a new thread by replying to a message belonging to an unrelated thread, you reduce the chance of people reading your emails. People using a mail client that supports threading, or using gmail are likely to completely miss that your email is unrelated to the current thread. On 28/01/11 17:42, patricklynch wrote:
Good morning,
I need to download SqlLite and then use it with Haskell in GHC environment...
I'm using the book "Real World Haskell" as a guide...
Do you have any advice in regard to the download onto a Windows pc?
Start with installing Haskell Platform, and then try to use cabal-install. I'm guessing you'll also need to download and install sqlite for Windows, this seems to be a good place to start looking for that [1]. /M [1] http://www.sqlite.org/download.html -- Magnus Therning OpenPGP: 0xAB4DFBA4 email: magnus@therning.org jabber: magnus@therning.org twitter: magthe http://therning.org/magnus

On Thursday 27 January 2011 15:55:58, Renzo Orsini wrote:
In studying Haskell, I produced the following output from GHC:
xxx-3:~ xxx$ GHCi GHCi, version 6.12.3: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Loading package ffi-1.0 ... linking ... done. Prelude> let f 7 = "ok" Prelude> let f x = "no" Prelude> f 3 "no" Prelude> f 7 "no"
I suppose it is correct. However, for someone who is interested in the language, it seems very counterintuitive... Somebody would be so kind to explain to a neophyte this "feature" of the language?
It's not a feature of the language, it's a feature of the interpreter. In ghci's own words (evoked by the -Wall flag): Prelude> let f 7 = "ok" <interactive>:1:5: Warning: Pattern match(es) are non-exhaustive In an equation for `f': Patterns not matched: #x with #x `notElem` [7#] Prelude> let f x = "no" <interactive>:1:5: Warning: This binding for `f' shadows the existing binding bound at <interactive>:1:5 <interactive>:1:7: Warning: Defined but not used: `x' You have defined two independent functions, the second overwriting the first binding. To get what you wanted, you have to put the branches on the same line, separated by a semicolon: Prelude> let f 7 = "ok"; f _x = "no" (0.02 secs, 3348380 bytes) Prelude> f 3 "no" (0.01 secs, 1962728 bytes) Prelude> f 7 "ok" (I prefixed the x with an underscore in the second equation to preven an "unused variable" warning, could also have used the wildcard _).
Thank you very much.
Renzo
Cheers, Daniel

On Thu, Jan 27, 2011 at 8:55 AM, Renzo Orsini
In studying Haskell, I produced the following output from GHC:
xxx-3:~ xxx$ GHCi GHCi, version 6.12.3: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Loading package ffi-1.0 ... linking ... done. Prelude> let f 7 = "ok" Prelude> let f x = "no" Prelude> f 3 "no" Prelude> f 7 "no"
I suppose it is correct. However, for someone who is interested in the language, it seems very counterintuitive... Somebody would be so kind to explain to a neophyte this "feature" of the language?
Here GHCi believes you've defined two functions. Since they both have the same name, the most recent one wins :-) Does that make sense? Antoine
Thank you very much.
Renzo _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

You have defined 2 functions that are called f and only the last definition is used (f x = "no"). The definition you want should be written let f 7 = "ok"; f x = "no" so the function is defined once. The problem comes from using the interpreter and not from haskell. On Thu, 2011-01-27 at 15:55 +0100, Renzo Orsini wrote:
In studying Haskell, I produced the following output from GHC:
xxx-3:~ xxx$ GHCi GHCi, version 6.12.3: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Loading package ffi-1.0 ... linking ... done. Prelude> let f 7 = "ok" Prelude> let f x = "no" Prelude> f 3 "no" Prelude> f 7 "no"
I suppose it is correct. However, for someone who is interested in the language, it seems very counterintuitive... Somebody would be so kind to explain to a neophyte this "feature" of the language?
Thank you very much.
Renzo _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

The problem comes from using the interpreter and not from haskell.
But a very similar 'problem' can be achieved in generic Haskell:
x = do
let f 7 = "yes"
let f x = "no"
putStrLn (f 7)
Arguably the compiler could issue a warning here because the second
'let' keyword might be intended to be spaces. I don't know how common
these mistakes are though.
I think the original poster suggests that the shadowing function
should only be used when the original function doesn't match. That is
more problematic if you ask me though.
/J
On 27 January 2011 16:09, jean verdier
You have defined 2 functions that are called f and only the last definition is used (f x = "no"). The definition you want should be written let f 7 = "ok"; f x = "no" so the function is defined once. The problem comes from using the interpreter and not from haskell.
On Thu, 2011-01-27 at 15:55 +0100, Renzo Orsini wrote:
In studying Haskell, I produced the following output from GHC:
xxx-3:~ xxx$ GHCi GHCi, version 6.12.3: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Loading package ffi-1.0 ... linking ... done. Prelude> let f 7 = "ok" Prelude> let f x = "no" Prelude> f 3 "no" Prelude> f 7 "no"
I suppose it is correct. However, for someone who is interested in the language, it seems very counterintuitive... Somebody would be so kind to explain to a neophyte this "feature" of the language?
Thank you very much.
Renzo _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 1/27/11 09:55 , Renzo Orsini wrote:
Prelude> let f 7 = "ok" Prelude> let f x = "no"
Doing it in separate let bindings doesn't combine; instead, the second one hides the first. let {f 7 = "ok"; f x = "no"} is more likely to do what you expect. - -- brandon s. allbery [linux,solaris,freebsd,perl] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAk1BpscACgkQIn7hlCsL25Xp2ACfYqe/fkuNY9YSnXM3ptFFGPvD 0k0An0hLeo4w7cv2ALc9MQJRhZIEOtyJ =pYh3 -----END PGP SIGNATURE-----
participants (8)
-
Antoine Latter
-
Brandon S Allbery KF8NH
-
Daniel Fischer
-
jean verdier
-
Jonas Almström Duregård
-
Magnus Therning
-
patricklynch
-
Renzo Orsini