Benefits of using notation in Haskell

What are the pros and cons of explicitly defining a function as opposed to where is the Haskell notation?

Explicit function definition:

foo :: Integer -> Integer
foo a = bar a
  where
    bar :: Integer -> Integer
    bar a = Some code here

Unlike:

foo :: Integer -> Integer
foo a = bar a

bar :: Integer -> Integer
bar a = Some code here

Why should I use one over the other? Is there anything you need to know about effectiveness? Safety? Code reuse? Code readability?

+4
source share
2 answers

If your helper function will not be used elsewhere, it is best not to pollute the namespace and use a local definition.

"" , where , .

outer x v z f = undefined
    where 
        inner i = i + x + v + z + f

outer x v z f = undefined

inner x v z f i = i + x + v + z + f

"" , , where. .

where . ( HaskellWiki let vs where)

fib x = map fib' [0 ..] !! x
    where
      fib' 0 = 0
      fib' 1 = 1
      fib' n = fib (n - 1) + fib (n - 2)

, :

fib = (map fib' [0 ..] !!)
    where
      fib' 0 = 0
      fib' 1 = 1
      fib' n = fib (n - 1) + fib (n - 2)

, fib' .

, fib fib'. .

+9

foo, , "where". foo, foo.

0

Source: https://habr.com/ru/post/1533035/


All Articles