Skip to content

Instantly share code, notes, and snippets.

@kakkun61
Created August 16, 2011 12:25
Show Gist options
  • Save kakkun61/1148954 to your computer and use it in GitHub Desktop.
Save kakkun61/1148954 to your computer and use it in GitHub Desktop.
Real World Haskell, Ex. 3.2.6 (p.72 in Ja)
import Data.List
sortByLength = sortBy ord
where ord l1 l2 = if length l1 < length l2
then LT
else if length l1 > length l2
then GT
else EQ
where l1l = length l1
l2l = length l2
-- 8:8: parse error on input `where'
@md2perpe
Copy link

Why set l1l and l2l when you don't use them anyway?

A simple solution:

import Data.List

sortByLength ls = sortBy compare' ls
    where compare' l1 l2 = compare (length l1) (length l2)

@kakkun61
Copy link
Author

Oh! It's so simple. Thank you.
But I want to know why the error occurred.

@md2perpe
Copy link

Indent the where line so that it starts further to the right than the line before:

                  else EQ
                     where l1l = length l1

@kakkun61
Copy link
Author

Thank you very much.
Hmm, Offside rule is different for me.

@md2perpe
Copy link

@kakkun61
Copy link
Author

I'll try to learn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment