functional-programming

How to use GADTs in Hugs

帅比萌擦擦* 提交于 2019-12-23 01:40:11
问题 I'd like to write a Haskell program that uses GADTs interactively on a platform not supported by GHCi (namely, GNU/Linux on mipsel). The problem is, the construct that can be used to define a GADT in GHC, for example: data Term a where Lit :: Int -> Term Int Pair :: Term a -> Term b -> Term (a,b) ... doesn't seem working on Hugs. Can't GADTs really be defined in Hugs? My TA at a Haskell class said it was possible in Hugs, but he seemed unsure. If not, can GADT be encoded by using other syntax

functional programming algorithm for finding repeated characters

别等时光非礼了梦想. 提交于 2019-12-22 18:47:09
问题 I am converting the "zxcvbn" password strength algorithm from JavaScript to Scala. I am looking for a pure functional algorithm for finding sequences of repeating characters in a string. I know that I can translate the imperative version from JavaScript, but I would like to keep this as side-effect free as possible, for all the reasons usually given for functional programming. The algorithm can be in Scala, Clojure, Haskell, F#, or even pseudocode. Thanks. 回答1: Using Haskell's standard higher

Service that returns data from an asynchronous method

喜夏-厌秋 提交于 2019-12-22 18:36:16
问题 I am using Sails' ORM (Waterline). I have written a geturl service that should return the url of several models/actions in my app. I am currently calling this service inside my templates. (As I am alone to develop this, don't hesitate to warn me if this design pattern is wrong) Now it occurs that Waterline's .find() method is asynchronous (as it should). I always use callbacks to do things when inserting or fetching things in database. Now I have seen everywhere that I cannot return any data

representing sets with lambda functions

和自甴很熟 提交于 2019-12-22 17:47:08
问题 I am struggling with understanding what I really need to do, and would like some outside input or a point to a good reference. I have been asked to use procedural representation to "implement sets of numbers." Each set will be a one argument function that takes a number and decides if the number is in the set. A few functions (that I have read can be defined in one line) that I have to create: A function that returns a function taking a number as an argument and checks if the number is in the

Difference between lifting and higher order functions

我与影子孤独终老i 提交于 2019-12-22 17:11:24
问题 I usually hear the term lifting, when people are talking about map , fold , or bind , but isn't basically every higher order function some kind of lifting? Why can't filter be a lift from a -> Bool to [a] -> [a] , heck even the bool function (which models an if statement) can be considered a lift from a -> a to Bool -> a . And if they are not, then why is ap from the Applicative type class considered a lift? If the important thing is going from ... a ... to ... f a ... , then ap wouldn't fit

How to avoid intermediate results when performing array iterations?

混江龙づ霸主 提交于 2019-12-22 12:38:30
问题 When working with arrays, intermediate representations are needed regularly - particularly in connection with functional programming, in which data is often treated as immutable: const square = x => x * x; const odd = x => (x & 1) === 1; let xs = [1,2,3,4,5,6,7,8,9]; // unnecessary intermediate array: xs.map(square).filter(odd); // [1,4,9,16,25,36,49,64,81] => [1,9,25,49,81] // even worse: xs.map(square).filter(odd).slice(0, 2); // [1,9] How can I avoid this behavior in Javascript/Ecmascript

how does the undecided generic type represents in ghci's runtime

你离开我真会死。 提交于 2019-12-22 11:49:13
问题 I'm clear about the generic functions and generic data-types. In the generic type: data SB = forall x. (show x) => SB x instance Show SB where show (SB x) = show x so for any given type x , if it has a signature of Show , and there sure be a show function corresponds to it. but when typing in ghci, e.g. :t 1 outputs 1 :: Num a => a if binds 1 to a name: let a=1 :t a now a has a true type. The question is: What is 1 's form in the run-time system (before it has a type, it has only Num ) ,

Difference between fold and reduce revisted

こ雲淡風輕ζ 提交于 2019-12-22 11:45:15
问题 I've been reading a nice answer to Difference between reduce and foldLeft/fold in functional programming (particularly Scala and Scala APIs)? provided by samthebest and I am not sure if I understand all the details: According to the answer ( reduce vs foldLeft ): A big big difference (...) is that reduce should be given a commutative monoid, (...) This distinction is very important for Big Data / MPP / distributed computing, and the entire reason why reduce even exists. and Reduce is defined

How to perform a nested update using Ramda in the given object structure?

江枫思渺然 提交于 2019-12-22 11:27:45
问题 Assuming the follow object how is it possible to use Ramda to perform a nested update in a criteria given an application, criteria ID and data? const application = { id: 'a1', features: [ { id: 'f1', criterias: [ { id: 'c1' } ] }, { id: 'f2', criterias: [ { id: 'c2' }, { id: 'c3' } ] } ] } The function would look something like this: const updateCriteria = (application, criteriaId, data) => // magic... updateCriteria(application, 'c2', { name: 'foo' }) // output: { // id: 'a1', // features: [

Have Python 2.7 functions remember value and not reference? Closure Weirdness

こ雲淡風輕ζ 提交于 2019-12-22 11:10:58
问题 I'm trying to return from a function a list of functions, each of which uses variables from the outside scope. This isn't working. Here's an example which demonstrates what's happening: a = [] for i in range(10): a.append(lambda x: x+i) a[1](1) # returns 10, where it seems it should return 2 Why is this happening, and how can I get around it in python 2.7 ? 回答1: The i refers to the same variable each time, so i is 9 in all of the lambdas because that's the value of i at the end of the loop.