English 中文(简体)
如何在全球界定Haskell的“地方”关键词
原标题:How can keyword "where" be defined globally in Haskell

I m reading the guide in learnyouahaskell.com . One sentence mentioned "where" can be shared globally, but no example was given, so where can I find some details, please?

"where bindings aren t shared across function bodies of different patterns. If you want several patterns of one function to access some shared name, you have to define it globally."

最佳回答

http://learnyouahaskell.com/syntax-in-Functions”rel=“nofollow” 第4章:职能中的协同效应:

where bindings aren t shared across function bodies of different patterns. If you want several patterns of one function to access some shared name, you have to define it globally.

Here is an illustration:

f (Left x) = double x
f (Right x) = double x
    where
    double x = 2 * x

The function f has a body for each of the patterns (Left x) and (Right x). The binding of double isn t shared across the function bodies, and therefore this code isn t valid Haskell.

If we want to access double from both of the function bodies, we have to move it out of the where clause:

double x = 2 * x

f (Left x) = double x
f (Right x) = double x

该款所引述的意思就是这样。

问题回答

我认为,这意味着你必须界定一个新的<>功能。 在全球范围,而不是在处。





相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签