How to remove nested list (of lists)? - list

I would like to remove nested list from a list of lists. I'm pretty new to Haskell and found this difficult.
For example
[[a],[b],[c]] --> [a,b,c]

You are likely looking for the concat :: Foldable f => f [a] -> [a] function. This can take a Foldable type of [a]s, and return an [a] as a result. Since a list is a Foldable type, that thus means that concat can be "specialized" to [[a]] -> [a]. It will thus concatenate the sublists together into a list.
For example:
Prelude> concat [[1],[2],[3]]
[1,2,3]
This can work on other Foldable types as well, like Maybe, Tree, etc.

Related

How do I write a nested list of lists with my custom list implementation?

I am wondering whether my definition of a list is less capable than the regular implementation. I am having problems creating a list of lists with the below definition:
data List a =
Nil
| Cons a (List a)
deriving (Eq, Ord, Show)
I do not see how to create a list like [[2],[3]] with my definition of list.
The simplest instance of a nested list is just [[]] which I think I can recreate with Cons Nil Nil. The second simplest instance of a nested list is [[1]]. This is Cons (Cons 1 Nil) Nil afaics.
However, like I said do not see how to create the list [[2],[3]] with my definition above. How do I do it? (It should be possible, since an exercise asks me to create a monad instance for the list definition above).
Let's first consider, how would you express [x,y]?
Cons x (Cons y Nil)
Now put in x = [2]:
Cons (Cons 2 Nil) (Cons y Nil)
and y = [3]:
Cons (Cons 2 Nil) (Cons (Cons 3 Nil) Nil)

All-to-all pairings of two lists in Haskell without list comprehension

I am reading through Bird and Wadler's nice book in Functional programming
and trying to solve the exercises in Haskell.
The chapter on Lists has a section which says that any list comprehension
can be implemented in terms of map, filter and concat and subsidiary functions which one might need for map
I am having trouble implementing the following expression in terms of these building blocks.
[(x,y) | x<- xs, y<- ys]
How would one use map and filter for this?
I got as far as doing
concat [ map (\ a -> (a,x)) ys | x<-xs ]
I suspect we have to use currying cleverly here, but i can't seem to figure it out.
well for each x you need all ys so:
\x -> map (\y -> (x,y)) ys
now you only need to map this over all xs
cross xs ys = map (\x -> map (\y -> (x,y)) ys) xs
example:
> cross [1..3] "AB"
[[(1,'A'),(1,'B')],[(2,'A'),(2,'B')],[(3,'A'),(3,'B')]]
While I'm not sure if ApplicativeDo is allowed as part of your "building blocks", but if it is, this can easily be implemented by mapping the tuple function over the first list, and applying that list to the second.
import Control.Applicative
let xs = [1,2,3]
ys = [4,5,6]
(,) <$> xs <*> ys
Result:
[(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)]

How do I find the ordering that sorts a list in Haskell?

In Haskell, how do I find the ordering that sorts a given list — that is, the indices to apply to the list that would result in its being sorted?
Essentially what I'm looking for is the Haskell equivalent of Mathematica's Ordering.
Is this what you are looking for?
ordering :: Ord a => [a] -> [Int]
ordering xs = map snd $ sort (zip xs [0..])

MIT Scheme Expression alternative form of append

I am currently attempting to resolve a question in a practice mid-term. The question asks for me to write an expression to append two lists (let us call them list1 and list2), and list2 must be appended to the end of list1. The function append cannot be used at any point in this. What I may use is cons, filter, accumulate, map, list-ref, and inumerate-interval. I have attempted various forms of getting to the solution, such as
(cons list1 list2)
(filter list? (map list (cons list1 list2)))
(list list1 list2)
(map list (list list1 list2))
I have spent 2 days attempting to find a solution to no avail. If anyone is able to guide me in the right direction, or even provide me with some form of assistance, I would be grateful.
Also, I apologize if there is some protocol that I am following incorrectly for the code formatting or the mannerisms in asking the question, as I am new to the site. Thank you.
Because this is homework, I can't give you a straight answer. Instead, I'll give you some hints, you can find the answer to your own question filing-in the blanks. This is the standard way to implement append:
(define (my-append l1 l2)
(cond (<???> ; if the first list is null
<???>) ; then return the second list
(<???> ; if the second list is null
<???>) ; then return the first list
(else ; otherwise `cons`
(cons <???> ; the first element of the first list
(my-append <???> l2))))) ; process the rest of the first list
The above solution uses cond, null?, cons, car and cdr. If you can't use any of those and you're restricted to the procedures in the question, try this instead (assuming accumulate is defined as a fold to the right):
(define (my-append l1 l2)
(accumulate
<???> ; what should be used for sticking list elements together?
<???> ; what should we return if the list being traversed is empty?
<???>)) ; this is the list that we want to traverse
The above solution only uses accumulate and cons, as requested in the question. The idea is: traverse the first list recreating it element by element, until the list is exhausted - at that point, the next element will be the second list.

Lisp, a couple of questions about lists and recursion

sorry to overflow with so many questions.
I have the following:
(defun recursive-function (string) "returns list of strings"
;I am trying to return flat list
; create list L
(append (mapcar 'recursive-function L)))
But since recursive-function returns a list, I end up with a list of a list of a list..., whereas I want just a flat list.
What is the proper way for me to implement recursion on functions which take a scalar and return a list of scalars?
Thanks.
If I understood correctly, you can combine reduce and append to flatten the list before returning it.
Example:
(reduce 'append '((1) (2) (3)))
Output:
(1 2 3)
In your case this might work:
(reduce 'append (mapcar 'recursive-function L))
I belive you are looking for mapcan:
[...] mapcan and mapcon are like mapcar and
maplist respectively, except that the
results of applying function are
combined into a list by the use of
nconc rather than list. [...]
(defun recursive-function (string) "returns list of strings"
;I am trying to return flat list
; create list L
(mapcan 'recursive-function L))