cons

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

大憨熊 提交于 2019-12-17 23:26: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

Scheme: how to produce '(5 . (5))

旧巷老猫 提交于 2019-12-13 20:12:42
问题 I have tried all kinds of combinations of cons and append to produce '(5 . (5)) but I couldn't. Is there any way? 回答1: At the risk of sounding like Bill Clinton, it depends on what you mean by "produce". If you mean "produce a value that prints on the screen as '(5 . (5)) , then you're sort of out of luck, because this value prints as '(5 5) . For a similar example: how do I produce the number 1e-1 ? Well, try typing it in; this is the same as 0.1, and if you type in 1e-1, it's going to print

Lisp difference between (cons 'a (cons 'b 'c)) and (cons 'a '(b.c))

谁都会走 提交于 2019-12-12 13:11:00
问题 What's the difference between: (cons 'a (cons 'b 'c)) ;; (A B . C) and (cons 'a '(b.c)) ;; (A B.C) I need to create the following list ((a.b).c) using cons so i'm trying to understand what that "." represents. L.E. : I have the following (cons (cons 'a 'b) 'c) but it produces ((A . B) . C) and not ((A.B).C) (Note the extra spaces) 回答1: Spaces are used to separate list tokens. A.B is a single token. (A.B) is a list with a single element. (A . B) is a cons cell with A as car and B as cdr. A

(x:xs) pattern Haskell logic

扶醉桌前 提交于 2019-12-12 02:56:57
问题 Let's say there is a simple function: maximum' :: (Ord a) => [a] -> a maximum' [] = error "maximum of empty list" maximum' [x] = x maximum' (x:xs) = max x (maximum' xs) I understand the idea and what (x:xs) does. As it was explained in details here What do the parentheses signify in (x:xs) when pattern matching? but there is one little thing that I cannot get out of my head. Since cons : operator appends x to a list xs, why is it that x is the first element of function argument list and xs is

Cons in scheme explanation

此生再无相见时 提交于 2019-12-11 03:56:56
问题 (cons 1 2) gives us (1 . 2) . (cons 3 4) gives us (3 . 4) . So why does (cons (cons 1 2) (cons 3 4)) give us ((1 . 2) 3 . 4) ? Why isn't it ((1 . 2) (3 . 4)) ? 回答1: Well, it wouldn't be ((1 . 2) (3 . 4)) because that would be a list containing two elements, each a cons pair. I'm guessing what you meant was: why isn't it ((1 . 2) . (3 . 4)) ? Well, actually the following two expressions are equivalent: '((1 . 2) . (3 . 4)) '((1 . 2) 3 . 4) This has to do with how Scheme's dotted notation works

c(a|d)+r macro in Racket

半腔热情 提交于 2019-12-05 04:21:43
问题 I wonder if it's possible to write a macro in Racket that would translate every form of shape (c(a|d)+r xs), where c(a|d)+r is a regular expression matching car, cdr, caar, cadr, ... etc, into the corresponding composition of first and rest. For example, this macro should take (caadr '(1 2 3 4 5)) and transform that to (first (first (rest '(1 2 3 4 5)))). Something like this in Shen (Mark Tarver's new programming language): https://groups.google.com/group/qilang/browse_thread/thread

c(a|d)+r macro in Racket

不羁岁月 提交于 2019-12-03 20:37:38
I wonder if it's possible to write a macro in Racket that would translate every form of shape (c(a|d)+r xs), where c(a|d)+r is a regular expression matching car, cdr, caar, cadr, ... etc, into the corresponding composition of first and rest. For example, this macro should take (caadr '(1 2 3 4 5)) and transform that to (first (first (rest '(1 2 3 4 5)))). Something like this in Shen (Mark Tarver's new programming language): https://groups.google.com/group/qilang/browse_thread/thread/131eda1cf60d9094?hl=en It is very possible to do exactly that in Racket, and in a much shorter way than done

clojure cons vs conj with lazy-seq

末鹿安然 提交于 2019-12-03 05:42:14
问题 Why does cons work in this context with lazy-seq, but conj doesn't? This works: (defn compound-interest [p i] (cons p (lazy-seq (compound-interest (* p (+ 1 i)) i)))) This doesn't (it gives a stack overflow[1] exception): (defn compound-interest2 [p i] (conj (lazy-seq (compound-interest2 (* p (+ 1 i)) i)) p)) [1] Oh ya! Asking a question involving a stack overflow on stackoverflow. 回答1: (conj collection item) adds item to collection . To do that, it needs to realize collection . (I'll explain

What does “my other car is a cdr” mean?

≯℡__Kan透↙ 提交于 2019-12-03 04:39:13
问题 Can anyone well versed in lisp explain this joke to me? I've done some reading on functional programming languages and know that CAR/CDR mean Contents of Address/Decrement Register but I still don't really understand the humour. 回答1: In Lisp, a linked list element is called a CONS. It is a data structure with two elements, called the CAR and the CDR for historical reasons. (Some Common Lisp programmers prefer to refer to them using the FIRST and REST functions, while others like CAR and CDR

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

◇◆丶佛笑我妖孽 提交于 2019-12-03 03:47:48
问题 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]