Factor and Haskell comparison

Submitted by metaperl on Thu, 02/01/2007 - 11:23am.

Haskell

let sq = (\x -> x * x) in map sq $ map sq [1 .. 3]

Factor

{ 1 2 3 } [ sq ] map [ sq ] map .

Haskell 2

let sq = (\x -> x*x) in map (sq . sq) [1..3]

Factor 2

{ 1 2 3 } [ sq sq ] map .

Factor 3

3 [ 1+ sq sq ] map .

CONCLUSION

One interesting thing is that no new language mechanisms were necessary in Factor. In Haskell, one had to know $ and the composition operator . as well a varioius precedence rules to make clean concise code.

Submitted by dons on Fri, 02/02/2007 - 2:15am.
Is 'sq' builtin? Then a more reasonable comparision is:
    map (sq . sq) [1..10]
        where sq x = x * x
Submitted by Cale Gibbard on Mon, 02/19/2007 - 6:42am.

Huh? (.) and ($) are part of the language spec of Haskell. They're just ordinary functions in the Prelude.

I'd probably write it as:

map (^4) [1..3]

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.