Article View: pl.comp.lang.funkcyjne
Article #719Re: [Haskell] head xs a x:xs
From: Tomasz Zielonka
Date: Thu, 08 Sep 2005 06:22
Date: Thu, 08 Sep 2005 06:22
26 lines
1958 bytes
1958 bytes
Zbyszek Malec wrote:
> Hej
> Mam w haskellu taką prostą funkjcę:
>
> ta się kompiluje
> delete :: Char -> String -> String
> delete c [] = []
> delete c xs | c == (head xs) = delete c (tail xs)
> | otherwise = (head xs):delete c xs
Widzę, że sam sobie poradziłeś z problemem. Kilka komentarzy nt. tej
implementacji:
- używanie head i tail, szczególnie w takiej ilości, jest złym
pomysłem, szczególnie jeśli już się używa pattern-matchingu. Błędy
czasu wykonania (np. head []) nie będą dawać żadnej informacji o ich
miejscu wystąpienia (pewnie sobie z tego zdajesz sprawę).
- jest błędna - delete 'a' "aaabc" = "bbbbbbbbbbbbbbbbbbbbbbbb.....
winne jest użycie xs zamiast (tail xs) na końcu ostatniej linijki.
- niepotrzebnie zawężony typ funkcji - gdybyś usunął sygnaturę typu,
kompilator sam zauważyłby
delete :: Eq a => a -> [a] -> [a]
A może właśnie się uczysz i jeszcze nie dotarłeś do klas typów?
Oczywiście nie zachęcam do niepisania sygnatur typu
Best regards
Tomasz
Technical Details
Message-ID:
<slrndhvm4b.19g.tomasz.zielonka@localhost.localdomain>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!newsfeed.pionier.net.pl!news-fra1.dfn.de!newsfeed.ision.net!ision!newsfeed.freenet.de!newsfeed01.chello.at!newsfeed02.chello.at!news.chello.pl.POSTED!not-for-mailReferences:
<fs4x8h2sta59.kdobpdw4xagk.dlg@40tude.net>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!newsfeed.pionier.net.pl!news-fra1.dfn.de!newsfeed.ision.net!ision!newsfeed.freenet.de!newsfeed01.chello.at!newsfeed02.chello.at!news.chello.pl.POSTED!not-for-mail Newsgroups: pl.comp.lang.funkcyjne From: Tomasz Zielonka <tomasz.zielonka@gmail.com> Subject: Re: [Haskell] head xs a x:xs References: <fs4x8h2sta59.kdobpdw4xagk.dlg@40tude.net> Reply-To: tomasz.zielonka@gmail.com Message-ID: <slrndhvm4b.19g.tomasz.zielonka@localhost.localdomain> Mime-Version: 1.0 User-Agent: slrn/0.9.8.1 (Debian) X-Complaints-To: abuse@chello.pl Organization: chello.pl Date: Thu, 08 Sep 2005 08:22:02 +0200 Lines: 26 NNTP-Posting-Host: 62.179.54.208 (62.179.54.208) NNTP-Posting-Date: Thu, 08 Sep 2005 08:22:02 +0200 X-Trace: ceb2b431fd88a9b16937526447 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit