dan
dan

Reputation: 21

How to sort a list of integer without built-in function and let-in expression in Haskell?

I have this code that output the sorted list of integer. If i want to change my code by removing the let-in expression but still want the same output, what should I do?

sort :: [Int] -> [Int]
sort [] = []
sort li =
    let 
        small = sort [a | a <- tail li, a <= head li]
        big = sort [a | a <- tail li, a > head li]
    in small ++ [head li] ++ big

Upvotes: 1

Views: 219

Answers (1)

Will Ness
Will Ness

Reputation: 71065

In Haskell you can just substitute equals for equals in an expression, and the expression's value won't change.

In particular, if you have a = b in some expression ...a..., you can just put b wherever a was in that expression, getting ...b..., and the expression's value won't change:

    let  small = sort [a | a <- tail li, a <= head li]
         big   = sort [a | a <- tail li, a >  head li]
    in 
       small ++ [head li] ++ big

becomes

       ..... ++ [head li] ++ .....

Upvotes: 1

Related Questions