scope - Why can't pattern-matching-definition be a closure in Haskell? -


Why are these pseudo-Haskell function definitions not accepted?

  fn = if n & lt; 3 then n and g 2 2 1 0 where gnabc = a - Note that 'n' has a value of scope scope gkabc = g (k + 1) (a + 2 * b + 3 * c) ab   

which calculates this "exercise-function": f (n) = n if n & lt; 3 and f (n-1) + 2 * f (n-2) + 3 * f (N-3)

  fib n = let fnab = b - note That is a value of the enclosed scope of 'n' fkab = f (k + 1) b (a + b) f1 0 1   

This work of course to calculate Fibonacci numbers Is:

  fib n = let {fkab = if k == n again bf (k + 1) b (a + b);} f1 0  <  
  Warning: Pattern matching (SA) Overlapped   

I closed a function Can not define, which is using a method of matching with a value, which I get from the scope of scope?

This is because the value of the scope of the scope is fixed at runtime (in general) and for some reason (for what reason?) The compiler can not make that orchestra?

This is a language design choice: Pattern can not be done on mailing variables It is difficult to decide whether to avoid brain exercise, In reality, if you declare local variables, take a look at this example:

Foo.hs:

  Module Fu Where foo: int = 42   

bar .hs:

  where the modules bar import fu bar :: int -> Bool bar foo = Tru E bar _ = False   

You can easily guess that foo is bound to look at Bar.hs. ​​Required to fix the context when there is a syntax Whether you declare a new variable or use existing one is misleading.

As an alternative solution, you can still use the Guard:

  Fn = if N & TT; 3 then NAG G2 2 1 0 giCa _ _ | K == n = agkabc = g (k + 1) (a + 2 * b + 3 * c) Now  < / Pre> 

or

  fn = if N & lt; 3 then NAG 2 2 1 0 where gkabc | K == N = A | Otherwise = g (k + 1) (a + 2 * b + 3 * c) a b    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -