functional-programming

Keep record of previously visited states when searching

余生长醉 提交于 2020-01-06 05:24:10
问题 I am programming several search functions, for which I use the Node datatype: data Node a = Node { [...] , getPath :: [a] -- ^ Previous states this node has visited } That field, getPath , is what I use to check if I have previously visited that state in that node: when expanding a new node, I check that by doing: visited = `elem` path It works, but it becomes incredibly costly when there are a lot of nodes expanded and the paths become too long. Is there a better way to keep track of the

Scheme - define variable as the result of a function?

风流意气都作罢 提交于 2020-01-06 05:23:13
问题 The beginning of one of my programs results in an error. This is the problem area. I am trying to define a variable as the result of a recursive function. (define (test n) (define (a1func i) (if (= i 1) 0 (+ (/ 1 i) (a1func (- i 1))))) (define a1 (a1func (- n 1)))) if you were to give it say (test 10) the error would be: procedure application: expected procedure, given: #<undefined> ; arguments were: 9 I assumed this could be done in Scheme?? ideas? 回答1: In pure FP languages computations are

summing elements from a user defined datatype

旧巷老猫 提交于 2020-01-06 05:00:27
问题 Upon covering the predefined datatypes in f# (i.e lists) and how to sum elements of a list or a sequence, I'm trying to learn how I can work with user defined datatypes. Say I create a data type, call it list1: type list1 = A | B of int * list1 Where: A stands for an empty list B builds a new list by adding an int in front of another list so 1,2,3,4, will be represented with the list1 value: B(1, B(2, B(3, B(4, A)))) From the wikibook I learned that with a list I can sum the elements by doing

summing elements from a user defined datatype

馋奶兔 提交于 2020-01-06 05:00:05
问题 Upon covering the predefined datatypes in f# (i.e lists) and how to sum elements of a list or a sequence, I'm trying to learn how I can work with user defined datatypes. Say I create a data type, call it list1: type list1 = A | B of int * list1 Where: A stands for an empty list B builds a new list by adding an int in front of another list so 1,2,3,4, will be represented with the list1 value: B(1, B(2, B(3, B(4, A)))) From the wikibook I learned that with a list I can sum the elements by doing

Z3 prime numbers

谁都会走 提交于 2020-01-06 04:03:50
问题 I am trying to learn z3, and this is the first program I write. In this exercise, I am trying to determine if x is prime. If x is prime, return SAT, otherwise, return UNSAT alongside with two of its factors. Here is what I have so far http://rise4fun.com/Z3/STlX My problem is I don't think the code is doing anything right now. It returns SAT for whatever I do. i.e if I assert that 7 is prime, it returns SAT, if I assert 7 is not prime, it returns SAT. I am not sure how recursion works in z3,

Java 8 conditional .map() (or map with identity function)

一世执手 提交于 2020-01-06 03:39:47
问题 Suppose we have boolean flag to turn on/off map in a stream. For example to trim or not. Are the below examples proper solution or there is a better way to implement that? boolean doTrim = true; optionalValue.map(doTrim ? String::trim : (x) -> x ).get()... or: boolean doTrim = true; optionalValue.map(doTrim ? String::trim : Function.identity() ).get()... 回答1: You are over-complicating things. If you have an Optional<String> optionalValue you can simply say: if(doTrim) optionalValue

Longest common sublist

故事扮演 提交于 2020-01-06 02:47:06
问题 While trying to write a solution to the longest common sublist problem in Scheme, I'm having trouble figuring out what is wrong with what I have so far. I think it's the right idea and before worrying about polynomial time I'm just trying to get one that works at all. I haven't written in a functional language before and the syntactic differences can make things a little harder at first. (define (lcs lst1 lst2) (if (or (null? lst1) (null? lst2)) '() (if (not (null? lcs)) lcs (if (equal? (car

Group arrays by parent ids object Ramda

爱⌒轻易说出口 提交于 2020-01-06 02:20:15
问题 Need a help from Ramda community... I have an object array that I need to sort by "chapter_id" and after sorting, just remove "chapter_id": const stuff = [ { id: 1, title: "hello world", chapter_id: "4321" }, { id: 2, title: "new title", chapter_id: "21" }, { id: 3, title: "...", chapter_id: "33" }, { id: 4, title: "huh!?", chapter_id: "14" }, { id: 5, title: "From Earth", chapter_id: "11" }, { id: 6, title: "alien", chapter_id: "11" }, { id: 7, title: "Saturn", chapter_id: "11" }, { id: 8,

Erlang - How Can I Parse RFC1123 Dates Into An Erlang Term?

假如想象 提交于 2020-01-06 01:33:49
问题 Without using a third party module, what steps would I need to take to convert this: <<"Mon, 17 Feb 2014 11:07:53 GMT">> Into this?: [17, 2, 2014, 10, 07, 53] Most of the answers I've Googled suggest using a library. So far, I suspect I'd get somewhere by pattern matching the formatted date string. Something like: <<_:5/binary, Date:2/binary>> = <<"Mon, 17 Feb 2014 11:07:53 GMT">>... Which I think should produce the following 'match' Date = 17... That's based on an idea found here - https:/

F# Using List.map on an array of strings

走远了吗. 提交于 2020-01-06 01:28:20
问题 I'm trying to use F#'s List.map function to call a function I've written on every string in the array. Here is the function I've written (*Takes a string and filters it down to common text characters*) let filterWord wordToFilter = Regex.Replace(wordToFilter, "[^a-zA-Z0-9/!\'?.-]", ""); and here is my main method where I call it (*Main method of the program*) [<EntryPoint>] let main argsv = let input = File.ReadAllText("Alice in Wonderland.txt"); //Reads all the text into a single string let