Lisp Move Elements in a List - list

I have a list of the form:
(or a b c (and d e) f g (and h i) (==> x y))
and I like to move the sublists beginning with and after the or like this:
(or (and d e) (and h i) a b c f g (==> x y))
How can I do this? I'm not sure what's the best way since it's a list and I can't just put an element whatever I want, like I can with other data structures.

? (stable-sort (rest '(or a b c (and d e) f g (and h i) (==> x y)))
(lambda (x y)
(and (consp x) (eq (first x) 'and))))
((AND H I) (AND D E) A B C F G (==> X Y))

Related

Non-empty-list comonad

I have been meditating on comonads and have an intuition that a non-empty list ("full list") is a comonad. I have constructed a plausible implementation in Idris and have worked on proving the comonad laws but have not been able to prove the recursive branch of one of the laws. How do I prove this (the ?i_do_not_know_how_to_prove_this_if_its_provable hole)--or am I wrong about my implementation being a valid comonad (I've looked at the Haskell NonEmpty comonad implementation and it seems to be the same as mine)?
module FullList
%default total
data FullList : Type -> Type where
Single : a -> FullList a
Cons : a -> FullList a -> FullList a
extract : FullList a -> a
extract (Single x) = x
extract (Cons x _) = x
duplicate : FullList a -> FullList (FullList a)
duplicate = Single
extend : (FullList a -> b) -> FullList a -> FullList b
extend f (Single x) = Single (f (Single x))
extend f (Cons x y) = Cons (f (Cons x y)) (extend f y)
extend_and_extract_are_inverse : (l : FullList a) -> extend FullList.extract l = l
extend_and_extract_are_inverse (Single x) = Refl
extend_and_extract_are_inverse (Cons x y) = rewrite extend_and_extract_are_inverse y in Refl
comonad_law_1 : (l : FullList a) -> extract (FullList.extend f l) = f l
comonad_law_1 (Single x) = Refl
comonad_law_1 (Cons x y) = Refl
nesting_extend : (l : FullList a) -> extend f (extend g l) = extend (\x => f (extend g x)) l
nesting_extend (Single x) = Refl
nesting_extend (Cons x y) = ?i_do_not_know_how_to_prove_this_if_its_provable
Notice that your goal is of the following form:
Cons (f (Cons (g (Cons x y)) (extend g y))) (extend f (extend g y)) =
Cons (f (Cons (g (Cons x y)) (extend g y))) (extend (\x1 => f (extend g x1)) y)
You basically need to prove that the tail parts are equal:
extend f (extend g y) = extend (\x1 => f (extend g x1)) y
But that is exactly what the induction hypothesis (nesting_extend y) says! Hence, the proof is quite trivial:
nesting_extend : (l : FullList a) -> extend f (extend g l) = extend (f . extend g) l
nesting_extend (Single x) = Refl
nesting_extend (Cons x y) = cong $ nesting_extend y
I used the congruence lemma cong:
cong : (a = b) -> f a = f b
which says that any function f maps equal terms into equal terms.
Here Idris infers that f is Cons (f (Cons (g (Cons x y)) (extend g y))), where the f inside Cons refers to nesting_extend's parameter f.

How do I accumulate repeated application of a function to a list of values?

How do I accumulate the successive application of a function to a list of values in Haskell?
I'm not sure I'm phrasing that right, but what I'm looking for, for example, is that I have a list values of type X,
l = [a, b, c, d, e]
and a function
f :: X -> X -> X
and I want
(f e (f d (f c (f b a))))
which I think can perhaps be expressed with $ somehow.
I see that Haskell has some fold functions, but I can't quite figure out how to get it to work like the fold operations I'm familiar with.
Isn't this just foldl1 (flip f) l? You want a left fold, and you want the operation to start with the first item in the list rather than a specific accumulator.
Well, your function f really has type:
f :: X -> X -> X
Then the expression you are interested in is:
foldr1 f [e, d, c, a, b] -- note the twist at the end.
If you want to compute:
f e (f d (f c (f b a))) -- note last term is: f b a
then it is just:
foldr1 f [e, d, c, b, a]
Play with simple-reflect to figure it out.
ghci> import Debug.SimpleReflect
ghci> foldl1 f [a,b,c,d,e]
f (f (f (f a b) c) d) e
ghci> foldl (flip f) z [a,b,c,d,e]
f e (f d (f c (f b (f a z))))
And so on...

Generate all possibilities in scheme from a list

I have a list of sublists:
((a b c) (e f) (z h))
and i want to generate something like this:
((a e z) (a f z) (a e h) (a f h) (b e z) (b e h) ... ) and so on.
I want, given a list of sublist, to generate all possibilities of sublists that contains an element from each of the input's sublists.
How can i get this ouput?
You're describing the cartesian product of a list of lists, here's a possible implementation (works in Racket):
(define (cartesian-product lsts)
(foldr (lambda (lst acc)
(for*/list ((x (in-list lst))
(y (in-list acc)))
(cons x y)))
'(())
lsts))
Now, if you're not using Racket, here's a vanilla implementation using mostly standard procedures; it should work on any Scheme interpreter that defines a fold-right-like procedure:
(define (flatmap f lst)
(apply append (map f lst)))
(define (cartesian-product lsts)
(foldr (lambda (lst acc)
(flatmap (lambda (x)
(map (lambda (y)
(cons x y))
acc))
lst))
'(())
lsts))
Either way, it works as expected:
(cartesian-product '((a b c) (e f) (z h)))
=> '((a e z) (a e h) (a f z) (a f h) (b e z) (b e h)
(b f z) (b f h) (c e z) (c e h) (c f z) (c f h))

Clojure - indexing a sequence without using nexts

New to Clojure and trying to figure out how to index a sequence without using lots of nexts. For instance say I have the sequence:
(a b c d e f g h)
and I want to incorporate into a function the returning of the 4th item of the sequence. There must be some way besides (next (next (next sequence_name)))? So I could just pass the number 4 to the function (or any other number) and get that item from the sequence. Thanks!
A few different ways:
(take 1 (drop 3 '(a b c d e f g h))) ;; d
(nth '(a b c d e f g h) 3) ;; d
(nth [a b c d e f g h] 3) ;; d
(nth (vec '(a b c d e f g h)) 3) ;;d
I recommend you become familiar with the sequence manipulation functions in the Clojure Cheat Sheet - it's totally worth it. Clojure's sequence library is extremely rich.
good old nth should do the trick
user> (nth '(a b c d e f g h) 4)
e
(that is indexed from 0 of course)

applying list of functions to list in common lisp

I have a list of functions, a list of elements, and I'd like to apply all the functions on all the elements then append all the resulting lists together. I did it as follow
(defun apply-functions(funcs elements)
(if (null funcs)
nil
(append (mapcar #'(lambda (x) (funcall (car funcs) x)) elements) (apply-functions (rest funcs) elements))))
It works as intended, but I don't like it. Is there a cleaner, more concise way of doing it?. I am new to lisp, and still getting used to the lispish style of doing things.
I don't know if you like loop macro (and I don't want to spoil anyone), but try this:
(defun apply-functions (fs es)
(loop for f in fs appending (mapcar f es)))
This is the same idea as yours, just shorter:
(defun apply-functions (functions elements)
(mapcan #'(lambda (x) (mapcar x elements)) functions))
I would define a function, call-each that returns a new function,
returning the list of calling each function on it's argument:
(defun call-each (fns)
(lambda (arg)
(mapcar (lambda (fn)
(funcall fn arg))
fns)))
(funcall (call-each (list #'third #'second #'first)) '(a b c))
;=> (C B A)
cl has the function mapcan which is basically nconc + mapcar :
(mapcan #'reverse '((a b c)
(e f g)
(h i j)))
;=> (C B A G F E J I H)
(mapcan (call-each (list #'identity #'1+)) '(1 3 5 7 9))
;=> (1 2 3 4 5 6 7 8 9 10)
unfortunately, nconc, which mapcan uses, is destructive:
(let ((data '((a b c)
(d e f)
(g h i))))
;;here be dragons
(list (mapcan #'identity data)
data))
;=> ((A B C D E F G H I) ((A B C D E F G H I) (D E F G H I) (G H I)))
alexandria to the rescue:
(let ((data '((a b c)
(d e f)
(g h i))))
;;safe version
(list (alexandria:mappend #'identity data)
data))
;=> ((A B C D E F G H I) ((A B C) (D E F) (G H I)))
note that using mapcan is more efficient, but unless you know exactly where
your data is coming from, and who owns it, mappend is the way to go.
so you could write:
(defun apply-functions (fs es)
(when fs
(alexandria:mappend (call-each fs) es))
(apply-functions (list #'identity #'1+) '(1 3 5 7 9))
;=> (1 2 3 4 5 6 7 8 9 10)