I have the following code:
case (HashMap.lookup "key123" jsnObj) of
Just c ->
case c of
Array d -> print d
and the result:
fromList [String "123.00000",String "0.456789"]
I can't figure out how I can retrieve the second value from it. Your suggestions?
You need to have Data.Vector imported and then you can simply use the indexing function ! on the Vector d:
case HashMap.lookup "key123" jsnObj of
Just c -> case c of
Array d -> print $ d ! 1
Related
Condition: Function containing character c and string xs, where all c's are replaced by '*'.
`
zensiert :: [Char] -> String
zensiert c (x:xs)
| x == c = x == '*'
| otherwise = zensiert c (x:xs)
`
The map function and list generators are prohibited. Example: zensiert 'l' ''Rolls a ball around the corner and falls down.'' returns:''Ros a ba around the corner and fas down.''
Because of the example is was thinking that is at the beginning a list in a char that leads to a string, but it didn ´ t work. Because of that I would be very conceivable, if someone would look over there times
Now I tried so:
zensiert :: [Char] -> String
zensiert (x:xs) = if x == 'c' then ('*' : zensiert xs) else (x : zensiert xs)
But there is the problem, if i give zensiert 'l' "Hello", the terminal says: Couldn't match expected type ‘[Char] -> t’ with actual type ‘[Char]’
First, the type signature should be
zensiert :: Char -> String -> String
You'll take a character and a string as arguments, and return a new string.
== is not an assignment operator, even if Haskell did have assignments. Your second definition would be fine (for replacing hard-coded 'c's, anyway), except you don't define it for the empty string the recursive call will eventually receive, and you incorrectly call it with a Char argument it won't use.
The correct definition takes a Char argument and uses that instead of the Char literal 'c'.
zensiert :: Char -> String -> String
zensiert _ [] = []
zensiert c (x:xs) = if x == c then ('*' : zensiert xs) else (x : zensiert xs)
A simpler approach would be to use map to apply a function that replaces 'c' with '*' to a list, whether it is empty or not.
zensiert c xs = map (\x -> if x == c then '*' else x) xs
or
zensiert c xs = map f xs
where f 'c' = '*'
f x = x
Whenever you are defining recursive functions, you need at least one base case that exists the recursion, and at least one case that calls the function recursively in a way that converges on the base case.
If you're dealing with strings, the base case is often an empty string, and we converge the making the input string closer to an empty string.
Replacing any character with '*' in an empty string gives us just an empty string, so the base case is simple. For the update case, we just have to decide what to append to recursively calling the function on the "tail" of the string.
ghci> :{
ghci| subChar _ "" = ""
ghci| subChar c (x:xs)
ghci| | c == x = '*' : subChar c xs
ghci| | otherwise = x : subChar c xs
ghci| :}
ghci> subChar 'l' "world"
"wor*d"
ghci>
My function will replace will any number in the string to 3 and any character to 4. For example "foo123" will be replaced to "444333". My question is how do I convert the "444333" into list in order to use the sum function. "444333" -> [4,4,4,3,3,3] -> sum [4,4,4,3,3,3] = 21
This my code
replaceString [] = []
replaceString (x:xs) =
if x `elem` ['0'..'9']
then '3' :replaceString xs
else if x `elem` ['a'..'z']
then '4' : replaceString xs
else x : replaceString xs
Your replaceString already returns a List of characters but I guess you want to obtain a list of numbers, the answer is just to replace '3' and '4' with 3 and 4 :), so it will become like this :
replaceString [] = []
replaceString (x:xs) = (if x `elem` ['0'..'9']
then 3
else 4) : replaceString xs
Notice that we don't need to repeat : replaceString xs :) .
Alternatively if you want to convert a list of digit characters into a list of numbers you could get character's "ordinal" and subtract 48 from it, in haskell the character's ordinal can be obtained by fromEnum char, with replaceString putting '3' and '4'(instead of numbers), we can define a function like this :
convertToDigits numstr = map ((48 -) . fromEnum) numstr
By the way your original function doesn't convert any other character into 4 but only alphabetic characets, so for foo21! the result would be 44433! and you wouldn't want to sum that, if you want to filter digits I suggest you filter the string from non-alphanumeric characters before even calling replaceString.
Edit :
As Thomas pointed out, you can replace (48 -) . fromEnum with digitToInt (needs to be imported from Data.Char).
This could also be done by foldl as follows;
import Data.Char
getsum :: String -> Int
getsum = foldl helper 0
where helper n c | isLetter c = n + 3
| isNumber c = n + 4
| otherwise = n
If you use map and read it should works great
funct l = map(\x ->read [x]::Int) l
sum funct("444333") = 21
# let rec map1 f l = match l with
[]->[]
|h::t -> f h::map1 f t;;
val map1 : ('a -> 'b) -> 'a list -> 'b list = <fun>
I am new to OCaml , I have two questions:
In the third line, why there is a f before h :: map1 f t? f should be a argument in the map1 function. Why the book's example puts it seperately?
In the first example ('a -> 'b) -> 'a list -> 'b list = <fun> why there is a b list?
the book explains that b list is the result of the function f and a list is the argument of the function f. However, why there is no a, b list in the following example? It also has a function f and it also puts f separately in the third line.
# let rec apply f n x=
if n = 0 then x
else f ( apply f (n-1) x);;
val apply : ('a -> 'a) -> int -> 'a -> 'a = <fun>
|h::t -> f h::map1 f t
the precedence rules of Ocaml's syntax means that the above match clause is parsed as
|h::t -> (f h)::(map1 f t)
and of course f h is the application of function f to argument h
In words, when the list l is matching the pattern h::t (so l is a proper list of head h and tail t), a pair is made :: (or built, or constructed) of head f h and tail map1 f t
A typical use would be first to have a function from integers to strings:
let nextasstr n = Printf.sprintf "(%d)" (n+1);;
So nextasstr 2 is the string "(3)" without the quotes. Of course [2;3] is a list of integers, i.e. a int list
Then map1 nextasstr [2;3] is evaluated to [ "(3)"; "(4)" ], a list of strings, i.e. a string list; you see that the second argument has a type different of the result. (this should give an insight on the 'a list vs 'b list difference and the typing map1 : ('a -> 'b) -> 'a list -> 'b list) with the first argument being a arbitrary function of type 'a -> 'b
You should see the Ocaml MOOC, follow the Ocaml tutorial, read its documentations. This may take weeks of work.
In OCaml, I have a list of strings that contains names of towns (Something like "1-New York; 2-London; 3-Paris"). I need to ask the user to type a number (if they want London they have to type 2).
I want to raise an exception message saying that the town is not valid, if the person types for example "4", in the example.
I tried this, but it doesn't work :
let chosenTown = match int_of_string (input_line stdin) with
| x > (length listOfTowns) -> raise (Err "Not a valid town")
What's the good way to code "if the chosen number is bigger than the length of the list then raise the error" ??
Pattern can't contain arbitrary expressions. It can be a constant, a constructor name, record field inside curly braces, list, array, etc.
But patterns can be guarded, e.g.
match int_of_string (input_line stding) with
| x when x >= length listOfTowns ->
invalid_arg "the number is too large"
| x -> List.nth listOfTowns x
To complete the answer, patter matching relies on unification and does not expect assertion (it is not the equivalent of a switch in C or so).
The idea is that you provide different "shapes" (patterns) that your term (the thing you match on) could have.
For a list for instance:
match l with
| e :: e' :: r -> (*...*)
| e :: r -> (*...*)
| [] -> (*...*)
It also had a binding effect, if you pass on, say, [1] (a very small list indeed), it won't match e :: e' :: r, but will match e :: r and then e = 1 and r = [].
As ivg said, you can add conditions, as booleans this time, thanks to the keyword when.
However, when manipulating lists like this, I would go for a recursive function:
let rec find_town n l =
match l with
| t :: _ when n = 1 -> t
| _ :: r -> find_town (n-1) r
| [] -> raise (Err "Not a valid town")
This is basically writing again List.nth but changing the exception that it raises.
Say we have a list of coordinates like:
(1,2)
(0,3)
(4,1)
(0,3)
(-2,3)
(6,5)
And we wanted to result in the following list, which is defined as the summation of each consecutive coordinates. (Sorry bad definition) like so:
(1,5)
(4,4)
(4,4)
(-2,6)
(4,8)
So there exists a set A = (a,b,c,...,n) where a,b,c,...,n are coordinates in R^2.
There exists a function f such that f(A) = B = (a+b,b+c,c+d,...,n-1+n).
~
How would you write something like that in a functional language like Haskell? A program that applies f to a given A to give B.
You can use zip to zip the list with its tail, you get pairs of pairs like [((1,2), (0,3)), ((0,3),(4,1)), ...]. Then you can use map to replace each pair of pairs with its sum. Or you can use zipWith which is basically zip + map in one function, except the function given to zipWith has type a -> b -> c, not (a,b) -> c:
summedCoords = zipWith (\ (a,b) (c,d) -> (a+c, b+d)) coords (tail coords)
You can write a generic function like this
g:: (a -> a -> b) -> [a] -> [b]
g f (x1:x2:xs) = (f x1 x2):(g (x2:xs))
g _ (x1:[]) = []
and pass it your add function
f = g f' where
f' (a,b) (a',b') = (a+a', b+b')