Matching letters in 2 strings with haskell [closed] - string

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I just started with haskell and im wondering if there is a easy way to match the letters between 2 string and output them.
like:
iced and liked will return i,e,d
Thank you!

Use Data.Set.intersection:
import qualified Data.Set as S
sharedLetters str1 str2 = S.toList $ S.intersection (S.fromList str1) (S.fromList str2)
EDIT: As #jozefg pointed out, there is a function in Data.List which does the same for lists:
> import Data.List (intersect)
> intersect "liked" "iced"
"ied"

Related

How to read the arithmetic operators as input in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How to take operator as input and how to perform operations using tat operator
I have tried using functions
If we provide the input 2,5,+. the program should give output as 7.
I'm not going to give you all of the code, but here's a good starting point.
In Python, check out the operator module:
>>> import operator
>>> sum = operator.add(2, 5)
>>> sum
7
>>> diff = operator.sub(2, 5)
>>> diff
-3

How to pass an argument to calculator? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am trying to speed up my calculator. How would i got about that?
You can't use fib as the name of the function that gets the nth Fibonacci number and the name of the generating list.
This should work:
fibList = 0:1:zipWith (+) fibList (tail fibList)
fib n = fibList !! n

groovy - collate maps in list of maps [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
What is the simplest way to combine several maps in list elements into one map. For instance: list1 -> list2
def list1 = [[a:'apple'],[b:'orange'],[c:'pear'],[a:'watermelon'],[b:'banana'],[c:'grape'],[a:'lychee'],[b:'guava'],[c:'starfruit']]
def list2 = [[a:'apple', b:'orange', c:'pear'],[a:'watermelon', b:'banana', c:'grape'],[a:'lychee', b:'guava', c:'starfruit']]
Try this:
list1.groupBy{it.values()[0].intdiv(10)}.collect{it.value.collectEntries()}

Remove duplicates from list of lists in Haskell [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Given the list of lists duplicates = [[1,1,1],[2],[1,1,1]]
How to remove the duplicates so that the result is [[2]]?
Like this:
Data.MultiSet> [a | (a, 1) <- toOccurList (fromList [[1,1,1],[2],[1,1,1]])]
[[2]]

insert letter before vowel in word [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
how to insert letter before vowel in each word in python. like Gibberish game. We have to ask user to to input some letter and input their word to insert some letter that they input at first to before vowels in each word they input at second.
If this is for homework, you really should try to figure this out on your own rather than going to StackOverflow, but you can use regex to do it in one line:
import re
re.sub(r"([aeiou])",r"X\g<1>", "hello")
Or if you want a function version of it:
import re
def add_letter_to_vowels(word, letter):
return re.sub(r"([aeiou])",r"%s\g<1>"%letter, word)
Then it runs as follows:
>>> add_letter_to_vowels("hello", "X")
'hXellXo'
Regular expressions are very powerful, read more about them here and the API reference is here

Resources