Home > Net >  I am new in haskell, Is there a way I can pass a function inside a function. I am not talking about
I am new in haskell, Is there a way I can pass a function inside a function. I am not talking about

Time:11-12

--A function to convet smaller case characters into upper case
convert :: [Char] -> [Char]
convert = map toUpper

--A function to remove all the numbers from a string
noNumber :: [Char] -> [Char]
noNumber = filter isLetter

I want to add these checks before calling the below function

encode :: [Char] -> [Char] -> [Char]
encode [] ps = []
encode ks [] = []
encode (k:ks) (p:ps) = chr (65   mod (ord  k   ord p) 26) : encode (ks  [k]) ps

So that I can remove any numbers or lower case characters from the Text

CodePudding user response:

You can work with a helper function:

encode :: [Char] -> [Char] -> [Char]
encode xs ys = go (cycle (f xs)) (f ys)
    where go [] ps = []
          go ks [] = []
          go (k:ks) (p:ps) = chr (65   mod (ord  k   ord p) 26) : encode ks ps
          f = noNumber . convert

then go is just a zipWith:

encode :: [Char] -> [Char] -> [Char]
encode xs ys = zipWith g (cycle (f xs)) (f ys)
    where g k p = chr (65   mod (ord  k   ord p) 26)
          f = noNumber . convert
  • Related