fold

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

Using reduce over a tree in Lisp

百般思念 提交于 2019-12-20 04:50:13
问题 To fold a flat list in Lisp you use reduce : * (reduce #'+ '(1 2 3 4 5)) 15 But what if I have an arbitrarily complex tree, and I want to apply a function over each of the element? So that fold over '(1 (2) (3 (4) 5)) would still give 15 ? I tried to use reduce , but I had to provide a custom function, which kinda defeats the purpose: (defun tree+ (a b) (cond ((null b) 0) ((atom b) (+ a b)) (t (+ (tree+ a (car b)) (tree+ 0 (cdr b)))))) (reduce #'tree+ '(1 (2) (3 (4) 5)) :initial-value 0) ;

Reverse a list in Scheme with foldl and foldr

浪子不回头ぞ 提交于 2019-12-20 01:34:43
问题 How can you define a function to reverse a list in Scheme by using foldr and foldl ? What we want is a succinct solution to reverse a list in Scheme using a foldl call and a different solution using a foldr call, as defined: (define (foldl operation lst initial) (if (null? lst) initial (foldl operation (cdr lst) (operation (car lst) initial)))) and (define (foldr operation lst initial) (if (null? lst) initial (operation (car lst) (foldr operation (cdr lst) initial)))) The astute person will

Haskell recursive function example with foldr

纵饮孤独 提交于 2019-12-19 17:35:32
问题 I've taken up learning Haskell again, after a short hiatus and I am currently trying to get a better understanding of how recursion and lambda expressions work in Haskell. In this: YouTube video, there is a function example that puzzles me far more than it probably should, in terms of how it actually works: firstThat :: (a -> Bool) -> a -> [a] -> a firstThat f = foldr (\x acc -> if f x then x else acc) For the sake of clarity and since it wasn't immediately obvious to me, I'll give an example

How to fold by a tag a group of selected (neighbor) tags with XSLT1?

主宰稳场 提交于 2019-12-19 10:43:13
问题 I have a set of sequential nodes that must be enclosed into a new element. Example: <root> <c>cccc</c> <a gr="g1">aaaa</a> <b gr="g1">1111</b> <a gr="g2">bbbb</a> <b gr="g2">2222</b> </root> that must be enclosed by fold tags, resulting (after XSLT) in: <root> <c>cccc</c> <fold><a gr="g1">aaaa</a> <b gr="g1">1111</b></fold> <fold><a gr="g2">bbbb</a> <b gr="g2">2222</b></fold> </root> So, I have a "label for grouping" ( @gr ) but not imagine how to produce correct fold tags. I am trying to use

Idiomatic construction to check whether a collection is ordered

不打扰是莪最后的温柔 提交于 2019-12-18 10:13:47
问题 With the intention of learning and further to this question, I've remained curious of the idiomatic alternatives to explicit recursion for an algorithm that checks whether a list (or collection) is ordered. (I'm keeping things simple here by using an operator to compare and Int as type; I'd like to look at the algorithm before delving into the generics of it) The basic recursive version would be (by @Luigi Plinge): def isOrdered(l:List[Int]): Boolean = l match { case Nil => true case x :: Nil

Sml folding a tree

时间秒杀一切 提交于 2019-12-18 07:17:18
问题 I am trying to get the product of a tree by using the fold function so far this is what I have. I am confused on how to use the fold method while transversing the tree datatype 'a bin_tree = Leaf of 'a | Node of 'a bin_tree * 'a bin_tree fun treefold g z Empty = z | treefold g z (Node (l, x, r)) = g(x, g(treefold g z l, treefold g z r) 回答1: First, some pointers about what isn't quite in order in your attempt. The base case of your treefold function matches against the value constructor Empty

Why doesn't Option have a fold method?

…衆ロ難τιáo~ 提交于 2019-12-17 22:26:48
问题 I wonder why scala.Option doesn't have a method fold like this defined: fold(ifSome: A => B , ifNone: => B) equivalent to map(ifSome).getOrElse(ifNone) Is there no better than using map + getOrElse ? 回答1: You can do: opt foldLeft (els) ((x, y) => fun(x)) or (els /: opt) ((x,y) => fun(x)) (Both solutions will evaluate els by value, which might not be what you want. Thanks to Rex Kerr for pointing at it.) Edit: But what you really want is Scalaz’s catamorphism cata (basically a fold which not

Haskell - foldl and foldr?

核能气质少年 提交于 2019-12-17 17:25:13
问题 Is difference between foldl and foldr just the direction of looping? I thought there was a difference in what they did, not just in the direction? 回答1: There's a difference if your function isn't associative (i.e. it matters which way you bracket expressions) so for example, foldr (-) 0 [1..10] = -5 but foldl (-) 0 [1..10] = -55 . On a small scale, this is because 10-(20-(30)) isn't the same as ((10)-20)-30 . Whereas because (+) is associative (doesn't matter what order you add subexpressions

Cross Validation in Weka

风格不统一 提交于 2019-12-17 15:44:08
问题 I've always thought from what I read that cross validation is performed like this: In k-fold cross-validation, the original sample is randomly partitioned into k subsamples. Of the k subsamples, a single subsample is retained as the validation data for testing the model, and the remaining k − 1 subsamples are used as training data. The cross-validation process is then repeated k times (the folds), with each of the k subsamples used exactly once as the validation data. The k results from the