Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
string = "this is an example string"
in haskell, how can i count how many times the character 'a' appears in the string above? Preferably in one line.
Think about it this way: you want to know the size of the list in which all letters that are not 'a' have been removed. To remove these, we can use filter. This function filters out any items in a list that do not satisfy a given predicate. Then getting the size of this list is trivial with length.
length $ filter (\x -> x=='a') "this is an example string"
Edit:
As per AndrewC's suggestion, you can also write this as:
length $ filter (=='a') "this is an example string"
This way is more readable and clearer.
Related
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 6 years ago.
Improve this question
I am new to clojure and not well versed with the available functions. So I wanted to know how to retrieve uppercase words from a sentence.
For Example:
Input: IllegalArgumentException contains? not SUPPORTED on type: java.lang.String clojure.lang.RT.contains
Output: SUPPORTED
One way would be to use regular expression like in any other language:
(re-find #"\b[A-Z]+\b" s)
;; => "SUPPORTED"
If there's more than one expected match, you can use re-seq:
(re-seq #"\b[A-Z]+\b" "THIS is a TEST")
;; => ("THIS" "TEST")
Since you mentioned you're not well-versed with regards to available functions, please check out ClojureDocs.
First we write a function that tests whether a string is upper-case or not.
(defn upper-case? [word]
(= word (clojure.string/upper-case word)))
Then we split any string into words and filter out all words that are not upper-case. In your example there was at least one instance of two consecutive spaces, so those had to be filtered out as well.
(defn upper-case-words [sentence]
(filterv #(and (upper-case? %) (not= "" %)) (clojure.string/split sentence #" ")))
(upper-case-words "Input: IllegalArgumentException contains? not SUPPORTED on type: java.lang.String clojure.lang.RT.contains")
["SUPPORTED"]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What i have is an array containing strings of length = 31. Total number of strings = 13700. I want to split all strings into substrings of length 5,22 and 4. Please help. Thank you.
With fixed-length strings and fixed-length substrings it's really easy. If they're not already, mash the strings around until they're in a 2-D char array, one string per row (cell2mat() will be your friend if they're currently in a cell array), and split:
s1 = s(:, 1:5);
s2 = s(:, 6:27);
s3 = s(:, 28:31);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let I have two string. Say, "learn" and "haskell". How can make them list like ["learn","haskell"]
makeListFromTwoThings :: a -> a -> [a]
makeListFromTwoThings x y = [x,y]
Edit: I am assuming you want a function that does this generically. If you want to do this in the Haskell shell then it seems like nothing is stopping you from typing ["learn","haskell"] directly. If you are trying to solve some more general problem directly in the shell then let us know the details of that problem.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
we've been given the task to make a Dictionary, as a List of tuples with (String, String).
The problem here is that I actually have no idea if I can rename String twice at the same time, since I want the tuple to look like this
(German, English)
Is it even possible to just make it look like this?
type German = String
type English = String
type Dictionary = [(German, English)]
Or would there be any conflicts?
It was kinda frustrating not finding a single entry about this, been almost 2 hours and not a single line of code stands right now :|
Yes, you can do that. You can't do the opposite
type German = String
type German = Int -- conflicts here!
Of course you can, if you couldn't, you wouldn't be able to compile type German = String in the first place, as it's already been named ;-)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Given a set of 10 symbols and a set of strings(at max 100) of length at max 20 each consisting of these symbols, find the maximum length string which can be made from these symbols that doesn't have any of the given strings as its sub-string. In case, if we can have infinite long string satisfying the property, print -1.
Besides brute force algorithm which will go exponential in time, I am not able to get any solution for this.
Any hint to approach this problem will be thankful.
Given a set of strings that need to be matched, my immediate reaction is to use http://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_string_matching_algorithm to create a matcher. This matcher is a finite state machine that accepts one character at a time and tells you which state you end up in next, given that character.
So I think you can reduce the problem to accepting a directed graph and a starting point and finding the longest route through that graph that does not go through the nodes that correspond to pattern matches - which I think we can simply delete from the graph. This is covered in http://en.wikipedia.org/wiki/Longest_path. Constructing this graph is also linear so the whole thing seems to be O(n)