Folding in Haskell, using more than one function

Saser

I have a list of integers based on letters. For example:

let charlist = map (ord) "ABCDEF"

charlist would then look like the following:

[65,66,67,68,69,70]

I also have a list of three functions: (+), (-) and (*). The list in this example looks like this

let funclist = [(+), (-), (*)]

I want to apply the functions in order between the elements in charlist (if there are more "spaces" in charlist than there are elements in funclist, you start over from the beginning of funclist) and calculate the final value from left to right, like the following:

s = ((((((65) + 66) - 67) * 68) + 69) - 70)

I was thinking about using foldl, but foldl seems to only work with one function. Is there any other way to do this? I would like to summarize this entire process in one function, if possible, though it's not a requirement.

Clinton

Whilst foldl may only apply one function, you can have different functions in your data. The trick it to attach the appropriate +, - or * function to your data. You're off to the right start:

funclist = [(+), (-), (*)]

But lets now make a infinite version of the above list, like [(+), (-), (*), (+), (-), (*)...]

infinite_funclist = cycle funclist

Lets assign the numbers we're going to fold over here. first is in this case 65, rest is [66..70]

(first:rest) = [65..70]

Now we zip together rest and infinite_funclist to get [(66,(+)), (67,(-)), (68,(*)), (69,(+)), (70,(-))]. We start with first, and for every new element, we apply the operation that's in the second part of the current tuple to the current value and the first part like so:

result = foldl' (\acc (v, f) -> acc `f` v) first (zip rest infinite_funclist)

If we want to print the result we can do so like so:

main = print result

(Link to code here)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

More than one function to IF statement

From Dev

Haskell filter more than one char

From Dev

Why not a more general function than mappend in Haskell?

From Dev

insert a function into more than one blueprints in Flask

From Dev

Replacing more than one elements with replace function

From Dev

CMake finds more than one main function

From Dev

Return more than one value from a function

From Dev

Unpacking more than one list as argument for a function

From Dev

How to disable more than one scroll function?

From Dev

rollapply with function that returns more than one value

From Dev

Integrating more than one function error in python

From Dev

execute function not more than one time

From Dev

more than one function from jquery ui

From Dev

insert a function into more than one blueprints in Flask

From Dev

Replacing more than one elements with replace function

From Dev

How to call more than one function in python

From Dev

pass more than one function to a callback

From Dev

Android permission check in more than one function

From Dev

Returning more than one variable in the same function

From Dev

Mysql function, return more than one row

From Dev

More than one functions after if then .., don't work in Haskell

From Dev

Filter more than one element, deleting words in a list in Haskell

From Dev

Using WCSession with more than one ViewController

From Java

For loop using more than one list in Python

From Dev

Is it worth using more than one web tracker?

From Dev

Creating more than one process using fork

From Dev

Using a variable in more than one funciton?

From Dev

Delete more than one file using unlink

From Dev

For loop using more than one list in Python

Related Related

HotTag

Archive