Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell, sequential program flow?

Tags:

haskell

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!

like image 306
user997112 Avatar asked Dec 21 '22 06:12

user997112


2 Answers

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
like image 77
Fred Foo Avatar answered Jan 05 '23 00:01

Fred Foo


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
like image 27
Alexey Romanov Avatar answered Jan 04 '23 22:01

Alexey Romanov