How to add brackets to each item in a list? - clojure

I am trying to make a method that adds a bracket to each item in a list, e.g '(h e l l o) will become '((h) (e) (l) (l) (o)).

Something like this:
(map list '(h e l l o))

Related

How do I filter a list without using List.filter in OCaml?

I have to write a function that, given two lists, it returns a list of the elements of the first one whose square is present in the second one (sry for my english). I can't do it recursively and i can't use List.filter.
this is what i did:
let lst1= [1;2;3;4;5];;
let lst2= [9;25;10;4];;
let filquadi lst1 lst2 =
let aux = [] in
List.map(fun x -> if List.mem (x*x) lst2 then x::aux else []) lst1;;
It works but it also prints [] when the number doesn't satisfy the if statement:
filquadi lst1 lst2 ;;
- : int list list = [[]; [2]; [3]; []; [5]]
how can I return a list of numbers instead of a list of a list of numbers?
- : int list = [2;3;5]
You can use List.concat to put things together at the end:
List.concat (List.map ...)
As a side comment, aux isn't doing anything useful in your code. It's just a name for the empty list (since OCaml variables are immutable). It would probably be clearer just to use [x] instead of x :: aux.
As another side comment, this is a strange sounding assignment. Normally the reason to forbid use of functions from the List module is to encourage you to write your own recursive solution (which indeed is educational). I can't see offhand a reason to forbid the use of recursion, but it's interesting to combine functions from List in different ways.
Your criteria don't say you can't use List.fold_left or List.rev, so...
let filter lst1 lst2 =
List.fold_left
(fun init x ->
if List.mem (x * x) lst2 then x::init
else init)
[] lst1
|> List.rev
We start with an empty list, and as we fold over the first list, add the current element only if that element appears in the second list. Because this results in a list that's reversed from its original order, we then reverse that.
If you're not supposed to use recursion, this is technically cheating, because List.fold_left works recursively, but then so does basically anything working with lists. Reimplementing the List module's functions is going to involve a lot of recursion, as can be seen from reimplementing fold_left and filter.
let rec fold_left f init lst =
match lst with
| [] -> init
| x::xs -> fold_left f (f init x) xs
let rec filter f lst =
match lst with
| [] -> []
| x::xs when f x -> x :: filter f xs
| _::xs -> filter f xs

Ocaml : transform a pattern matching expression into if expression (if... then... else)

I'm trying to write this pattern matching recursion expression
let rec drop_last l =
match l with
| [] -> []
| [_] -> []
| h::t -> h :: drop_last t ;;
as an 'if statement' recursion expression.
I've started doing the following :
let rec drop_last2 l =
if l = [] then [] else
if l = [_] then [] else
l = List.hd::List.tl then List.hd::drop_last2 (List.tl l);;
However, I'm getting a syntax error from the compiler. Could someone please tell me how should I modify this if statement into something correct?
As Jeffrey said, [_] is a pattern not a value, you can't compare to it.
and in l = List.hd::List.tl first of all it's incorrect, List.hd is a function that takes a list and returns it's it's head, List.tl is also a function that takes a list but this one returns it's tail, and the constructor _::_ takes a value and a list of values of the same type. So you should really check the documentation (https://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html) for more information on how to use functions and costructors.
to go from this:
let rec drop_last l =
match l with
| [] -> []
| [_] -> []
| h::t -> h :: drop_last t ;;
to an if-statement, you can do it this way:
let rec drop_last l =
if List.length l <= 1
then []
else List.hd l :: drop_last (List.tl l) ;;
That is because the pattern [] checks if the list is empty, so it can be replaced by List.length l = 0 and [_] checks if the list has one element, so it can be replaced by List.length l = 1 and since in both those cases you return [], you can join them with List.length l <= 1 and List.hd requires an argument that list a list, so it needs to be applied to l to give you the head, then appended to the recursive call with the tail to create a new list to be returned.
The construct [_] is a pattern, not a value. So you can't use = to compare it to a value as you are trying to do.
The thing to do, I believe, is to concentrate on thinking about the length of the list. The pattern [] matches when the length is 0 and the pattern [_] matches when the length is 1. You can use these facts in your if statements.

How can I sum the middle elements of an expanding list in Haskell?

So far I know how to expand a list from its ends, but they end up getting doubled because of the first condition, which is to double a singleton. Would it make sense for the code to be like this:
sumExpand :: [Integer] -> [Integer]
sumExpand l = expand l []
where
expand [] a = a
expand (x:[]) a = x: expand [] (x:a)
expand (x:xs) a = expand (x:a) (expand xs a)
And for me to work on its output:
[1,1,2,2,3,3] from [1,2,3]
instead of [1,3,5,3]
The latter being my desire? Here's how I got to a temporary solution for a list of two elements:
expand (x:xs) a = x: tail (expand (map (x+) xs) (last xs:a))
Output:
*Main> sumExpand [1,2]
[1,3,2]
*Main> sumExpand [1,2,3]
[1,7,4,3]
EDIT: basically, I want the algorithm to work like this: [a, b, c] => [a, a+b, b+c, c]
Basically, all you want to compute component-wise sums between your input list and a shifted version of it:
a b c d e
a b c d e
---------------------------
a a+b b+c c+d d+e e
Fill each empty slot with a 0 (0:x and x++[0]), and you just need zipWith
> (\x -> zipWith (+) (0:x) (x++[0])) [1,2,3]
[1,3,5,3]

List difference function

Does any existing programming language, particularly in the Lisp or ML families, have a library function to calculate list difference in the sense of 'first until start of second' - I'm not sure offhand what it should be called exactly - for example, considering strings as lists of characters, if the inputs are:
abcdef
def
Then the output would be
abc
Code in Common Lisp:
CL-USER 1 > (defun fusos (s1 s2)
(let ((pos (search s2 s1)))
(when pos (subseq s1 0 pos))))
FUSOS
CL-USER 2 > (fusos '(a b c d e f) '(d e f))
(A B C)
There's already an accepted answer, but Common Lisp's LDIFF (short for "list difference") is still worth mentioning. It is based on the structure of lists (the cons cells the list is made of) rather than the elements of the list, so the list being "subtracted" has to be the same cons cell as some tail of the list. It's a bit more specific, but it certainly computes a list difference.
CL-USER> (let* ((abcdef '(a b c d e f))
(def (cdddr abcdef)))
(ldiff abcdef def))
(A B C)
Since takeWhile was mentioned in a comment and Haskell has this function, here is how you could achieve the desired result in Haskell:
takeWhile (flip notElem ys) xs
where your example would be
takeWhile (flip notElem "def") "abcdef"
That is, you take elements from the list xs as long as they are not contained in the list ys. As soon as you find an element that is contained in ys (or hit the end of xs) you stop.
In Standard ML it would be:
fun take_while p [] = []
| take_while p (x::xs) =
if p x then x :: take_while p xs
else []
EDIT: Above, I assumed that the specification was that we stop in the first list, as soon as we find an (arbitrary) element of the second list. Hence the use of takeWhile. However, from the OP it is not clear to me what the actual specification is. If it is remove an existing suffix (the second list) from the input (the first list), then the solution is of course different. In Haskell, without thinking about efficiency, we could do:
removeSuffix [] ys = []
removeSuffix xs#(z:zs) ys
| xs == ys = []
| otherwise = z : removeSuffix zs ys

how to filter the minimums haskell

now I already have a function that takes the minimum of the list of tuples' first element, for example;
mymin [(3,4),(3,2),(4,3)] = 3
By using this function, I'd like to take all the tuples which has 3 as its first element. I tried to filter the ones that has 3 on its first element but;
filter (\a -> mymin (x:xs) == fst x) (x:xs)
which gives
[(3,4),(3,2),(4,3)]
again because everytime it cuts the list, it finds mymin again, but I just want to take the
[(3,4),(3,2)]
part, what track should I follow, I stuck. Thanks for any help.
Why not use let or where to precompute the minimum value prior to filtering based on it?
yourFilter list =
let m = yourMin list
in filter (\(a, _) -> a == m) list
Alternatively, with a point-free style lambda:
yourFilter list =
let m = yourMin list
in filter ((== m) . fst) list
You only have to replace x with a in
filter (\a -> mymin (x:xs) == fst x) (x:xs)
(fst a instead of fst x)