functional-programming

Clojure - tail recursive sieve of Eratosthenes

旧城冷巷雨未停 提交于 2019-12-30 08:23:34
问题 I have this implementation of the sieve of Eratosthenes in Clojure: (defn sieve [n] (loop [last-tried 2 sift (range 2 (inc n))] (if (or (nil? last-tried) (> last-tried n)) sift (let [filtered (filter #(or (= % last-tried) (< 0 (rem % last-tried))) sift)] (let [next-to-try (first (filter #(> % last-tried) filtered))] (recur next-to-try filtered)))))) For larger n (like 20000) it ends with stack overflow. Why doesn't tail call elimination work here? How to fix it? 回答1: Problem: filter does lazy

Operator Overloading in Clojure

守給你的承諾、 提交于 2019-12-30 08:04:26
问题 Even looking closely over documentation on Clojure, I do not see any direct confirmation as to whether or not Clojure supports operator overloading. If it does, could someone provide me with a quick snipplet of how to overload, let's say, the "+" operator to delegate to some predefined method that we can call myPlus . I am very new to Clojure, so someone's help here would be greatly appreciated. 回答1: Clojure's (as any Lisp's) operators are plain functions; you can define an "operator" like a

Find object in array with subarray checking an property

青春壹個敷衍的年華 提交于 2019-12-30 07:51:12
问题 I have the array below and each element has another array called FunctionalityChildren, I need find the unique object that contains the property ActionFull equal a variable, for example '/budget/allocation' or '/budget' let bigArray = [ { "FunctionalityID": 114, "Name": "General Register", "Action": "/general-register", "Icon": "settings_input_composite", "System_ID": 21, "FunctionalityFather_ID": null, "Active": 1, "Priority": 1, "FunctionalityChildren": [ { "FunctionalityID": 115, "Name":

How do I prove that two Fibonacci implementations are equal in Coq?

╄→гoц情女王★ 提交于 2019-12-30 05:27:22
问题 I've two Fibonacci implementations, seen below, that I want to prove are functionally equivalent. I've already proved properties about natural numbers, but this exercise requires another approach that I cannot figure out. The textbook I'm using have introduced the following syntax of Coq, so it should be possible to prove equality using this notation: <definition> ::= <keyword> <identifier> : <statement> <proof> <keyword> ::= Proposition | Lemma | Theorem | Corollary <statement> ::= {

R: specifying variable name in function parameter for a function of general (universal) use

故事扮演 提交于 2019-12-30 05:12:12
问题 Here is my small function and data. Please note that I want to design a function not personal use for general use. dataf <- data.frame (A= 1:10, B= 21:30, C= 51:60, D = 71:80) myfun <- function (dataframe, varA, varB) { daf2 <- data.frame (A = dataframe$A*dataframe$B, B= dataframe$C*dataframe$D) anv1 <- lm(varA ~ varB, daf2) print(anova(anv1)) } myfun (dataframe = dataf, varA = A, varB = B) Error in eval(expr, envir, enclos) : object 'A' not found It works with when I specify data$variable

Zipping with padding in Haskell

断了今生、忘了曾经 提交于 2019-12-30 03:07:49
问题 A couple of times I've found myself wanting a zip in Haskell that adds padding to the shorter list instead of truncating the longer one. This is easy enough to write. ( Monoid works for me here, but you could also just pass in the elements that you want to use for padding.) zipPad :: (Monoid a, Monoid b) => [a] -> [b] -> [(a, b)] zipPad xs [] = zip xs (repeat mempty) zipPad [] ys = zip (repeat mempty) ys zipPad (x:xs) (y:ys) = (x, y) : zipPad xs ys This approach gets ugly when trying to

What is the correct way of initializing an elm application

会有一股神秘感。 提交于 2019-12-30 02:44:14
问题 The documentation for Elm's Random module states: A good way to get an unexpected seed is to use the current time. http://package.elm-lang.org/packages/elm-lang/core/1.1.0/Random I don't see however a good example of how to perform such initialization logic in FRP application. What signal should I react to? How to do this with a minimum of code and maximum of clarity. 回答1: There are different ways to do this. Each has it's own benefits. I'll give you the three that I know with a similar

Is there a Python equivalent of the Haskell 'let'

僤鯓⒐⒋嵵緔 提交于 2019-12-30 02:42:06
问题 Is there a Python equivalent of the Haskell 'let' expression that would allow me to write something like: list2 = [let (name,size)=lookup(productId) in (barcode(productId),metric(size)) for productId in list] If not, what would be the most readable alternative? Added for clarification of the let syntax: x = let (name,size)=lookup(productId) in (barcode(productId),metric(size)) is equivalent to (name,size) = lookup(productId) x = (barcode(productId),metric(size)) The second version doesn't

Functional languages (Erlang, F#, Haskell, Scala)

最后都变了- 提交于 2019-12-29 18:50:10
问题 1) Are functional languages suited for web applications development? 2) Are functional languages suited for business/ERP/CRM type of applications? 回答1: Functional languages of the kind you describe are general purpose programming languages, they're used for all manner of things, including web apps and business apps. (I use Haskell). Is Haskell good for Web Apps? Building commerical web apps in Haskell As gabor implies, ultimately it comes down to libraries. Scala has a web framework: lift.

What is Haskell's style of polymorphism?

依然范特西╮ 提交于 2019-12-29 18:37:03
问题 With Haskell's type classes it almost seems that it enables ad hoc polymorphism, but its functions declarations seem parametric polymorphism. Am I mixing my understanding of different things? 回答1: Indeed, Haskell supports both (higher rank) parametric polymorphism, and ad hoc (or bounded ) polymorphism. Parametric polymorphism in Haskell is supported via its Hindley-Milner/System F type system. Ad hoc polymorphism is supported via type classes. For the origin of type classes and ad hoc