## Scala working using a var in a pure function. Is it possible without var?

Is it possible (or even worthwhile) to try to write the below code block without a var? It works with a var. This is not for an interview, it's my first attempt at scala (came from java). The problem: Fit people as close to the front of a theatre as

## Need help finding the second largest number in a list. Racket

I've been struggling with this question for a lot now. Could someone please explain the logic behind the program by using the simplest way possible possibly recursion? Thank you.Have 2 variables (say x and y) Move through the list of numbers Keep lar

## Recursive tail function for sum of fractions

I am trying to convert this recursive function into a tail recursive function def sumOfFractions(n: Int): Double = { require(n > 0, "Parameter n has to be greater than 0"); if (n==1) 1.0 else 1.0 / n + sumOfFractions(n - 1) } I thought that t

## Assign special cases when certain variables have certain values?

for my functional programming homework I am instructed to write a function that gives back the real solutions of a quadratic equation in a list, I used the discriminant to find them out. So, my code looks something like this: quadSols::Double->Double

## Lambda calculation beta reduction specific steps and why

From a book I'm recently reading: First:

## Clojure: O (1) function to check if the sequence has exactly 1 element

Is there a fast way to check if a sequence has exactly 1 element in Clojure? Note that a sequence may contain nils. If I'm reading the source correctly, calling count on a sequence takes O(n) time. Alternate solution: (and (not (empty? a-seq)) (empty

## JavaScript - Converting Tables to Maps

Given an array of objects: var projects = [ { id: 1, name: 'Trader Portal' }, { id: 2, name: 'Risk Engine' }, ]; What is the most elegant way of converting it into the following structure - essentially and array of the ids along with a map of the obj

## How to continue from the beginning of the code when an exception is thrown?

I'm running a code that throws an exception,I want run the exception code continuously whenever the exception is thrown it has go to starting and should start the program from starting. Here is my exception method and mainYou need to put the try / ca

## Access to the depth of the call stack in Scheme

In order to demonstrate the effectiveness of tail recursion, I would like a way to access the depth of the call stack dynamically in Scheme. Is there a way to do this? If not, is there a way to do this in other major functional languages (OCaml, Hask

## Counting survival with selection with functional programming

I would like to rewrite better my code using functional programming, but I am not so expert. I have a collection of pointer to items: std::vector<Item*>* collection; I need to apply a selection to the items. The selection is made by several steps, e

## Can someone explain to me what state and mutable data are?

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. http://en.wikipedia.org/wiki/Functional_programming Can anyone explain me wha

## Equivalent of std :: bind of C ++ in Java?

Is there a way to bind parameters parameters to function pointers in Java like you can with std::bind in C++? What would the Java equivalent of something like this be? void PrintStringInt(const char * s, int n) { std::cout << s << ", &quo

## How to decompose a list like this in scheme / lisp?

Input1: (decompose '(* 1 2 3 4)) Output1: '(* 1 (* 2 (* 3 4))) Input2 (decompose '(+ 1 2 3 (* 5 6 7))) Output2 '(+ 1 (+ 2 (+ 3 (* 5 (* 6 7))))) Does anyone have ideas about this?Same way as you would evaluate it, but instead of outputting the result,

## Scala flattens the list

I want to write a function that flats a List. object Flat { def flatten[T](list: List[T]): List[T] = list match { case Nil => Nil case head :: Nil => List(head) case head :: tail => (head match { case l: List[T] => flatten(l) case i => List

## Why can not I use the function symbol (# ') extracted from the list in Common Lisp?

The first one doesn't work. But the second one works,which make me confused.. Could anyone explain a bit about that? CL-USER> (funcall (first '(#'+ +)) 1) ; Evaluation aborted on #<TYPE-ERROR expected-type: ; (OR FUNCTION SYMBOL) datum: #'+>. CL-

## JQuery is a monad

I read somewhere that jQuery is a monad and this answer shows that chain function in underscore.js library is not a monad (but comonad). And answer to this which is similar, shows that is monoid. So, is jQuery a monad?Most APIs do not satisify the mo

## Interesting way to apply a function to every nth element of a sequence?

What's a neat way to map a function to every nth element in a sequence ? Something like (map-every-nth fn coll n), so that it would return the original sequence with only every nth element transformed, e.g. (map-every-nth inc (range 16) 4) would retu

## filtered and unfiltered elements in a single pass

Filter function returns a sublist of elements which return true for a given function. Is it possible to get the list of elements which return false in a different list without going over the entire list again. Example: trueList,falseList = someFunc(t

## How to automatically expose and decorate the functional versions of Python methods?

I would like to expose the methods of a class as functions (after decoration) in my local scope. For example if I had a class and decorator: def some_decorator(f): ...transform f... return decorated_f class C(object): def __init__(self,x): self.x = x

## Abuse algebra of algebraic data types - why does it work?

The 'algebraic' expression for algebraic data types looks very suggestive to someone with a background in mathematics. Let me try to explain what I mean. Having defined the basic types Product • Union + Singleton X Unit 1 and using the shorthand X² f

## a function composition VS a function that acts on another function

I have numerous reusable functions, all with the same signature (they take a record and return a float). I often need to combine functions into a new function. Let's say I want to create a function that takes a record, applies f to it, and if the res

## Whole Sharing in Scala

Given n ( say 3 people ) and s ( say 100\$ ), we'd like to partition s among n people. So we need all possible n-tuples that sum to s My Scala code below: def weights(n:Int,s:Int):List[List[Int]] = { List.concat( (0 to s).toList.map(List.fill(n)(_)).f

## When to use closures

Note: jump down to "Question" below if you just want to skip the context When giving talks on Scala I'm almost always certain I'll be asked "but when would you use a Closure?" Typically I say "wait until I show you a for comprehen

## Function application in LINQ expressions

We are trying to find a workaround for the issue that the Entity Framework doesn't support non-scalar entities. We are using a particular equality so we try to build an expression that for a given input and a function from that input checks whether t

## Which functional programming language should I choose as the first functional programming language?

I would like to learn a functional programming language to see a different programming paradigm. My background in programming: Java (I have just passed the SCJP exam), some ruby and very limited Rails. I use bash for basic sysadmin tasks. I use Ubunt

## Coding practice for F #

I have been dabbling with F# in Visual Studio 2010. I am a developer with more code/architecture design experience in object-oriented languages such as C# and Java. To expand my skill set and help make better decisions I am trying different languages

## Playing with infinity - lazy arithmetic

Many modern programming languages allow us to handle potentially infinite lists and to perform certain operations on them. Example [Python]: EvenSquareNumbers = ( x * x for x in naturals() if x mod 2 == 0 ) Such lists can exist because only elements

## What are the best / worst problems solved by functional programming?

I've often heard that functional programming solves a lot of problems that are difficult in procedural/imperative programming. But I've also heard that it isn't great at some other problems that procedural programming is just naturally great at. Befo

## Basic F # questions: mutability, capitalization standards, functions and methods

Feel free to point me to other answers if these have already been asked! I'm just starting F# with the new release this month. I've got some background in both OO and functional languages (Haskell and Scheme, but not OCaml/ML). A couple of questions

## Does Ruby optimize the call of the queue?

Functional languages lead to use of recursion to solve a lot of problems, and therefore many of them perform Tail Call Optimization (TCO). TCO causes calls to a function from another function (or itself, in which case this feature is also known as Ta