2014年1月18日土曜日

開発環境

Real World Haskell―実戦で学ぶ関数型言語プログラミング(Bryan O'Sullivan (著)、 John Goerzen (著)、 Don Stewart (著)、山下 伸夫 (翻訳)、伊東 勝利 (翻訳)、株式会社タイムインターメディア (翻訳)、オライリージャパン)の3章(型を定義し、関数を単純化する)、3.13(ガードの条件節の評価)、練習問題6.を解いてみる。

その他参考書籍

練習問題6.

コード(BBEdit)

Sample.hs

{-# OPTIONS -Wall -Werror #-}
module Main where

import qualified Data.List as List

sortBySubListLength :: [[a]] -> [[a]]
sortBySubListLength xs = List.sortBy compareLength xs

compareLength :: [a] -> [a] -> Ordering
compareLength xs ys
    | length xs < length ys = LT
    | length xs == length ys = EQ
    | otherwise = GT

a :: [[Int]]
a = [[1..5], [1..4], [1], [1..3], [1,2]]

b :: [[Int]]
b = [[], [1]]

c::[[a]]
c = [[]]

d :: [String]
d = ["abcde", "a", "abcd", "abc", "ab"]

e :: [[a]]
e = []

入出力結果(Terminal, インタプリタghci)

$ ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :load Sample
[1 of 1] Compiling Main             ( Sample.hs, interpreted )
Ok, modules loaded: Main.
*Main> sortBySubListLength a
[[1],[1,2],[1,2,3],[1,2,3,4],[1,2,3,4,5]]
*Main> sortBySubListLength b
[[],[1]]
*Main> sortBySubListLength c
[[]]
*Main> sortBySubListLength d
["a","ab","abc","abcd","abcde"]
*Main> sortBySubListLength e
[]
*Main> :quit 
Leaving GHCi.
$

0 コメント:

コメントを投稿