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

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

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. 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

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

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

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

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)

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

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

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