functional-programming

How to generate stable id for AST nodes in functional programming?

回眸只為那壹抹淺笑 提交于 2019-12-13 14:05:24
问题 I want to substitute a specific AST node into another, and this substituted node is specified by interactive user input. In non-functional programming, you can use mutable data structure, and each AST node have a object reference, so when I need to reference to a specific node, I can use this reference. But in functional programming, use IORef is not recommended, so I need to generate id for each AST node, and I want this id to be stable , which means: when a node is not changed, the

repeating elements in common lisp [closed]

北城以北 提交于 2019-12-13 13:17:41
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I am try to create a function with two arguments x and y which creates a list of y times repeated elements X but im getting confused on how to do it which or which method to use i think list compression can do

Is it possible for the presented case to be optimized into one loop?

懵懂的女人 提交于 2019-12-13 13:05:01
问题 Suppose I have two functions f :: [a] -> b and g :: [a] -> c . I have the following two questions: If I perform (f &&& g) xs where xs :: [a] , and if both f and g involve loops, is it possible for the compiler to optimize these two loops into one? (Please note that I am not asking whether some specific Haskell compiler implements this. I want to know whether such a thing is possible .) Can the traverse function from Traverse type class help me have such an optimization with something along

Functional way to map over a list with an accumulator in Scala

爱⌒轻易说出口 提交于 2019-12-13 12:33:52
问题 I would like to write succinct code to map over a list, accumulating a value as I go and using that value in the output list. Using a recursive function and pattern matching this is straightforward (see below). But I was wondering if there is a way to do this using the function programming family of combinators like map and fold etc. Obviously map and fold are no good unless you use a mutable variable defined outside the call and modify that in the body. Perhaps I could do this with a State

Why is flatMap on a Vector[Option[Int]] whose mapper function result is not a Vector[Option[Int]] valid?

Deadly 提交于 2019-12-13 12:10:24
问题 For example, Vector(Some(1), Some(2), Some(3), None).flatMap{ n => n } produces a Vector(1, 2, 3) instead of giving an error. As I have seen in other languages, flatMap is used when you have a mapper function that produces nesting so I would expect this to be a valid flatMap : Vector(1, 2, 3).flatMap{ eachNum => Vector(eachNum) } My mapper function produces a Vector which would cause nesting (i.e. Vector(Vector(1), Vector(2), Vector(3), Vector(4)) ) if I used a map due to the container

scala: idiomatic and functional way to add errors to a list of errors

ε祈祈猫儿з 提交于 2019-12-13 12:09:52
问题 I have the following validation method: def validate(wine: Wine): List[Error] = { var errors = List[Error]() if (Validate.isEmptyWord(wine.name)) { errors ::= ValidationError("name", "Name not specified") } else { if (isDuplicate(wine, "name")) { errors ::= ValidationError("name", "There already exists a wine with the name '%s'".format(wine.name)) } } if (Validate.isEmptyWord(wine.grapes)) { errors ::= ValidationError("grapes", "Grapes not specified") } if (Validate.isEmptyWord(wine.country))

Monads not with “flatMap” but “flatUnit”? [closed]

左心房为你撑大大i 提交于 2019-12-13 11:32:00
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed last year . Monads in category theory is defined by triples T, unit, flat ⟩. class Monad t where map :: (a -> b) -> (t a -> t b) -- functorial action unit :: a -> t a flat :: t (t a) -> t a class KleisliTriple t where unit :: a -> t a flatMap :: t a -> (a -> t b) -> t b KleisliTriple flats the structure by the

function that gets an Int and returns a list

青春壹個敷衍的年華 提交于 2019-12-13 10:58:47
问题 I have an exercise in Haskell where I need to create various types.The first type is called Finite which is defined like this: type Finite a = [a] and then I need to return a singleton which is defined like this singleF :: a -> Finite a so I implemented it like so: single n = [n] Then later I create another type type Enumeration a = Int -> Finite a then I need to reimplement the singleton function singleE :: a -> Enumeration a In my understanding the type Enumeration is a synonym for a

Where should one draw the line between functional programming and imperative programming in a hybrid language like Scala? [closed]

こ雲淡風輕ζ 提交于 2019-12-13 10:29:54
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . Functional programmers often look down upon conventions and approaches used in imperative programming, like the usage of mutable variable, the usage of for loops, the creation of explicit exceptions. However if one digs deep into Scala's library one would discover that the

How to use CONST when there are multiple conditions

心不动则不痛 提交于 2019-12-13 10:13:45
问题 I'm getting on the const train and want to start avoiding let at all costs. The problem I'm seeing is the below case - how would I use const in a situation with more than 2 forks in a logic tree? What's the equivalent pattern with const? function getResult(input) { let result; switch (input): { case (1): { result=x;} case (2): { result=y;} case (3): { result=x;} ...etc } /* ...additional conditionals and functions depending on the outcomes of switch statement */ } thanks, 回答1: const result =