In SML, it's common and easy to define a function using both currying and pattern matching. Here's a simple example:
fun zip [] _ = []
| zip _ [] = []
| zip (x::xs) (y::ys) = (x,y)::(zip xs ys)
Ignoring library functions, what's the best way to port this to OCaml? As far as I can tell, there is no easy way to declare a function using both currying and pattern matching.
I would say it's best to just use a match expression.
let rec zip xs ys =
match xs, ys with
| [], _
| _, [] -> []
| x :: xs, y :: ys -> (x, y) :: zip xs ys
If you're set on not using match, it's a bit convoluted, but you can do this.
let rec zip = function
| [] -> (fun _ -> [])
| x :: xs ->
function
| [] -> []
| y :: ys -> (x, y) :: zip xs ys
Related
I need to create a function that will take a list of integers and split them into 2 lists, one with odd numbered and the other even
split :: [a] -> ([a], [a])
split = undefined
Above is the function baseline, below is current attempt I have
split :: [a] -> ([a], [a])
split [] = ([],[])
split (x:xs) | x mod 2 == 0 = ([],[x:split xs])
| x mod 2 /= 0 = ([x:split xs],[])
| otherwise = ([],[])
explicit recursion:
split :: Integral a => [a] -> ([a], [a])
split [] = ([], [])
split (x:xs)
| even x = (x:ys, zs)
| otherwise = (ys, x:zs)
where
(ys, zs) = split xs
implicit recursion:
splitf :: Integral a => [a] -> ([a], [a])
splitf xs = foldr (\x (ys, zs) -> if even x then (x:ys, zs) else (ys, x:zs))
([], []) xs
which you can eta reduce to point-free style:
splitf2 :: Integral a => [a] -> ([a], [a])
splitf2 = foldr (\x (ys, zs) -> if even x then (x:ys, zs) else (ys, x:zs))
([], [])
I think the code is self explanatory. If there is anything I need to further explain, please let me know.
I am attempting to implement the zipWith function via the zip and map functions, but I am getting an error that reads: "error: parse error on input '::' My code is below and and I am unsure of what I have done wrong
zipWith` :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith` f x y = zip x $ map f y
You have to use ' symbol and not ` ; then, to combine the function you need to use uncurry:
zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith' f xs ys = map (uncurry f) (zip xs ys)
why is that, well the type of zip is:
zip :: [a] -> [b] -> [(a, b)]
but the function f is f :: (a -> b -> c), so, with the help of uncurry,
uncurry :: (a -> b -> c) -> (a, b) -> c
you can map the function f into the [(a, b)], transforming it into [c].
As Damian points out, zipWith` doesn't work with the trailing backtick -- the backtick has a special meaning in Haskell. Rename it to zipWith'.
zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
Then of course you have to actually write the solution. With explicit recursion you've got
zipWith' _ _ [] = []
zipWith' _ [] _ = []
zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys
but using map and zip you could apply it like this:
zipWith' f xs ys = map (\(x,y) -> f x y) . zip xs $ ys
or more easily-read:
zipWith' f xs ys = map (\(x,y) -> f x y) zipped
where zipped = zip xs ys
I am trying to make a list of pairs in ocaml, but the problem is when the lists' length are different i don't hot to make pairs (a,b) when one of the elements doesn't exist.
Most likely you'll have to create a type to encapsulate the situations of the differing length,
type ('a,'b) combined = Both of 'a * 'b | Left of 'a | Right of 'b
let rec zipwith xs ys = match xs,ys with
| x::xs,y::ys -> Both (x,y) :: (zipwith xs ys)
| x::xs, [] -> Left x :: (zipwith xs ys)
| [], y::ys -> Right y :: (zipwith xs ys)
| [], [] -> []
# zipwith [1;2;3] [1;2];;
- : (int, int) combined list = [Both (1, 1); Both (2, 2); Left 3]
You'll have to make optimizations regarding tail-calls for this to work on long lists, but this is a start of how to approach the problem.
I am trying to enumerate all the possible merges of two lists.
In example inserting "bb" into "aaa" would look like
["bbaaa", "babaa", "baaba", "baaab", "abbaa", "ababa", "abaab", "aabba", "aabab", "aaabb"]
What I currently did is this
import Data.List
insert'' :: Char -> String -> [(String, String)] -> String
insert'' _ _ ([]) = []
insert'' h b ((x, y):xs) =
(x ++ [h] ++ (insert' (b, y))) ++ (insert'' h b xs)
insert' :: (String, String) -> String
insert' ([], ys) = ys
insert' (xs, ys) =
insert'' h b lists
where
h = head xs
b = tail xs
lists = zip (tails ys) (inits ys)
This returns for ("aaa", "bb")
"bbaaababaaabaababbaababaababbabababb"
a concatenated string, I tried making it a list of strings, but I just cannot wrap my head around this function. I always seems to get infinite type construction.
How could I rewrite the function, so it would return a list of strings?
An other implementation idea as in Daniel Wagners first post is to choose in each step a element from one of the lists and prepending it to the results generated by the function called with only the remaining parts of the list:
interleave :: [a] -> [a] -> [[a]]
interleave xs [] = [xs]
interleave [] ys = [ys]
interleave xs#(x : xs') ys#(y : ys') =
map (x :) (interleave xs' ys) ++ map (y :) (interleave xs ys')
For your intial example this produces:
ghci> interleave "bb" "aaa"
["bbaaa","babaa","baaba","baaab","abbaa","ababa","abaab","aabba","aabab","aaabb"]
Here is one implementation idea: for each element in the first list, we will choose (nondeterministically) a position in the second list to insert it, then recurse. For this to work, we first need a way to nondeterministically choose a position; thus:
choose :: [a] -> [([a], [a])]
choose = go [] where
go before xs = (before, xs) : case xs of
[] -> []
x:xs -> go (x:before) xs
For example:
> choose "abcd"
[("","abcd"),("a","bcd"),("ba","cd"),("cba","d"),("dcba","")]
Now we can use this tool to do the insertion:
insert :: [a] -> [a] -> [[a]]
insert [] ys = [ys]
insert (x:xs) ys = do
(before, after) <- choose ys
rest <- insert xs (reverse after)
return (before ++ [x] ++ rest)
In ghci:
> insert "ab" "cde"
["abcde","aebcd","adebc","acdeb","cabde","caebd","cadeb","dcabe","dcaeb","edcab"]
In this answer, I will give the minimal change needed to fix the code you already have (without completely rewriting your code). The first change needed is to update your type signatures to return lists of strings:
insert'' :: Char -> String -> [(String, String)] -> [String]
insert' :: (String, String) -> [String]
Now your compiler will complain that the first clause of insert' is returning a String instead of a [String], which is easily fixed:
insert' ([], ys) = [ys]
...and that the second clause of insert'' is trying to append a String to a [String] when running [h] ++ insert' (b, y). This one takes some thinking to figure out what you really meant; but my conclusion is that instead of x ++ [h] ++ insert' (b, y), you really want to run \t -> x ++ [h] ++ t for each element in insert' (b, y). Thus:
insert'' h b ((x, y):xs) =
(map (\t -> x ++ [h] ++ t) (insert' (b, y))) ++ (insert'' h b xs)
The complete final code is:
import Data.List
insert'' :: Char -> String -> [(String, String)] -> [String]
insert'' _ _ ([]) = []
insert'' h b ((x, y):xs) =
(map (\t -> x ++ [h] ++ t) (insert' (b, y))) ++ (insert'' h b xs)
insert' :: (String, String) -> [String]
insert' ([], ys) = [ys]
insert' (xs, ys) =
insert'' h b lists
where
h = head xs
b = tail xs
lists = zip (tails ys) (inits ys)
Now ghci will happily produce good answers:
> insert' ("aaa", "bb")
["bbaaa","babaa","baaba","baaab","abbaa","ababa","abaab","aabba","aabab","aaabb"]
this is my assignment the string concatenation function is below and below that is the function that i need help with.
type Language = [String]
strcat :: String -> String -> String
strcat [] y = y
strcat (x:xs) y = x:(strcat xs y)
concat_lang :: Language -> Language -> Language
concat_lang [] y = y
concat_lang x [] = x
concat_lang (x:xs) (y:ys) = (strcat x y):(concat_lang (x:xs) ys)
This is my input to concat_lang : concat_lang ["a","b","c"] ["d","e","f"]
i want the output to be [ad,ae,af,bd,be,bf,cd,ce,cf]
Pls help!!
List comprehension makes life much easier
lang xs ys = [x:y:[] | x <- xs , y <- ys]
lang is polymorphic, if this is undesirable, simply add a type signature.
combinations :: [a] -> [b] -> [(a,b)]
combinations xs ys = concatMap (flip zip ys . repeat) xs
type Language = [String]
concat_lang :: Language -> Language -> Language
concat_lang xs ys = map f $ combinations xs ys
where
f (x,y) = x ++ y
use
concat_lang ["a","b","c"] ["d","e","f"]
to get
["ad","ae","af","bd","be","bf","cd","ce","cf"]
concat_lang xs ys = [ x++y | x <- xs, y <- ys]
Classic example where Applicative Functors can come in handy:
>> import Control.Applicative
>> (++) <$> ["a", "b", "c"] <*> ["d", "e", "f"]
["ad","ae","af","bd","be","bf","cd","ce","cf"]
>>
You should definitely check out Aplicative Functors for this ...