cons

Understanding infix method call and cons operator(::) in Scala

巧了我就是萌 提交于 2019-12-02 17:14:28
I'm quite new to Scala programming language, and was trying something out stucked in my mind while I was following the lecture notes at here . I think I couldn't really understand how cons operator works, here are some things I tried: I've created a pseudo-random number generator, then tried to create a list of one random value: scala> val gen = new java.util.Random gen: java.util.Random = java.util.Random@1b27332 scala> gen nextInt 3 :: Nil <console>:7: error: type mismatch; found : List[Int] required: Int gen nextInt 3 :: Nil ^ But it tried to pass List(3) to nextnt method. When i used

LISP cons in python

China☆狼群 提交于 2019-12-01 03:49:54
Is there an equivalent of cons in Python? (any version above 2.5) If so, is it built in? Or do I need easy_install do get a module? In Python, it's more typical to use the array-based list class than Lisp-style linked lists. But it's not too hard to convert between them: def cons(seq): result = None for item in reversed(seq): result = (item, result) return result def iter_cons(seq): while seq is not None: car, cdr = seq yield car seq = cdr >>> cons([1, 2, 3, 4, 5, 6]) (1, (2, (3, (4, (5, (6, None)))))) >>> iter_cons(_) <generator object uncons at 0x00000000024D7090> >>> list(_) [1, 2, 3, 4, 5,

LISP cons in python

时光毁灭记忆、已成空白 提交于 2019-12-01 01:02:05
问题 Is there an equivalent of cons in Python? (any version above 2.5) If so, is it built in? Or do I need easy_install do get a module? 回答1: In Python, it's more typical to use the array-based list class than Lisp-style linked lists. But it's not too hard to convert between them: def cons(seq): result = None for item in reversed(seq): result = (item, result) return result def iter_cons(seq): while seq is not None: car, cdr = seq yield car seq = cdr >>> cons([1, 2, 3, 4, 5, 6]) (1, (2, (3, (4, (5,

Building a compile time list incrementally in C++

霸气de小男生 提交于 2019-11-30 18:14:42
问题 In C++, is there a way to build a compile time list incrementally, in the following pattern? START_LIST(List) ADD_TO_LIST(List, int) ADD_TO_LIST(List, float) ADD_TO_LIST(List, double) END_LIST(List) The result of this should be equivalent to: using List = Cons<int, Cons<float, Cons<double, Nil>>>; I also have a restriction that the space between the macros needs to be at whatever scope the whole thing is. I'm planning to define things and register them with the list at the same time using a

What's the difference between `::` and `+:` for prepending to a list)?

大兔子大兔子 提交于 2019-11-28 22:22:25
List has 2 methods that are specified to prepend an element to an (immutable) list: +: (implementing Seq.+: ), and :: (defined only in List ) +: technically has a more general type signature— def +:[B >: A, That](elem: B)(implicit bf: CanBuildFrom[List[A], B, That]): That def ::[B >: A](x: B): List[B] —but ignoring the implicit, which according to the doc message merely requires That to be List[B] , the signatures are equivalent. What is the difference between List.+: and List.:: ? If they are in fact identical, I assume +: would be preferred to avoid depending on the concrete implementation

Lisp, cons and (number . number) difference

*爱你&永不变心* 提交于 2019-11-28 02:04:51
What is the difference between (cons 2 3) and '(2 . 3) in Lisp? '(2 . 3) is a dotted pair. (cons 2 3) creates a dotted pair too. So these should evaluate to the same thing. So one is a literal for a dotted pair, the other one creates a dotted pair. They are not exactly the same, even though they evaluate to the same values in the REPL. Consider these examples, in which cons cells are modified destructively: TEST> (defun literal-cons () (let ((cons '(1 . 2))) (incf (cdr cons)) cons)) LITERAL-CONS TEST> (literal-cons) (1 . 3) TEST> (literal-cons) (1 . 4) TEST> (literal-cons) (1 . 5) Compared to

Lisp, cons and (number . number) difference

痴心易碎 提交于 2019-11-27 04:50:53
问题 What is the difference between (cons 2 3) and '(2 . 3) in Lisp? 回答1: '(2 . 3) is a dotted pair. (cons 2 3) creates a dotted pair too. So these should evaluate to the same thing. So one is a literal for a dotted pair, the other one creates a dotted pair. 回答2: They are not exactly the same, even though they evaluate to the same values in the REPL. Consider these examples, in which cons cells are modified destructively: TEST> (defun literal-cons () (let ((cons '(1 . 2))) (incf (cdr cons)) cons))

Cons element to list vs cons list to element in Scheme

做~自己de王妃 提交于 2019-11-26 17:03:12
问题 What's the difference between using cons to combine an element to a list and using cons to combine a list to an element in scheme? Furthermore, how exactly does cons work? Does it add element to the end of the list or the beginning? Thanks! 回答1: The primitive cons simply sticks together two things, the fact that some of those things are considered lists is incidental. For instance, this works and creates a pair (also known as a cons cell ): (cons 1 2) => '(1 . 2) ; a pair Now, if the second