functional-programming

How to represent a simple finite state machine in Ocaml?

十年热恋 提交于 2019-12-31 15:40:05
问题 I have written some state machine in C++ and Java but never in a functional language like Ocaml Problem is I don't know if I can just adapt code from the object languages versions, since in Ocaml records and variants are more powerful than class; So, I need an event-driven finite state machine (hierarchical like in UML), easily configurable Could someone experienced in the field post a simple sample of that ? Just to avoid the most common traps thanks :) EDIT 16/03 : Is it possible to do it

Which programming language or a library can process Infinite Series?

浪子不回头ぞ 提交于 2019-12-31 11:33:40
问题 Which programming language or a library is able to process infinite series (like geometric or harmonic)? It perhaps must have a database of some well-known series and automatically give proper values in case of convergence, and maybe generate an exception in case of divergence. For example, in Python it could look like: sum = 0 sign = -1.0 for i in range(1,Infinity,2): sign = -sign sum += sign / i then, sum must be math.pi/4 without doing any computations in the loop (because it's a well

Generics and Constrained Polymorphism versus Subtyping

别等时光非礼了梦想. 提交于 2019-12-31 10:35:43
问题 In this PDF presentation on Haskell Type Classes, slide #54 has this question: Open Question : In a language with generics and constrained polymorphism, do you need subtyping too? My questions are: How do generics and constrained polymorphism make subtyping unnecessary? If generics and constrained polymorphism make subtyping unnecessary, why does Scala have subtyping? 回答1: How do generics and constrained polymorphism make subtyping unnecessary? It is not known that they do. If you put the

Python: Difference between filter(function, sequence) and map(function, sequence)

拥有回忆 提交于 2019-12-31 08:45:33
问题 I'm reading through the Python documentation to really get in depth with the Python language and came across the filter and map functions. I have used filter before, but never map, although I have seen both in various Python questions here on SO. After reading about them in the Python tutorial, I'm confused on the difference between the two. For example, from 5.1.3. Functional Programming Tools: >>> def f(x): return x % 2 != 0 and x % 3 != 0 ... >>> filter(f, range(2, 25)) [5, 7, 11, 13, 17,

What do “reify” and “reification” mean in the context of (functional?) programming?

这一生的挚爱 提交于 2019-12-31 08:33:27
问题 I read this term a lot in blogs about haskell and functional programming (specially in sigfpe's blog) but I don't have a clue about what it means. I get away with not knowing it most of the times, but I probably would have understood the texts a lot better if I knew. Google didn't help me. I get lost in the technical stuff. Also the non-technical meaning of the world ("turning the abstract concrete") doesn't help me understand what it practically means to reify something in code. I'm kinda

What do “reify” and “reification” mean in the context of (functional?) programming?

a 夏天 提交于 2019-12-31 08:32:50
问题 I read this term a lot in blogs about haskell and functional programming (specially in sigfpe's blog) but I don't have a clue about what it means. I get away with not knowing it most of the times, but I probably would have understood the texts a lot better if I knew. Google didn't help me. I get lost in the technical stuff. Also the non-technical meaning of the world ("turning the abstract concrete") doesn't help me understand what it practically means to reify something in code. I'm kinda

First class functions in Go

好久不见. 提交于 2019-12-31 08:07:31
问题 I come from JavaScript which has first class function support. For example you can: pass a function as a parameter to another function return a function from a function. Can someone give me an example of how I would do this in Go? 回答1: Go Language and Functional Programming might help. From this blog post: package main import fmt "fmt" type Stringy func() string func foo() string{ return "Stringy function" } func takesAFunction(foo Stringy){ fmt.Printf("takesAFunction: %v\n", foo()) } func

Haskell filtering a nested list with specific data constructors

拜拜、爱过 提交于 2019-12-31 07:43:52
问题 Suppose I have the data type data Joke = Funny String | Lame String and say I have the following nested list [[Funny "Haha", Lame "boo"], [Funny "Haha"], [Lame "BOO"]] How would I go about filtering such a nested list so that any list within the nested list that contains Funny "Haha" is removed? In other words, I'm trying to filter the list so that I receive the following result: [[Lame "BOO"]] Any list that contains Funny "Haha" is removed. Would appreciate any help, I'm having a terrible

Scala to Java (functional programming)

不打扰是莪最后的温柔 提交于 2019-12-31 05:26:31
问题 I have been asked to 'translate' some Scala code to Java for a course. However, the requirements of the assignment are that Java 8 and external libraries, such as Functional Java and Totally Lazy, are not allowed . The line in Scala is: charges.groupBy(_.cc).values.map(_.reduce(_ combine _)).toList I have been able to write groupBy and values but .map and _.reduce still elude me. I have looked at the source code of those two libraries as well as the Scala source to try and find something to

How to convert a string to integer list in ocaml?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-31 04:44:08
问题 I need to pass two list as command line arguments in ocaml. I used the following code to access it in the program. let list1=Sys.argv.(1);; let list2=Sys.argv.(2);; I need to have the list1 and list2 as list of integers. I am getting the error This expression has type string but an expression was expected of type int list while processing. How can I convert that arguments to a list of integers. The arguments are passed in this format [1;2;3;4] [1;5;6;7] 回答1: Sys.argv.(n) will always be a