tags:

views:

147

answers:

3

As my first haskell program I'm trying to do this - it's the hard way to get 1 to 10. I'm constructing an infinite list of integers, and sorting them, and taking the first 10. My intention was to convince myself that I could work with infinite lists without causing evaluation of them beyond what was strictly (ahem) required for the demanded result.

My code is..

module Main where

import Data.List

minima n xs = take n (sort xs)

main = do
    let x = [1..] 
    print (minima 10 x)

Compiling with ghc and running the resulting executable.. it sits there allocating until killed.

Any hints?

+10  A: 

The problem is that you're trying to sort the infinite list. The list can never be fully sorted until all elements in the list are known, so that's why it's hanging. Your program works fine with a finite list.

Also, as a minor aside, you can rewrite minima as

minima n = take n . sort

Because of partial application, minima n will return a function expecting a list.

Mark Rushakoff
Though of course that partially applied version is exactly equivalent to the original function and will still hang on an infinite list. (I know you know this — I'm just pointing it out for those playing along at home.)
Chuck
Ah yes - quite obvious in retrospect that I couldn't expect the implementation to not keeping looking for a smaller value. *blush*When I try your other suggestion, I get the following. Don't quite understand it yet but I'll keep digging, thanks!test.hs:6:21: Couldn't match expected type `[a]' against inferred type `[a1] -> [a1]' In the second argument of `($)', namely `sort' In the expression: take n $ sort In the definition of `minima': minima n = take n $ sort
billt
Mark corrected his rewrite. Should have been a dot (function composition) rather than a $ (function application).
Chuck
It's supposed to be a dot, not a $ -- I goofed it like I always do, and edited the post (after you saw the original). The $ is like "put a left parenthesis here and a right paren at the end of the line" while the dot is like saying "apply `take n` to whatever is the result of `sort`.
Mark Rushakoff
+2  A: 

You're trying to sort an infinite list.

Don Stewart
+4  A: 

It's impossible to sort infinite lists in finite time.

As a proof, consider that sorting includes finding the minimum element, and to find the minimum of an infinite list you have to check every element in the list, which will take infinite time.

In your case, however, you know that the list is already sorted. This makes it a special case of sorting infinite lists, namely, sorting sorted lists. This case is solvable.

yairchu