functional-programming

In Haskell, why non-exhaustive patterns are not compile-time errors?

时间秒杀一切 提交于 2019-12-28 16:30:13
问题 This is a follow-up of Why am I getting "Non-exhaustive patterns in function..." when I invoke my Haskell substring function? I understand that using -Wall , GHC can warn against non-exhaustive patterns. I'm wondering what's the reason behind not making it a compile-time error by default given that it's always possible to explicitly define a partial function: f :: [a] -> [b] -> Int f [] _ = error "undefined for empty array" f _ [] = error "undefined for empty array" f (_:xs) (_:ys) = length

Understanding Y Combinator through generic lambdas

不羁岁月 提交于 2019-12-28 16:09:42
问题 While building a small lambda-based metaprogramming library, I had the necessity of using recursion in a C++14 generic lambda, to implement a left-fold. My own solution was passing the lambda itself as one of its parameters, like this: template <typename TAcc, typename TF, typename... Ts> constexpr auto fold_l_impl(TAcc acc, TF f, Ts... xs) { // Folding step. auto step([=](auto self) { return [=](auto y_acc, auto y_x, auto... y_xs) { // Compute next folding step. auto next(f(y_acc, y_x)); //

This expression should have type 'unit', but has type 'ConsoleKeyInfo'

蓝咒 提交于 2019-12-28 12:57:41
问题 I just wanted to pause in an F# console application, so I wrote: Console.ReadKey() But this gives the warning: This expression should have type 'unit', but has type 'ConsoleKeyInfo'. What can I do to fix this? 回答1: Solution: Console.ReadKey() |> ignore Explanation: Console.ReadKey() returns an object of type 'ConsoleKeyInfo' but you're using it as a statement without assigning the return value to anything. So F# warns you that you're ignoring a value. ignore takes any type and returns nothing

This expression should have type 'unit', but has type 'ConsoleKeyInfo'

試著忘記壹切 提交于 2019-12-28 12:56:51
问题 I just wanted to pause in an F# console application, so I wrote: Console.ReadKey() But this gives the warning: This expression should have type 'unit', but has type 'ConsoleKeyInfo'. What can I do to fix this? 回答1: Solution: Console.ReadKey() |> ignore Explanation: Console.ReadKey() returns an object of type 'ConsoleKeyInfo' but you're using it as a statement without assigning the return value to anything. So F# warns you that you're ignoring a value. ignore takes any type and returns nothing

Modify bound variables of a closure in Python

不羁岁月 提交于 2019-12-28 12:56:12
问题 Is there any way to modify the bound value of one of the variables inside a closure? Look at the example to understand it better. def foo(): var_a = 2 var_b = 3 def _closure(x): return var_a + var_b + x return _closure localClosure = foo() # Local closure is now "return 2 + 3 + x" a = localClosure(1) # 2 + 3 + 1 == 6 # DO SOME MAGIC HERE TO TURN "var_a" of the closure into 0 # ...but what magic? Is this even possible? # Local closure is now "return 0 + 3 + x" b = localClosure(1) # 0 + 3 +1 ==

Modify bound variables of a closure in Python

淺唱寂寞╮ 提交于 2019-12-28 12:50:11
问题 Is there any way to modify the bound value of one of the variables inside a closure? Look at the example to understand it better. def foo(): var_a = 2 var_b = 3 def _closure(x): return var_a + var_b + x return _closure localClosure = foo() # Local closure is now "return 2 + 3 + x" a = localClosure(1) # 2 + 3 + 1 == 6 # DO SOME MAGIC HERE TO TURN "var_a" of the closure into 0 # ...but what magic? Is this even possible? # Local closure is now "return 0 + 3 + x" b = localClosure(1) # 0 + 3 +1 ==

Automatic memoizing in functional programming languages

假如想象 提交于 2019-12-28 12:00:20
问题 I always thought that Haskell would do some sort of automatic intelligent memoizing. E.g., the naive Fibonacci implementation fib 0 = 0 fib 1 = 1 fib n = fib (n-2) + fib (n-1) would be fast because of that. Now I read this and it seems I was wrong -- Haskell doesn't seem to do automatic memoization. Or do I understand something wrong? Are there other languages which do automatic (i.e. implicit, not explicit) memoization? What are common ways to implement memoization? In all sample

“Uncurrying” an instance method in .NET

杀马特。学长 韩版系。学妹 提交于 2019-12-28 11:56:22
问题 Can you create a delegate of an instance method without specifying the instance at creation time? In other words, can you create a "static" delegate that takes as it's first parameter the instance the method should be called on? For example, how can I construct the following delegate using reflection? Func<int, string> = i=>i.ToString(); I'm aware of the fact that I can use methodInfo.Invoke, but this is slower, and does not check for type-correctness until it is called. When you have the

Can someone explain to me why the app function of ArrowApply makes them as powerful as monads?

送分小仙女□ 提交于 2019-12-28 11:11:24
问题 So I'll break my question into 4 parts, but first some background: I feel relatively comfortable with Monads, but not very comfortable with Arrows. I suppose the main problem I have with them is, I don't see what they are useful for. Whether formally correct or not, I understand Monads to be a tool that allows us to introduce side effects from computation. As they generalize program fragments from pure values to values boxed with other actions. From my shotgun "read all the papers" approach

How to implement Haskell's splitEvery in Swift?

北城以北 提交于 2019-12-28 07:01:11
问题 PROBLEM let x = (0..<10).splitEvery( 3 ) XCTAssertEqual( x, [(0...2),(3...5),(6...8),(9)], "implementation broken" ) COMMENTS I am running into problems calculating number of elements in the Range, etc... extension Range { func splitEvery( nInEach: Int ) -> [Range] { let n = self.endIndex - self.startIndex // ERROR - cannot invoke '-' with an argument list of type (T,T) } } 回答1: The values in a range are of ForwardIndexType , so you can only advance() them, or compute the distance() , but the