1=2

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

1=2

M Douglas McIlroy
ghc and ghci 8.6.5 accept 1=2 at top level. It seems to have no effect.
What does it mean?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: 1=2

amindfv@mailbox.org
It's the same in do-blocks:

main :: IO ()
main = do
   let 3 = 2 + 2
   putStrLn "Oh fiddlesticks"

On Mon, Dec 14, 2020 at 12:14:53PM -0500, M Douglas McIlroy wrote:
> ghc and ghci 8.6.5 accept 1=2 at top level. It seems to have no effect.
> What does it mean?
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: 1=2

Francesco Ariis
Il 14 dicembre 2020 alle 20:05 [hidden email] ha scritto:
> It's the same in do-blocks:
>
> main :: IO ()
> main = do
>    let 3 = 2 + 2
>    putStrLn "Oh fiddlesticks"

What happens exactly when I type this?

    λ> "prova" = "foo"
    λ> 'c' = 'd'
    λ> 'c'
    'c'

From the Report I read:

    lexp  → let decls in exp
    decls → { decl1 ; … ; decln }       (n ≥ 0)
    ⁝
    decl  → (funlhs | pat) rhs
    ⁝

Am I correct in saying `pat rhs` is the rule being used here? I do not
understand how/when it comes useful in a let
—F


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: 1=2

Thomas Hallgren
On 2020-12-14 18:14, M Douglas McIlroy wrote:
> ghc and ghci 8.6.5 accept 1=2 at top level. It seems to have no effect.

On 2020-12-15 02:29, Francesco Ariis wrote:

> Il 14 dicembre 2020 alle 20:05 [hidden email] ha scritto:
>> It's the same in do-blocks:
>>
>> main :: IO ()
>> main = do
>>    let 3 = 2 + 2
>>    putStrLn "Oh fiddlesticks"
>
> What happens exactly when I type this?
>
>     λ> "prova" = "foo"
>     λ> 'c' = 'd'
>     λ> 'c'
>     'c'

These are examples of pattern bindings, but since they don't bind any variables,
they are not very useful.

As examples of more useful pattern bindings, consider

        > (xs,ys) = splitAt 3 [1..8]
        > [1,x,y] = [1..3]

These are both examples where the value of the expression on the rhs matches the
pattern on the lhs, so you can obtain the values of the variables bound by the
pattern:

        > xs
        [1,2,3]
        > ys
        [4,5,6,7,8]
        > x
        2
        > y
        3

If the value of the expression on the rhs doesn't match the pattern on the lhs,
you get an error, but because of lazy evaluation the value of the rhs is not
computed and matched against the pattern until you use one of the variables in
the pattern:

        > [1,z,2] = [1..3]
        > z
        *** Exception: <interactive>:7:1-16: Irrefutable pattern failed for pattern [1,
z, 2]

This means that when the pattern does not contain any variables, the value of
the rhs is never computed and matched against the pattern, so even if the value
does not match the pattern, there is no error message:

        > [] = [1..3]
        > [_,_,_] = []
        > True = False
        > 1 = 2

If you turn on -Wunused-pattern-binds, you get a warning for pattern bindings
like these:

        > :set -Wunused-pattern-binds
        > True=False

        <interactive>:2:1: warning: [-Wunused-pattern-binds]
            This pattern-binding binds no variables: True = False


Best regards,
Thomas H



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: 1=2

Francesco Ariis
Il 15 dicembre 2020 alle 15:45 Thomas Hallgren ha scritto:
> These are examples of pattern bindings, but since they don't bind any variables,
> they are not very useful.
> […]

Very instructive, many thanks
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners