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.  A very counterintuitive behaviour of Haskell (Renzo Orsini)
   2. Re:  A very counterintuitive behaviour of Haskell
      (Magnus Therning)
   3. Re:  A very counterintuitive behaviour of Haskell (Daniel Fischer)
   4. Re:  A very counterintuitive behaviour of Haskell (Antoine Latter)
   5. Re:  A very counterintuitive behaviour of Haskell (jean verdier)
   6. Re:  A very counterintuitive behaviour of Haskell
      (Magnus Therning)
   7. Re:  A very counterintuitive behaviour of Haskell (Renzo Orsini)
   8. Re:  A very counterintuitive behaviour of Haskell
      (Jonas Almstr?m Dureg?rd)
   9. Re:  A very counterintuitive behaviour of Haskell
      (Brandon S Allbery KF8NH)


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

Message: 1
Date: Thu, 27 Jan 2011 15:55:58 +0100
From: Renzo Orsini <[email protected]>
Subject: [Haskell-beginners] A very counterintuitive behaviour of
        Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

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


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

Message: 2
Date: Thu, 27 Jan 2011 15:02:22 +0000
From: Magnus Therning <[email protected]>
Subject: Re: [Haskell-beginners] A very counterintuitive behaviour of
        Haskell
To: Renzo Orsini <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Thu, Jan 27, 2011 at 14:55, Renzo Orsini <[email protected]> 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.

/M

-- 
Magnus Therning ? ? ? ? ? ? ? ? ? ? ?OpenPGP: 0xAB4DFBA4
email: [email protected] ? jabber: [email protected]
twitter: magthe ? ? ? ? ? ? ? http://therning.org/magnus



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

Message: 3
Date: Thu, 27 Jan 2011 16:07:21 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] A very counterintuitive behaviour of
        Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

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



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

Message: 4
Date: Thu, 27 Jan 2011 09:08:28 -0600
From: Antoine Latter <[email protected]>
Subject: Re: [Haskell-beginners] A very counterintuitive behaviour of
        Haskell
To: Renzo Orsini <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Thu, Jan 27, 2011 at 8:55 AM, Renzo Orsini <[email protected]> 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?

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
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



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

Message: 5
Date: Thu, 27 Jan 2011 16:09:53 +0100
From: jean verdier <[email protected]>
Subject: Re: [Haskell-beginners] A very counterintuitive behaviour of
        Haskell
To: Renzo Orsini <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="UTF-8"

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
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners





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

Message: 6
Date: Thu, 27 Jan 2011 15:31:23 +0000
From: Magnus Therning <[email protected]>
Subject: Re: [Haskell-beginners] A very counterintuitive behaviour of
        Haskell
To: Renzo Orsini <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Thu, Jan 27, 2011 at 15:02, Magnus Therning <[email protected]> wrote:
> On Thu, Jan 27, 2011 at 14:55, Renzo Orsini <[email protected]> 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: [email protected] ? jabber: [email protected]
twitter: magthe ? ? ? ? ? ? ? http://therning.org/magnus



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

Message: 7
Date: Thu, 27 Jan 2011 16:31:36 +0100
From: Renzo Orsini <[email protected]>
Subject: Re: [Haskell-beginners] A very counterintuitive behaviour of
        Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Thank you very much to everybody.  Your explanation is perfectly clear! I was 
reading the tutorial at http://learnyouahaskell.com/syntax-in-functions . 
Initially the tutorial cited GHCi, so I mistakenly wrote the functions on 
different lines.

Thank very much again. I regained the desire of continuing my study... :-)

Renzo

On Jan 27, 2011, at 16:09 , jean verdier wrote:

> 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
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
> 
> 




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

Message: 8
Date: Thu, 27 Jan 2011 16:35:27 +0100
From: Jonas Almstr?m Dureg?rd <[email protected]>
Subject: Re: [Haskell-beginners] A very counterintuitive behaviour of
        Haskell
To: jean verdier <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

> 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 <[email protected]> wrote:
> 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
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



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

Message: 9
Date: Thu, 27 Jan 2011 12:09:27 -0500
From: Brandon S Allbery KF8NH <[email protected]>
Subject: Re: [Haskell-beginners] A very counterintuitive behaviour of
        Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

-----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]      [email protected]
system administrator  [openafs,heimdal,too many hats]  [email protected]
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-----



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

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


End of Beginners Digest, Vol 31, Issue 35
*****************************************

Reply via email to