functional-programming

How to implement a dictionary as a function in OCaml?

时间秒杀一切 提交于 2019-12-21 03:33:35
问题 I am learning Jason Hickey's Introduction to Objective Caml. Here is an exercise I don't have any clue First of all, what does it mean to implement a dictionary as a function ? How can I image that? Do we need any array or something like that? Apparently, we can't have array in this exercise, because array hasn't been introduced yet in Chapter 3 . But How do I do it without some storage? So I don't know how to do it, I wish some hints and guides. 回答1: I think the point of this exercise is to

Can someone clarify what this Joel On Software quote means: (functional programs have no side effects)

依然范特西╮ 提交于 2019-12-21 03:17:04
问题 I was reading Joel On Software today and ran across this quote: Without understanding functional programming, you can't invent MapReduce, the algorithm that makes Google so massively scalable. The terms Map and Reduce come from Lisp and functional programming. MapReduce is, in retrospect, obvious to anyone who remembers from their 6.001-equivalent programming class that purely functional programs have no side effects and are thus trivially parallelizable. What does he mean when he says

Compose LINQ-to-SQL predicates into a single predicate

梦想与她 提交于 2019-12-21 02:47:52
问题 (An earlier question, Recursively (?) compose LINQ predicates into a single predicate, is similar to this but I actually asked the wrong question... the solution there satisfied the question as posed, but isn't actually what I need. They are different, though. Honest.) Given the following search text: "keyword1 keyword2 ... keywordN" I want to end up with the following SQL: SELECT [columns] FROM Customer WHERE ( Customer.Forenames LIKE '%keyword1%' OR Customer.Forenames LIKE '%keyword2%' OR .

Functional style early exit from depth-first recursion

拟墨画扇 提交于 2019-12-20 23:49:47
问题 I have a question about writing recursive algorithms in a functional style. I will use Scala for my example here, but the question applies to any functional language. I am doing a depth-first enumeration of an n -ary tree where each node has a label and a variable number of children. Here is a simple implementation that prints the labels of the leaf nodes. case class Node[T](label:T, ns:Node[T]*) def dfs[T](r:Node[T]):Seq[T] = { if (r.ns.isEmpty) Seq(r.label) else for (n<-r.ns;c<-dfs(n))

How would the 'Model' in a Rails-type webapp be implemented in a functional programming language?

Deadly 提交于 2019-12-20 20:39:08
问题 In MVC web development frameworks such as Ruby on Rails, Django, and CakePHP, HTTP requests are routed to controllers, which fetch objects which are usually persisted to a backend database store. These objects represent things like users, blog posts, etc., and often contain logic within their methods for permissions, fetching and/or mutating other objects, validation, etc. These frameworks are all very much object oriented. I've been reading up recently on functional programming and it seems

How would the 'Model' in a Rails-type webapp be implemented in a functional programming language?

风格不统一 提交于 2019-12-20 20:38:28
问题 In MVC web development frameworks such as Ruby on Rails, Django, and CakePHP, HTTP requests are routed to controllers, which fetch objects which are usually persisted to a backend database store. These objects represent things like users, blog posts, etc., and often contain logic within their methods for permissions, fetching and/or mutating other objects, validation, etc. These frameworks are all very much object oriented. I've been reading up recently on functional programming and it seems

Most useful and instructive functional-logic language to learn

 ̄綄美尐妖づ 提交于 2019-12-20 19:43:14
问题 I was pretty amazed by the power of Prolog. It took some time to get the head around, but to me it seemed to be the coolest declarative language out there. That's why recently, after two years of some functional programming with Scala, I decided to take a look at logical programming again, to "train my brain" or better for actual usage. Combining functional and logical programming seems attractive for me to learn/solidify concepts of both declarative paradigms. I find also find strong type

What is the correct term for the following functional programming pattern?

余生长醉 提交于 2019-12-20 19:42:09
问题 I've heard it referred to as a stream, as an infinite list, and sometimes even as a lazy sequence. What is the correct term for the following pattern? (Clojure code shown) (def first$ first) (defn second$ [str] (cond (empty? str) () true ((first (rest str))))) (defn stream-builder [next_ n] (cons n (cons (fn [] (stream-builder next_ (next_ n))) ()))) (defn stream [str n] (cond (= 0 n) () true (cons (first$ str) (stream (second$ str) (- n 1))))) (def odd (stream-builder (fn [n] (+ 2 n))1))

Map, Filter, Foldr in DrRacket/Scheme

狂风中的少年 提交于 2019-12-20 19:38:36
问题 Programming language: Scheme/DrRacket We're currently going over map , filter , and foldr in my comp sci class. I understand that all three can be used to create abstract functions, but I am honestly a little confused about the difference between the three and when I'd use each one. Anyone care to explain what each is used for and how they are different? Unfortunately my book is not very clear. 回答1: The basic idea is that all three are ways of applying some function to all the elements of a

Performance Implications of Point-Free style

十年热恋 提交于 2019-12-20 17:34:26
问题 I’m taking my first baby-steps in learning functional programing using F# and I’ve just come across the Forward Pipe (|>) and Forward Composition (>>) operators. At first I thought they were just sugar rather than having an effect on the final running code (though I know piping helps with type inference). However I came across this SO article: What are advantages and disadvantages of “point free” style in functional programming? Which has two interesting and informative answers (that instead