functional-programming

How do you represent a graph in Haskell?

帅比萌擦擦* 提交于 2019-12-27 12:40:10
问题 It's easy enough to represent a tree or list in haskell using algebraic data types. But how would you go about typographically representing a graph? It seems that you need to have pointers. I'm guessing you could have something like type Nodetag = String type Neighbours = [Nodetag] data Node a = Node a Nodetag Neighbours And that would be workable. However it feels a bit decoupled; The links between different nodes in the structure don't really "feel" as solid as the links between the current

How do you represent a graph in Haskell?

旧巷老猫 提交于 2019-12-27 12:39:50
问题 It's easy enough to represent a tree or list in haskell using algebraic data types. But how would you go about typographically representing a graph? It seems that you need to have pointers. I'm guessing you could have something like type Nodetag = String type Neighbours = [Nodetag] data Node a = Node a Nodetag Neighbours And that would be workable. However it feels a bit decoupled; The links between different nodes in the structure don't really "feel" as solid as the links between the current

Scala currying vs partially applied functions

做~自己de王妃 提交于 2019-12-27 12:39:06
问题 I realize that there are several questions on here about what currying and partially applied functions are, but I'm asking about how they are different. As a simple example, here is a curried function for finding even numbers: def filter(xs: List[Int], p: Int => Boolean): List[Int] = if (xs.isEmpty) xs else if (p(xs.head)) xs.head :: filter(xs.tail, p) else filter(xs.tail, p) def modN(n: Int)(x: Int) = ((x % n) == 0) So you could write the following to use this: val nums = List(1,2,3,4,5,6,7

What is the difference between def foo = {} and def foo() = {} in Scala?

北慕城南 提交于 2019-12-27 11:45:43
问题 Given the following constructs for defining a function in Scala, can you explain what the difference is, and what the implications will be? def foo = {} vs. def foo() = {} Update Thanks for the quick responses. These are great. The only question that remains for me is: If I omit the parenthesis, is there still a way to pass the function around? This is what I get in the repl: scala> def foo = {} foo: Unit scala> def baz() = {} baz: ()Unit scala> def test(arg: () => Unit) = { arg } test: (arg:

Why does the absence of an else block translate to Unit type return for a function?

▼魔方 西西 提交于 2019-12-25 18:52:20
问题 I noticed there is a type mismatch caused in the line else if(r1 == 0 || divisors.tail.isEmpty || !divisors.tail.contains(r1)){newAcc} . Because there is no else clause to my if ... else if ... def euclidianDivision(dividend:Int,divisor:Int):(Int,Int)={ val quotient = dividend/divisor val remainder = dividend%divisor (quotient,remainder) } def firstExpansion(dividend:Int,divisors:List[Int]):List[(Int,Int)]={ def firstExpansionIter(dividend:Int,divisors:List[Int], acc:List[(Int,Int)]):List[

How to determine which day of a year belongs to what month? [closed]

寵の児 提交于 2019-12-25 18:21:29
问题 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 6 years ago . I'm a beginner and I gotta write a function in SML. The assignment question is : write a function named what_month that takes the

Is performing a mapping operation without using returned value an antipattern?

北城余情 提交于 2019-12-25 18:14:38
问题 Lets say i have a list and i want to add some values into it using a mapping function: const arr = [1, 2, 3, 4, 5]; const anotherArr = []; I do this using a functional approach: arr.map((item) => anotherArr.push(item); Is this an antipattern / bad logic - that is, not using the mapping operation return value for anything? Are there any good resources on this? (i know this logic is silly and i can just copy the list - that is not the point of my question) 回答1: Yes, this is an anti-pattern. The

Programming with dplyr 0.3

喜夏-厌秋 提交于 2019-12-25 16:57:09
问题 I am trying to group and summarise with a function, using the new underscore functions for standard evaluation provided in dplyr 0.3. However, I ran into an issue when trying to use lapply instead of a loop: Small example fruits <- c("APPLE", "PEAR", "BANANA") makes <- c("HONDA", "FERRARI", "TESLA") df <- data.frame(fruit = sample(fruits, 100, replace = T), make = sample(makes, 100, replace = T), value = 1:100) cols <- c("fruit", "make") showTopTenFactors <- function(x, ...) x %>% group_by_(.

Insert Node into a Tree - Racket

与世无争的帅哥 提交于 2019-12-25 16:46:49
问题 I am trying to add a new node to the tree. The following are my definitions and function type: (define-struct (Some T) ([value : T])) (define-type (Option T) (U 'None (Some T))) (define-type BST (U 'E Nd)) (define-struct Nd ([root : Integer] [lsub : BST] [rsub : BST])) (: insert : Integer BST -> BST) ;; insert an item into a tree ;; note: do not insert duplicate items (define (insert n x) (match x ('E 'E) ((Nd ro ls rs) (cond ((= (size x) 1) (Nd ro (Nd n 'E 'E) 'E)) (else (Nd ro ls rs))))))

How to pass one function as an argument into another function in C++? [duplicate]

扶醉桌前 提交于 2019-12-25 11:14:11
问题 This question already has answers here : How do I pass a function as parameter? (3 answers) Best way to pass function as parameter (2 answers) c++: pass function as parameter to another function (3 answers) Closed 2 years ago . Functions are said to be first class objects in C++. How is it possible to replicate the following pyhton code in C++? def print_wrapper(a): print(a) def execute_function(func, arg): return func(arg) execute_function(print_wrapper, 'test_argument') Please note that the