functional-programming

number_in_month exercise (Why x = x + 1 is considered bool in sml while x is int and how to say x = x + 1 correctly?)

孤人 提交于 2021-02-08 09:22:27
问题 Update: What I want to do with this code is to get a list of dates, year/month/day and a given number as a month, and check to see how many of the dates in the given list are in the same month as that given month. What I meant of x = x + 1 was x++ such as in java or C or C#. As the output I want x. if there is no match, 0 and for any match x = x + 1 So this is my code, fun number_in_month (Dlist : (int * int * int) list, Month : int, x : int) = if null Dlist then x else if #2 (hd Dlist) =

python functions returning functions [closed]

╄→尐↘猪︶ㄣ 提交于 2021-02-08 09:15:41
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . I am aware of the partial function in functools , but how common is it in general python programs (not: Haskell, Erlang, Clojure etc )

How to return a lazy sequence from a loop recur with a conditional in Clojure?

烈酒焚心 提交于 2021-02-08 07:28:28
问题 Still very new to Clojure and programming in general so forgive the stupid question. The problem is: Find n and k such that the sum of numbers up to n (exclusive) is equal to the sum of numbers from n+1 to k (inclusive). My solution (which works fine) is to define the following functions: (defn addd [x] (/ (* x (+ x 1)) 2)) (defn sum-to-n [n] (addd(- n 1))) (defn sum-to-k [n=1 k=4] (- (addd k) (addd n))) (defn is-right[n k] (= (addd (- n 1)) (sum-to-k n k))) And then run the following loop:

How to return a lazy sequence from a loop recur with a conditional in Clojure?

穿精又带淫゛_ 提交于 2021-02-08 07:27:57
问题 Still very new to Clojure and programming in general so forgive the stupid question. The problem is: Find n and k such that the sum of numbers up to n (exclusive) is equal to the sum of numbers from n+1 to k (inclusive). My solution (which works fine) is to define the following functions: (defn addd [x] (/ (* x (+ x 1)) 2)) (defn sum-to-n [n] (addd(- n 1))) (defn sum-to-k [n=1 k=4] (- (addd k) (addd n))) (defn is-right[n k] (= (addd (- n 1)) (sum-to-k n k))) And then run the following loop:

HackerRank Product Distribution

这一生的挚爱 提交于 2021-02-08 05:56:08
问题 I have been trying to solve this challenge, and somewhat figured out what to do. But after all my attempts, I am able to pass only the test cases, and one more case in the Submit Panel. All fails after that PROBLEM: A company has requested to streamline their product allocation strategy, and given n products, each of which has an associated value, you are required to arrange these products into segments for processing. There are infinite segments indexed as 1, 2, 3 and so on. However, there

What assumptions could a garbage collector for a pure, functional, eagerly-evaluated language safely make?

笑着哭i 提交于 2021-02-08 04:47:31
问题 Clarifying the question a bit: Garbage collectors such as those used by the JVM involve a lot of complexity as a result of the nature of the languages they support. What simplifications would be afforded to a garbage collector purpose-built for a pure, functional, eagerly-evaluated programming language compared to say, the JVM garbage collector? 回答1: I'm barely an expert in functional languages design but when thinking about your question, immediately the following topics come to my mind:

Why it is not a tail recursion?

China☆狼群 提交于 2021-02-07 20:11:29
问题 I have the following code, that I do not understand, why it is not a tail recursion: override fun drop(n: Int): List<A> = if (n == 0) this else tail.drop(n - 1) whereas this is a tail recursion: fun drop(n: Int): List<A> { tailrec fun drop(n: Int, list: List<A>): List<A> = if (n <= 0) list else when (list) { is Cons -> drop(n - 1, list.tail) is Nil -> list } return drop(n, this) } Why is the first example not a tail recursion? 回答1: It isn't tail recursion because Kotlin checks the recursive

Is there a Variadic Version of either (R.either)?

最后都变了- 提交于 2021-02-07 15:17:15
问题 I have a need for a variadic version of R.either . After doing some searching around the web, I have not found a solution. R.anyPass would work but it returns a Boolean instead of the original value. Is there already a solution that I have overlooked? If not, what would be the most optimal way to write a variadic either utility function? An example: const test = variadicEither(R.multiply(0), R.add(-1), R.add(1), R.add(2)) test(1) // => 2 回答1: You could use a combination of reduce + reduced :

Is there a Variadic Version of either (R.either)?

匆匆过客 提交于 2021-02-07 15:11:40
问题 I have a need for a variadic version of R.either . After doing some searching around the web, I have not found a solution. R.anyPass would work but it returns a Boolean instead of the original value. Is there already a solution that I have overlooked? If not, what would be the most optimal way to write a variadic either utility function? An example: const test = variadicEither(R.multiply(0), R.add(-1), R.add(1), R.add(2)) test(1) // => 2 回答1: You could use a combination of reduce + reduced :

Is there a Variadic Version of either (R.either)?

走远了吗. 提交于 2021-02-07 15:10:16
问题 I have a need for a variadic version of R.either . After doing some searching around the web, I have not found a solution. R.anyPass would work but it returns a Boolean instead of the original value. Is there already a solution that I have overlooked? If not, what would be the most optimal way to write a variadic either utility function? An example: const test = variadicEither(R.multiply(0), R.add(-1), R.add(1), R.add(2)) test(1) // => 2 回答1: You could use a combination of reduce + reduced :