Functional programming is a programming paradigm which primarily uses functions as means for building abstractions and expressing computations that comprise a computer program.
0
votes
1answer
35 views
OO vs FP | imperative vs FP [on hold]
I often come across these kind of comparisons:
object oriented vs functional programming
imperative vs functional programming
As you can see, on the left there are two distinct terms, which in my ...
2
votes
3answers
87 views
How to replace this loop with something more idiomatic?
I've been working through "Clojure for the Brave and True" and I just spent an hour staring at this loop trying to turn it into a reduce or some other "prettier" loop. I'm getting tripped up on ...
0
votes
2answers
32 views
Functional recursive method in JavaScript? Bad practise?
Can anyone see a problem with a recursive function written like this:
var recurse = 100;
var recursed = 0;
(function (callback){
callback(callback);
})(function(callback){
recursed++;
...
1
vote
2answers
27 views
Function.prototype.call.bind(Array.prototype.slice) vs. Array.prototype.slice.call
I am trying to write a static version of slice.
What is the difference between
Function.prototype.call.bind(Array.prototype.slice)
and
Array.prototype.slice.call.
If I write:
var x = ...
5
votes
1answer
62 views
OCaml Currying/ Multiple arguments
I have a simple function:
let rec ap x y = if x < 10 then 12 else ap((x-1) (y));;
but the error I keep on getting is:
Error: This expression has type int
This is not a function; it cannot ...
0
votes
2answers
67 views
Update the values of a list with their absolute values
Newbie to scala.
I am trying to make this code to work for a few hours now . It is intended to update the List[Int](list of integers) with absolute values of the integers.
Took a long time to figure ...
0
votes
1answer
35 views
OCaml Abstract typed functions
I want to accomplish something like this, but I can't quite get the syntax down.
type _ s = Var : 'a -> 'a s
type _ t =
| AA :('a -> 'a s) -> 'c t
| AB : ('a -> 'b s) -> 'c t
let ...
1
vote
1answer
46 views
OCaml while true do loop
I have the following code:
let a = 1 in
while a<10 do
let a = a+1 in
done
Printf.printf "the number is now %d\n" a
The interpreter is complaining about line 4, which is done and I have no idea ...
0
votes
1answer
63 views
How to communicate between Agents?
Using the MailboxProcessor in F#, what is the preferred way to communicate between them? - Wrapping the agents into objects like:
type ProcessAgent(saveAgent:SaveAgent) = ...
type SaveAgent() = ...
...
3
votes
1answer
50 views
Approaches For Deeply Extending a Parser Library
The material on parser combinators I have found covers building up complex parsers though composition, but I would like to know if there are any good approaches for defining parsers by tweaking the ...
0
votes
1answer
38 views
Scala: abstract type pattern A is unchecked since it is eliminated by erasure
I am writing the function that can catch exceptions of the certain type only.
def myFunc[A <: Exception]() {
try {
println("Hello world") // or something else
} catch {
...
5
votes
1answer
276 views
What's the highest order function used in practice?
For instance, mask in Haskell is of type (((forall a . IO a -> IO a) -> IO b) -> IO b). What is the purpose of such a function? Any language with a notion of a higher-order function is ...
0
votes
1answer
69 views
strange flatMap return type
my problem at its simplest form:
object test {
import scala.language.higherKinds
sealed trait IO[+F[+_], +A] {
def flatMap[G[+_] >: F[_], B](f: A => IO[G, B]): IO[G, B]
}
trait ...
3
votes
2answers
83 views
Scala: split string via pattern matching
Is it possible to split string into lexems somehow like
"[email protected]" match {
case name :: "@" :: domain :: "." :: zone => doSmth(name, domain, zone)
}
In other words, on the same ...
2
votes
6answers
81 views
Scala: Silently catch all exceptions
Empty catch block seems to be invalid in Scala
try {
func()
} catch {
} // error: illegal start of simple expression
How I can catch all exceptions without their processing?
1
vote
2answers
60 views
How to pass a code block to function?
I am trying to create a try clause analogue which repeats code block if exception occurred inside this code block.
def retry(attempts: Int)(func: Unit => Unit) {
var attempt = 0
while ...
1
vote
2answers
96 views
Count negative numbers in list using list comprehension
Working through the first edition of "Introduction to Functional Programming", by Bird & Wadler, which uses a theoretical lazy language with Haskell-ish syntax.
Exercise 3.2.3 asks:
Using a ...
1
vote
1answer
39 views
What's the advantage of declaring a function as a var? [duplicate]
I've seen this in a lot of scripts. When is it better to do
var foo = function() { console.log("Foo!"); };
than
function foo() { console.log("Foo!"); }
when it's obvious that function name(){} ...
-3
votes
1answer
54 views
Is there a reason to use Scheme over JavaScript? [closed]
As far as I know, JavaScript can be used to do everything that is possible in Scheme.
Every functional programming paradigm I've learnt in Scheme is doable in JavaScript.
I know that one may end up ...
3
votes
1answer
66 views
Haskell IORef array usage
I am working on an program that uses a large list of IORef's to a data type. Which is the more memory/processor-efficient way to do this:
[IORef Foo]
or
IORef [Foo]
Ignore the fact that I ...
2
votes
1answer
51 views
Implicit resolution and companion objects for case classes
I'm trying to add an implicit value to (what I believe is) the companion object of a case class, but this implicit value is not found.
I'm trying to achieve something like the following:
package ...
1
vote
1answer
23 views
How can callbacks modify state of objects in pure functional programming?
Here is the problem: a GUI button has a callback that toggles its state from checked to unchecked.
In imperative programming languages, it is very easy to implement: just modify the Button's state. ...
0
votes
2answers
77 views
F# adding an element to a list if it doesn't exists
I'm starting to learn f# from scratch, and i'm finding myself quite confused with the difference between arrays and lists, I know lists are immutable but still.
I'm trying to create a function that ...
0
votes
1answer
94 views
Scala way to write lisp-like progn?
Is there in Scala some language construction like lisp's progn?
Thanks!
1
vote
2answers
64 views
is a call to create a new object instance considered pure or not?
in functional programming terminology if I perform:
val a = new Client
val b = new Client
Is calling the above constructor twice considered a pure or a non pure function?
-2
votes
1answer
49 views
Non Anonymous Function Java
I'm trying to convert a non-anonymous function into java code.
The expression that I have is: let foo = fun(x) -> sum(x,3) in foo(10)
This expression should produce the result 13. (sum 3 to 10).
How ...
4
votes
2answers
63 views
Picking which ocaml module to use with command line parameter
In my code I've module M = Implementation1 and then I reference M, instead of Implementation1. The problem is, I've to recompile my program to change Implementation1 to Implementation2. I'd like to ...
0
votes
0answers
23 views
Schema modelling with functions
I am looking for a schema language that allows definition of functions. I thought of http://json-schema.org/ at first, but that format is data-only. My knowledge from XSD is the same.
The language is ...
2
votes
2answers
76 views
Is this the correct way of doing functional programming in Java?
I'm trying to change my way of thinking to be more functional. Here's the sample code which I'm trying to change to be more functional.
List<Integer> numbers = Arrays.asList(1, 2 ,3, 4, 5, ...
4
votes
2answers
98 views
Functional way in IO related tasks [closed]
Most beginners program IO related tasks in an imperative way. Are there any general suggestions to help beginners switching to more functional way in implementing IO related tasks?
As a concrete ...
1
vote
1answer
50 views
Remove duplicates from a list in SML
I just started learning functional programming in SML and I want to know how I can combine the following two functions into a single function. The function isolate deletes the duplicates of a list of ...
3
votes
5answers
158 views
Checking if a password is strong enough - Haskell vs procedural language
I implemented a function in Python that checks if a password is strong enough. A password is strong enough if it passes 3 out of 5 checks. This is the Python function:
def is_valid(password):
...
0
votes
1answer
42 views
Ocaml - building binary trees
I'm reading tutorial for OCaml by Jason Hickey and here is in short the proposed way of building a tree:
type 'a elem = Empty | Node of 'a * 'a elem * 'a elem;;
let rec insert x = function
| ...
1
vote
1answer
31 views
Pattern matching in SML - representing a list as (x::y)
I just started learning functional programming and I find myself very confused by the concept of pattern matching (i'm using SML). Take for example the following expression to insert an element in an ...
0
votes
2answers
79 views
Scala read file and split and modify each line
I'm new to Scala. I want to read lines from a text file and split and make changes to each lines and output them.
Here is what I got:
val pre:String = " <value enum=\""
val mid:String = "\" ...
0
votes
3answers
97 views
Maximum non-segment sum
We have a list / array of numbers (positives and negatives are all possible).
A segment is defined as contiguous subsequence of the numbers. For example, [1;-2;3;4;5] is the array and a segment of it ...
1
vote
1answer
62 views
How to implement a deep-tree-set algorithm using fold?
This is the pseudocode for a function that will modify a specific node on a tree given a path and a new value:
path_set val path tree =
| empty? path -> val
| otherwise -> set ...
1
vote
1answer
79 views
GHC won't run this function, but does compile it
This is the code:
finde_f x =
if (x-2) mod 3 /= 0
then 1
else x - (x-2)/3
These are the errors during run-time:
*Main> finde_f 6
<interactive>:170:1:
No instance for ...
0
votes
2answers
92 views
Haskell function example, why infinite list doesnt stop
Why doesnt map sqrt[1..] not give an infinite recursion????
How can i better understand the haskell?
sqrtSums :: Int
sqrtSums = length ( takeWhile (<1000) (scanl1 (+) (map sqrt[1..]))) + 1
3
votes
2answers
157 views
Does OCaml have fusion laws
Recently I am reading some functional programming books involving Haskell.
It seems Haskell quite fancies “modular programs”, for example,
f : (Integer;Integer) ! Integer
f = sum . map . sq . ...
3
votes
3answers
79 views
Calculating types of Haskell Functions
Having troubles with manually calculating types of given functions in Haskell for an exam at the weekend.
I understand the basics such as:
i x = x :: t -> t
k x y = x :: t -> t1 -> t
But having ...
1
vote
1answer
70 views
Displaying the stemming word and the stemming using haskell
hi i am new to Haskell and functional programing.
i want to find the stemming word in the string and display the word and the word after the stemming is removed.
eg if the string is : "he is a good ...
3
votes
1answer
121 views
Lambda Calculus (λa.b)((λx.xx)(λx.xx)) [closed]
Im looking for an example of a weakly normalising lambda term.
Am I right in saying that the following:
(λa.b)((λx.xx)(λx.xx))
Reduces to:
b
or:
doesnt terminate (if you try to reduce ...
0
votes
1answer
25 views
Iterating across subsets of collections
I have a collection of data objects (say x1, x2, and x3).
require(xts)
set.seed(1)
x1 <- xts(data.frame(replicate(6, sample(c(1:10), 10, rep = T))), Sys.Date() + 1:10)
x2 <- ...
3
votes
1answer
44 views
What is an algorithm to enumerate lambda terms?
What is an algorithm that will enumerate expressions for the lambda calculus by order of length? For example, (λx.x), (λx.(x x)), (λx.(λy.x)) and so on?
1
vote
0answers
45 views
Node.js Empty function, [closed]
Suppose I have a function defined in the following way.
function MyFunction(param, CallBack) {
// Doing stuff
CallBack();
}
I want to call the same function with three different parameters but ...
1
vote
1answer
82 views
Can the same fusion law for foldr be applied to foldlmap?
I've read there is no fusion law for foldl alone. If I guess correctly, this is due to foldl awkwardness for implementing map - which is mostly due to (foldl cons nil xs) reversing the list.
If ...
5
votes
2answers
126 views
Is it possible to implement foldl/foldr using unsided fold?
By unsided fold, I mean a hypothetic primitive fold operation for associative operators that, does not guarantee any ordering. That is, (fold + 0 [a b c d]) could be (+ (+ a b) (+ c d)) or (+ (+ (+ a ...
-1
votes
2answers
147 views
Cannot IO in haskell
I have a function to count no of times each word is repeated in a string:
keywords :: String -> [String]
keywords = words . map (\x -> if isAlpha x then x else ' ')
count :: Ord a => [a] ...
8
votes
2answers
135 views
Fundamentals of Haskell Type Classes and “could not deduce (~) from the context (~)” error
I'm relatively new to Haskell, and I believe I'm misunderstanding something fundamental about type classes. Suppose I'd like to create a type class 'T' implementing n-ary trees supported by four ...