functional-programming

Understanding Peter Norvig's permutation solution in PAIP

為{幸葍}努か 提交于 2021-02-20 19:00:28
问题 Peter Norvig's PAIP book contains this code as a solution to the permutation problem (some sections are removed for brevity) (defun permutations (bag) ;; If the input is nil, there is only one permutation: ;; nil itself (if (null bag) '(()) ;; Otherwise, take an element, e, out of the bag. ;; Generate all permutations of the remaining elements, ;; And add e to the front of each of these. ;; Do this for all possible e to generate all permutations. (mapcan #'(lambda (e) (mapcar #'(lambda (p)

Is it possible in C++11 to combine functions into a new function?

痴心易碎 提交于 2021-02-20 18:51:54
问题 This is more a kind of theoretical question. Is it possible in C++11 to combine functions into a new function? For example : auto f = [](int i){return i * 2;}; auto g = [](int i){return i + 10;}; So this works: auto c = f(g(20)); // = 60 But I want an object that stores the combination, like auto c = f(g); std::cout << c(20) << std::endl; //prints 60 Edit: Additionally what i want to create is a function a, which you can give a function b and an int n , and which returns the n'th combination

Difference b/w Method[Int]() vs Method() in scala?

情到浓时终转凉″ 提交于 2021-02-20 04:12:32
问题 After seeing the method signature of map in scala as def map[A, B](l: List[A])(f: A => B): List[B] = ??? my layman undersanting of [A, B] in above is that they signal the method that we'll be exclusively working with generic Type A and B in the defined method. Now I go on to implement a method to add values from two lists consquetively (basically zipWith) using similar coding pattern. def addCorresponding[Int](l1: List[Int], l2: List[Int]): List[Int] = (l1, l2) match { case (_, Nil) => Nil

Using map with multiple args

流过昼夜 提交于 2021-02-19 07:10:07
问题 Python's map can take multiple iterables, for use when the callable can accept the same number of input arguments. If the input iterables are the same length, thats behaving like the list comprehension passing in zipped arguments, e.g.: >>> iterables = 'spam', 'eggs' >>> map(max, *iterables) ['s', 'p', 'g', 's'] >>> [max(*a) for a in zip(*iterables)] ['s', 'p', 'g', 's'] When the input arguments are different length, it gets weird - Python 2 (docs) pads with None , but Python 3 (docs)

Using map with multiple args

戏子无情 提交于 2021-02-19 07:09:53
问题 Python's map can take multiple iterables, for use when the callable can accept the same number of input arguments. If the input iterables are the same length, thats behaving like the list comprehension passing in zipped arguments, e.g.: >>> iterables = 'spam', 'eggs' >>> map(max, *iterables) ['s', 'p', 'g', 's'] >>> [max(*a) for a in zip(*iterables)] ['s', 'p', 'g', 's'] When the input arguments are different length, it gets weird - Python 2 (docs) pads with None , but Python 3 (docs)

Using map with multiple args

随声附和 提交于 2021-02-19 07:08:10
问题 Python's map can take multiple iterables, for use when the callable can accept the same number of input arguments. If the input iterables are the same length, thats behaving like the list comprehension passing in zipped arguments, e.g.: >>> iterables = 'spam', 'eggs' >>> map(max, *iterables) ['s', 'p', 'g', 's'] >>> [max(*a) for a in zip(*iterables)] ['s', 'p', 'g', 's'] When the input arguments are different length, it gets weird - Python 2 (docs) pads with None , but Python 3 (docs)

Using map with multiple args

安稳与你 提交于 2021-02-19 07:08:07
问题 Python's map can take multiple iterables, for use when the callable can accept the same number of input arguments. If the input iterables are the same length, thats behaving like the list comprehension passing in zipped arguments, e.g.: >>> iterables = 'spam', 'eggs' >>> map(max, *iterables) ['s', 'p', 'g', 's'] >>> [max(*a) for a in zip(*iterables)] ['s', 'p', 'g', 's'] When the input arguments are different length, it gets weird - Python 2 (docs) pads with None , but Python 3 (docs)

Optimizing a Free Monad

牧云@^-^@ 提交于 2021-02-19 03:44:10
问题 If I have a value a: Free[Op, A] , is it possible to "flatten" the structure of a so that two Op s that are bound together by the free monad may be collapsed into one? Context: I'd like to perform this as an optimization step before interpretation because a semantic of Op is that its operations are idempotent. So if two appear "in a row", the second can be eliminated at no cost to the semantics of the program. 回答1: As far as I understand there is no way for this kind introspection of Free

How do I determine type of Haskell functions? [duplicate]

扶醉桌前 提交于 2021-02-19 01:18:05
问题 This question already has an answer here : Type Inference in Haskell for functions (1 answer) Closed 1 year ago . I'm preparing for my exams but there is something I can't understand. functions: tw f x = f (f x) f x y = (y, x) I am able to determine the type of 'f' which is f :: t1 -> t -> (t, t1) but can't determine the type of 'tw'. Supposed type of tw: tw :: (t -> t) -> t -> t thanks! 回答1: Let us analyze the function tw : tw f x = f (f x) tw takes as parameters f and x . At the moment we

for/continue in scheme/lisp

旧街凉风 提交于 2021-02-19 01:16:57
问题 I'm writing a small interpreter for a C-like language in Scheme (R5RS) and trying to convert something like: for (i = 0; i < 100; i++) { if (isprime(i)) continue; else /* do something with i */ } to valid Scheme (the isprime function is just an example and not important). However, after trying for some time, I have not been able to find an efficient/simple way to add the equivalent of a continue statement to a do loop in Scheme. What would be even better would be a "for" macro which allows