user997112
user997112

Reputation: 30605

Haskell, sequential program flow?

Apologies, as this may seem a very weird question. All of my experience in haskell has been writing functions which usually recursively progress (and some data element decreases with each iteration). However, I have a set of functions which each do some processing on a piece of data and I wanted my calling method to contain each stage, for example

(Pseudo code)

myFunc1 :: Something1 -> Something2

execute myFunc1 Something1
.
execute myFunc2
.
execute myFunc3
.
execute myFunc4
.

return Something2

But I am unsure if this is even possible? Do I simply have to have something silly like:

myFunc4(myFunc3(myFunc2(MyFunc1(Something1)))) ?

EDIT: The above line cannot be right, surely!

Upvotes: 4

Views: 1684

Answers (2)

Alexey Romanov
Alexey Romanov

Reputation: 170713

If you want to keep left-to-right reading order, you can define

(.>) = flip (.) -- are those in standard library somewhere?
($>) = flip ($)

myComplex = Something1 $> myFunc1 .> myFunc2 .> myFunc3 .> myFunc4

Upvotes: 3

Fred Foo
Fred Foo

Reputation: 363507

Use the function call operator $:

myFunc4 $ myFunc3 $ myFunc2 $ myFunc1 $ Something1

Or function composition:

myFunc4 . myFunc3 . myFunc2 . myFunc1 $ Something1

Or let:

let x = myFunc1 Something1 in
let y = myFunc2 x in
let z = myFunc3 y in
myFunc4 z

Upvotes: 8

Related Questions