Ok, I can't figure this one out even though I have an idea what it's doing...
let t = ["APE", "MONKEY", "DONKEY"]
Now consider three cases:
map (length.group) t
(map length.group) t
map (map length.group) t
This returns these three answers:
[3,6,6]
[1,1,1]
[[1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1]]
Now, can someone explain to me in details what's going on. A crucial part of this question is that I assume that map needs a list to work on and I don't see two maps being passed in the third case for example.
map (length.group) t
This composes the functions length and group. The result is a function that takes a list (string) and returns the number of "groups" in that list (where a group is a sequence of the same character repeating 1 or more times, so "abc" contains 3 groups and so does "aabbcc").
This function is then applied to each string in t using map.
(map length.group) t
Here the function map length (which takes the length of each sublist in a list of lists) is composed with the function group and the composed function is applied to t. In other words it's the same as map length (group t).
map (map length.group) t
Here the function map length . group is applied to each string in t, i.e. map length (group str) is calculated for each string str in t.
Try removing the "length." from all your cases, and see if that helps answer your question. It'll simplify the problem and the answer might show you a little better what's going on.
Or, factoring the third one, it becomes
map (map length.group) ["APE", "MONKEY", "DONKEY"]
--make parse order explicit
map ((map length) . group) ["APE", "MONKEY", "DONKEY"]
--do mapping
[((map length) . group) "APE", ((map length) . group) "MONKEY", ((map length) . group) "DONKEY"]
--use (f.g) x == f (g x)
[(map length) (group "APE"), ...]
[(map length) ["A", "P", "E"], ...]
[[1, 1, 1], ...]
Also try using some animals like "EEL" or "BEE" or "LLAMA" to see anything other than 1's in the final result.
Related
I want to use the standard Prelude in Haskell (no recursion, no list comprehension) to filter a list of tuples and then output a list of integers. It should check a list of 3-tuples that contain integers, e.g.,
[(1,2,3), (2,3,7), (4,5,20)]
and see if the sum of element #1 and #2 equals element #3. If it does, put element #3 in the list and then output the list. This is the output that I am looking for:
>sumOfElements [(1,2,3), (2,3,7), (4,5,9)]
[3,9]
This is what I have tried:
sumsOfElements :: [(Int, Int, Int)] -> [Int]
sumsOfElements list = filter (\(a,b,c) -> a+b==c) list
This kind of works but it outputs a list of tuples that meets the conditional.
>sumOfElements [(1,2,3), (2,3,7), (4,5,9)]
[(1,2,3), (4,5,9)]
I'm not sure how to take c from the tuple and append that element to a new list for the output.
Think about this as a pipeline: first find the right elements, then transform them into the shape you need:
sumOfElements = map (\(_,_,c) -> c) . filter (\(a,b,c) -> a+b==c)
And trust that laziness and optimizations (specifically, foldr/build fusion) will make it performant.
You can do this more succinctly with a list comprehension:
sumOfElements list = [c | (a,b,c) <- list, a+b==c]
I'm tryung to create this predicate in prolog:
The predicate json_get/3 can be defined as: json_get(JSON_obj, Fields, Result). which is true when Result is recoverable by following
the chain of fields in Fields (a list) starting from JSON_obj. A field
represented by N (with N a major number o equal to 0) corresponds to
an index of a JSON array.
Please help me to understand to follow the chain of fields.
Thanks
edit1:
Of course, so json object looks like this '{"name" : "Aretha", "surname" : "Franklin"}'.
if i call json_parse predicate to this object prolog show me this
json_obj([(”name”, ”Aretha”), (”surname”, ”Franklin”)]), we call this obj O.
with json_get i need to extract from O the name in this way, json_get(O, ["name"], R)
edit2:
with someone's help this is the predicate now:
json_get(json_obj(JSON_obj), Field, Result) :-
memberchk((Field,Result), JSON_obj).
json_get(JSON_obj, Fields, Result) :-
maplist(json_get(JSON_obj), Fields, Result).
so now the problem is nested list.
For example with this input
json_parse('{"nome" : "Zaphod",
"heads" : ["Head1", "Head2"]}', Z),
json_get(Z, ["heads", 1], R).
the output will should be R = "Head2" but the predicate doesn't extract the field and fail.
edit3:
this is the output of json_parse
json_obj([("nome", "Zaphod"), ("heads", json_array(["Head1", "Head2"]))]).
How about this
json_get(json_obj(Obj),[F|Fs],Res) :-
member((F,R),Obj),
json_get(R,Fs,Res).
json_get(json_array(Is),[N|Fs],Res) :-
nth1(N,Is,R),
json_get(R,Fs,Res).
json_get(Res,[],Res).
This produces Head1 not Head2 in your 2nd example. Please explain how that is supposed to work, if you did not just make a typo. (If it is zero-based you can just change nth1/3 to nth0/3.)
I have difficulty processing a list a Scala:
Currently I have a list of like this
(List(JString(2437), JString(2445), JString(2428), JString(321)), CompactBuffer((4,1)))
and I would like after processing, the result will look like below:
( (2437, CompactBuffer((4,1))), (2445, CompactBuffer((4,1))), (2428, CompactBuffer((4,1))), (321, CompactBuffer((4,1))) )
Can any body help me with this issue?
Thank you very much.
Try this:
val pair = (List(JString(2437), JString(2445), JString(2428), JString(321)),
CompactBuffer((4,1)))
val result = pair._1.map((_, pair._2))
First, pair._1 gets the list from the tuple. Then, map performs the function on each element of the list. The function (_, pair._2) puts the given element from the list in a new tuple together with the second part of the pair tuple.
I have created a LinkedHashMap, which produces a list of statuses in groovy.
These are the results of a mysql query. I want to filter the results of my map to use key entries (statusName) in the map which start with the letter "O". However I am struggling to find how to do this with a map.
My code is as follows:
db.eachRow(reportQuery, [date]) {
cache.put(it.statusName, it.statusTickets)
}
cache.each{statusName, statusTickets ->
reportMetricsWithValues[statusName] = statusTickets
table.addData([metric:statusName, value: statusTickets])
}
This is the part of my code where I need to add this filter. The code is adding the key value metrics to a database table.
To filter, you would use findAll. Pass it the map element by element and check, if the key of the element starts with the letter O; Something along the lines of:
groovy:000> [zomg: 1, omg: 2].findAll{ it.key.startsWith('o' ) }
===> [omg:2]
If you also need the "others", then groupBy (same syntax like above) could prove useful.
I am doing problem 68 at project euler and came up with the following code in Haskell to return the list of numbers which fit the (given) solution:
lists = [n|n<- permutations [1..6] , ring n ]
ring [a,b,c,d,e,f] = (length $ nub $ map sum [[d,c,b],[f,b,a],[e,a,c]]) == 1
This only returns a list of lists of 6 numbers each which fit the solution. What I don't know how to do, is make it return the actual solution, the lists that fit the form:
[d,c,b],[f,b,a],[e,a,c]
How can I make lists return a list of this format?
(PS: I will add in the appropriate functions to return what the site actually wants later)
It's simply
lists = [ [[d,c,b],[f,b,a],[e,a,c]] | n#[a,b,c,d,e,f] <- permutations [1..6], ring n ]
Or in order to generate the strings:
[ foldl (++) "" $ map show [d,c,b,f,b,a,e,a,c] | n#[a,b,c,d,e,f] <- permutations [1..6], ring n ]