Variables in Haskell

后端 未结 3 940
再見小時候
再見小時候 2021-01-18 20:43

Why does the following Haskell script not work as expected?

find :: Eq a => a -> [(a,b)] -> [b]
find k t = [v | (k,v) <- t]

Giv

3条回答
  •  别那么骄傲
    2021-01-18 21:31

    From the Haskell 98 Report:

    As usual, bindings in list comprehensions can shadow those in outer scopes; for example:

    [ x | x <- x, x <- x ] = [ z | y <- x, z <- y]

    One other point: if you compile with -Wall (or specifically with -fwarn-name-shadowing) you'll get the following warning:

    Warning: This binding for `k' shadows the existing binding
               bound at Shadowing.hs:4:5
    

    Using -Wall is usually a good idea—it will often highlight what's going on in potentially confusing situations like this.

提交回复
热议问题